|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/djthorpe/go-tablewriter" |
| 9 | + "github.com/mutablelogic/go-client" |
| 10 | + "github.com/mutablelogic/go-client/pkg/newsapi" |
| 11 | +) |
| 12 | + |
| 13 | +/////////////////////////////////////////////////////////////////////////////// |
| 14 | +// GLOBALS |
| 15 | + |
| 16 | +var ( |
| 17 | + newsapiName = "newsapi" |
| 18 | + newsapiClient *newsapi.Client |
| 19 | + newsapiCategory string |
| 20 | + newsapiLanguage string |
| 21 | + newsapiCountry string |
| 22 | +) |
| 23 | + |
| 24 | +/////////////////////////////////////////////////////////////////////////////// |
| 25 | +// LIFECYCLE |
| 26 | + |
| 27 | +func newsapiRegister(flags *Flags) { |
| 28 | + // Register flags required |
| 29 | + flags.String(newsapiName, "newsapi-key", "${NEWSAPI_KEY}", "API Key") |
| 30 | + flags.String(newsapiName, "category", "", "News category: business, entertainment, general, health, science, sports, technology") |
| 31 | + flags.String(newsapiName, "language", "", "ISO 639 language code") |
| 32 | + flags.String(newsapiName, "country", "", "ISO 3166 country code") |
| 33 | + |
| 34 | + flags.Register(Cmd{ |
| 35 | + Name: newsapiName, |
| 36 | + Description: "Obtain news headlines from https://newsapi.org/", |
| 37 | + Parse: inewsapiParse, |
| 38 | + Fn: []Fn{ |
| 39 | + {Name: "sources", Call: newsapiSources, Description: "Return sources of news"}, |
| 40 | + {Name: "headlines", Call: newsapiHeadlines, Description: "Return top headlines from news sources"}, |
| 41 | + {Name: "search", Call: newsapiArticles, Description: "Return articles from news sources with search term", MaxArgs: 1}, |
| 42 | + }, |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func inewsapiParse(flags *Flags, opts ...client.ClientOpt) error { |
| 47 | + apiKey := flags.GetString("newsapi-key") |
| 48 | + if apiKey == "" { |
| 49 | + return fmt.Errorf("missing -newsapi-key flag") |
| 50 | + } |
| 51 | + if client, err := newsapi.New(flags.GetString("newsapi-key"), opts...); err != nil { |
| 52 | + return err |
| 53 | + } else { |
| 54 | + newsapiClient = client |
| 55 | + } |
| 56 | + |
| 57 | + // Set category |
| 58 | + newsapiCategory = strings.ToLower(flags.GetString("category")) |
| 59 | + newsapiLanguage = strings.ToLower(flags.GetString("language")) |
| 60 | + newsapiCountry = strings.ToLower(flags.GetString("country")) |
| 61 | + |
| 62 | + // Return success |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +/////////////////////////////////////////////////////////////////////////////// |
| 67 | +// METHODS |
| 68 | + |
| 69 | +func newsapiSources(w *tablewriter.TableWriter, _ []string) error { |
| 70 | + // Set options |
| 71 | + opts := []newsapi.Opt{} |
| 72 | + if newsapiCategory != "" { |
| 73 | + opts = append(opts, newsapi.OptCategory(newsapiCategory)) |
| 74 | + } |
| 75 | + if newsapiLanguage != "" { |
| 76 | + opts = append(opts, newsapi.OptLanguage(newsapiLanguage)) |
| 77 | + } |
| 78 | + if newsapiCountry != "" { |
| 79 | + opts = append(opts, newsapi.OptCountry(newsapiCountry)) |
| 80 | + } |
| 81 | + |
| 82 | + // Request -> Response |
| 83 | + sources, err := newsapiClient.Sources(opts...) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + // Write table |
| 89 | + return w.Write(sources) |
| 90 | +} |
| 91 | + |
| 92 | +func newsapiHeadlines(w *tablewriter.TableWriter, _ []string) error { |
| 93 | + // Set options |
| 94 | + opts := []newsapi.Opt{} |
| 95 | + if newsapiCategory != "" { |
| 96 | + opts = append(opts, newsapi.OptCategory(newsapiCategory)) |
| 97 | + } |
| 98 | + if newsapiLanguage != "" { |
| 99 | + opts = append(opts, newsapi.OptLanguage(newsapiLanguage)) |
| 100 | + } |
| 101 | + if newsapiCountry != "" { |
| 102 | + opts = append(opts, newsapi.OptCountry(newsapiCountry)) |
| 103 | + } |
| 104 | + |
| 105 | + // Request -> Response |
| 106 | + articles, err := newsapiClient.Headlines(opts...) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + // Write table |
| 112 | + return w.Write(articles) |
| 113 | +} |
| 114 | + |
| 115 | +func newsapiArticles(w *tablewriter.TableWriter, args []string) error { |
| 116 | + // Set options |
| 117 | + opts := []newsapi.Opt{} |
| 118 | + if newsapiCategory != "" { |
| 119 | + opts = append(opts, newsapi.OptCategory(newsapiCategory)) |
| 120 | + } |
| 121 | + if newsapiLanguage != "" { |
| 122 | + opts = append(opts, newsapi.OptLanguage(newsapiLanguage)) |
| 123 | + } |
| 124 | + if newsapiCountry != "" { |
| 125 | + opts = append(opts, newsapi.OptCountry(newsapiCountry)) |
| 126 | + } |
| 127 | + // Set query |
| 128 | + if len(args) > 0 { |
| 129 | + q := strconv.Quote(args[0]) |
| 130 | + opts = append(opts, newsapi.OptQuery(q)) |
| 131 | + } |
| 132 | + |
| 133 | + // Request -> Response |
| 134 | + articles, err := newsapiClient.Articles(opts...) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + // Write table |
| 140 | + return w.Write(articles) |
| 141 | +} |
0 commit comments