diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e38da20 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +settings.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/golang_news.go b/golang_news.go new file mode 100644 index 0000000..a5e1daf --- /dev/null +++ b/golang_news.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "github.com/ChimeraCoder/anaconda" + rss "github.com/jteeuwen/go-pkg-rss" + "net/url" + "os" + "time" +) + +const timeout = 50 + +func main() { + go PollFeed("http://blog.golang.org/feed.atom", timeout) + go PollFeed("https://news.ycombinator.com/rss", timeout) + PollFeed("http://www.reddit.com/r/golang.rss", timeout) +} + +func PollFeed(uri string, timeout int) { + feed := rss.New(timeout, true, chanHandler, itemHandler) + + for { + if err := feed.Fetch(uri, nil); err != nil { + fmt.Fprintf(os.Stderr, "[e] %s: %s", uri, err) + return + } + + <-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9)) + } +} + +func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) { + fmt.Printf("%d new channel(s) in %s\n", len(newchannels), feed.Url) +} + +func itemHandler(feed *rss.Feed, ch *rss.Channel, newItems []*rss.Item) { + fmt.Printf("%d new item(s) in %s\n", len(newItems), feed.Url) +} + +func PostTweet(tweet string) { + anaconda.SetConsumerKey("9AuRCuPxHAuvPdLIt9Sg") + anaconda.SetConsumerSecret("14pYoR2B3IAeRsQiOZrSrjweseNj8YW97XbHjwkp4") + api := anaconda.NewTwitterApi("404687248-c3I6DzhE3KpZvX7lX1J3DFw1AFrtzj8xiwldvBJG", "P7pFC7ZEpKp5WvqtK6xnY0GPyH8Fqt9eOjDrjqi0jlIbO") + + v := url.Values{} + api.PostTweet(tweet, v) +} diff --git a/settings.go b/settings.go new file mode 100644 index 0000000..1561e8e --- /dev/null +++ b/settings.go @@ -0,0 +1,39 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +type settings struct { + ConsumerKey string + ConsumerSecret string + AccessToken string + AccessTokenSecret string +} + +func ReadConsumerKey() string { + return readSettings().ConsumerKey +} + +func ReadConsumerSecret() string { + return readSettings().ConsumerSecret +} + +func ReadAccessToken() string { + return readSettings().AccessToken +} + +func readSettings() settings { + file, err := ioutil.ReadFile("./settings.json") + if err != nil { + fmt.Errorf("File error: %v\n", err) + } + var s settings + err = json.Unmarshal(file, &s) + if err != nil { + fmt.Errorf("File failed to decode: %v\n", err) + } + return s +} diff --git a/settings.json.example b/settings.json.example new file mode 100644 index 0000000..0cf158d --- /dev/null +++ b/settings.json.example @@ -0,0 +1,6 @@ +{ + "ConsumerKey": "your-consumer-key", + "ConsumerSecret": "your-consumer-secret", + "AccessToken": "your-access-token", + "AccessTokenSecret": "your-access-token-secret" +}