author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 13:35:58 +0100 | |
changeset 271 | c8b8b7cc8896 |
parent 268 | 4dd196a4ee7c |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
0 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
9 |
"os" |
198
e9c2947a84a6
Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
10 |
"strings" |
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
11 |
|
0 | 12 |
"github.com/spf13/cobra" |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
13 |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
239
diff
changeset
|
14 |
"github.com/McKael/madon/v3" |
0 | 15 |
) |
16 |
||
17 |
var timelineOpts struct { |
|
200
488f5fc5be98
Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
198
diff
changeset
|
18 |
local, onlyMedia bool |
488f5fc5be98
Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
198
diff
changeset
|
19 |
limit, keep uint |
267 | 20 |
sinceID, maxID madon.ActivityID |
0 | 21 |
} |
22 |
||
23 |
// timelineCmd represents the timelines command |
|
24 |
var timelineCmd = &cobra.Command{ |
|
235
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
25 |
Use: "timeline [home|public|direct|:HASHTAG|!list_id] [--local]", |
0 | 26 |
Aliases: []string{"tl"}, |
27 |
Short: "Fetch a timeline", |
|
28 |
Long: ` |
|
29 |
The timeline command fetches a timeline (home, local or federated). |
|
235
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
30 |
The timeline "direct" contains only direct messages (that is, messages with |
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
31 |
visibility set to "direct"). |
0 | 32 |
It can also get a hashtag-based timeline if the keyword or prefixed with |
198
e9c2947a84a6
Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
33 |
':' or '#', or a list-based timeline (use !ID with the list ID).`, |
0 | 34 |
Example: ` madonctl timeline |
35 |
madonctl timeline public --local |
|
198
e9c2947a84a6
Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
36 |
madonctl timeline '!42' |
235
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
37 |
madonctl timeline :mastodon |
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
38 |
madonctl timeline direct`, |
0 | 39 |
RunE: timelineRunE, |
235
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
40 |
ValidArgs: []string{"home", "public", "direct"}, |
0 | 41 |
} |
42 |
||
43 |
func init() { |
|
44 |
RootCmd.AddCommand(timelineCmd) |
|
45 |
||
46 |
timelineCmd.Flags().BoolVar(&timelineOpts.local, "local", false, "Posts from the local instance") |
|
200
488f5fc5be98
Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
198
diff
changeset
|
47 |
timelineCmd.Flags().BoolVar(&timelineOpts.onlyMedia, "only-media", false, "Only statuses with media attachments") |
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
48 |
timelineCmd.Flags().UintVarP(&timelineOpts.limit, "limit", "l", 0, "Limit number of API results") |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
49 |
timelineCmd.Flags().UintVarP(&timelineOpts.keep, "keep", "k", 0, "Limit number of results") |
267 | 50 |
timelineCmd.PersistentFlags().StringVar(&timelineOpts.sinceID, "since-id", "", "Request IDs greater than a value") |
51 |
timelineCmd.PersistentFlags().StringVar(&timelineOpts.maxID, "max-id", "", "Request IDs less (or equal) than a value") |
|
0 | 52 |
} |
53 |
||
54 |
func timelineRunE(cmd *cobra.Command, args []string) error { |
|
55 |
opt := timelineOpts |
|
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
56 |
var limOpts *madon.LimitParams |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
57 |
|
267 | 58 |
if opt.limit > 0 || opt.sinceID != "" || opt.maxID != "" { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
59 |
limOpts = new(madon.LimitParams) |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
60 |
} |
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
61 |
|
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
62 |
if opt.limit > 0 { |
20
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
63 |
limOpts.Limit = int(opt.limit) |
b0ccc09f07a2
Sync with Madon library update; use limit API parameter
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
64 |
} |
267 | 65 |
if opt.maxID != "" { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
66 |
limOpts.MaxID = opt.maxID |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
67 |
} |
267 | 68 |
if opt.sinceID != "" { |
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
69 |
limOpts.SinceID = opt.sinceID |
22
5778b09bc6fe
Add --since-id and --max-id
Mikael Berthe <mikael@lilotux.net>
parents:
20
diff
changeset
|
70 |
} |
0 | 71 |
|
72 |
tl := "home" |
|
73 |
if len(args) > 0 { |
|
74 |
tl = args[0] |
|
75 |
} |
|
76 |
||
198
e9c2947a84a6
Add support for list-based timelines
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
77 |
// Home timeline and list-based timeline require to be logged in |
235
747f108d5821
timeline: Allow "direct" timeline
Mikael Berthe <mikael@lilotux.net>
parents:
200
diff
changeset
|
78 |
if err := madonInit(tl == "home" || tl == "direct" || strings.HasPrefix(tl, "!")); err != nil { |
0 | 79 |
return err |
80 |
} |
|
81 |
||
200
488f5fc5be98
Add option --only-media to madonctl timelines (Mastodon 2.3.0+)
Mikael Berthe <mikael@lilotux.net>
parents:
198
diff
changeset
|
82 |
sl, err := gClient.GetTimelines(tl, opt.local, opt.onlyMedia, limOpts) |
0 | 83 |
if err != nil { |
84 |
errPrint("Error: %s", err.Error()) |
|
47
82d8b6074309
Set exit code to non-zero when API calls fail
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
85 |
os.Exit(1) |
0 | 86 |
} |
87 |
||
167
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
88 |
if opt.keep > 0 && len(sl) > int(opt.keep) { |
1341bacd01c9
Add option --keep to keep the N last items
Mikael Berthe <mikael@lilotux.net>
parents:
110
diff
changeset
|
89 |
sl = sl[:opt.keep] |
13
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
90 |
} |
f862af8faf17
Add option --limit (-l) to limit the number of displayed results
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
91 |
|
0 | 92 |
p, err := getPrinter() |
93 |
if err != nil { |
|
81
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
94 |
errPrint("Error: %s", err.Error()) |
b1671f83e91b
Do not display usage when GetPrinter fails
Mikael Berthe <mikael@lilotux.net>
parents:
47
diff
changeset
|
95 |
os.Exit(1) |
0 | 96 |
} |
110 | 97 |
return p.printObj(sl) |
0 | 98 |
} |