This repository has been archived by the owner on Aug 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter2rss.go
87 lines (72 loc) · 2.1 KB
/
twitter2rss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/feeds"
"github.com/volker-fr/twitter2rss/config"
"github.com/volker-fr/twitter2rss/feed"
"github.com/volker-fr/twitter2rss/version"
"github.com/davecgh/go-spew/spew"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
var conf config.Config = config.LoadConfig()
func processAPIError(message string, error error) {
fmt.Printf(message)
spew.Dump(error)
}
func getRss() string {
twitterConfig := oauth1.NewConfig(conf.ConsumerKey, conf.ConsumerSecret)
token := oauth1.NewToken(conf.AccessToken, conf.AccessSecret)
// OAuth1 http.Client will automatically authorize Requests
httpClient := twitterConfig.Client(oauth1.NoContext, token)
// Twitter Client
client := twitter.NewClient(httpClient)
// debugging & testing
/*if conf.Debug {
var tweetId int64 = 7654321
tweet, _, err := client.Statuses.Show(tweetId, &twitter.StatusShowParams{})
if err != nil {
processAPIError("Couldn't load client.Statuses.Show: ", err)
return ""
}
fmt.Println(parser.GetTweetUrl(*tweet))
spew.Dump(tweet)
fmt.Println(parser.ParseTweetText(*tweet))
return ""
}*/
// Get timeline
homeTimelineParams := &twitter.HomeTimelineParams{Count: conf.MaxTweets}
tweets, _, err := client.Timelines.HomeTimeline(homeTimelineParams)
if err != nil {
processAPIError("Couldn't load HomeTimeline: ", err)
return ""
}
// get either single tweets or combine multiple tweets of the same author together
var rssFeed *feeds.Feed
if conf.CombinedFeed == false {
rssFeed = feed.CreateIndividualFeed(conf, tweets)
} else {
rssFeed = feed.CreateCombinedUserFeed(conf, tweets)
}
// Create feed
atom, err := rssFeed.ToAtom()
if err != nil {
log.Fatal(err)
}
return atom
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", getRss())
}
func main() {
fmt.Println("twitter2rss version", version.GetVersion())
//if conf.Debug {
// _ = getRss()
//}
// TODO: add logging
// TODO: add error handling in case the port is already in use
http.HandleFunc("/", handler)
http.ListenAndServe("127.0.0.1:8080", nil)
}