S Smith revised this gist . Go to revision
1 file changed, 15 insertions, 18 deletions
twitter-discord-lists.py
| @@ -1,19 +1,20 @@ | |||
| 1 | 1 | #!/usr/bin/env python3 | |
| 2 | 2 | from requests import post | |
| 3 | - | from twitter import Api # pip install python-twitter | |
| 3 | + | import twitter # pip install python-twitter | |
| 4 | 4 | from json import load | |
| 5 | 5 | ||
| 6 | - | lists = [("567697393169072140", "blha303", "reog")] | |
| 7 | - | ||
| 8 | 6 | with open("twitter.json") as f: | |
| 9 | - | api = Api(**load(f)) | |
| 7 | + | api = twitter.Api(**load(f)) | |
| 10 | 8 | ||
| 11 | 9 | with open(".bottoken") as f: | |
| 12 | 10 | token = f.read().strip() | |
| 13 | 11 | ||
| 14 | 12 | ||
| 15 | - | def get_list_since(sn, slug, since): | |
| 16 | - | timeline = api.GetListTimeline(slug=slug, owner_screen_name=sn, since_id=since) | |
| 13 | + | def get_list_since(since): | |
| 14 | + | try: | |
| 15 | + | timeline = api.GetListTimeline(slug=_slug, owner_screen_name=_sn, since_id=since) | |
| 16 | + | except twitter.error.TwitterError: | |
| 17 | + | return [], None | |
| 17 | 18 | return timeline, timeline[0].id if timeline else None | |
| 18 | 19 | ||
| 19 | 20 | def post_to_discord(channel, text): | |
| @@ -29,15 +30,11 @@ def post_to_discord(channel, text): | |||
| 29 | 30 | return "cool" | |
| 30 | 31 | ||
| 31 | 32 | if __name__ == "__main__": | |
| 32 | - | for channel, sn, slug in lists: | |
| 33 | - | try: | |
| 34 | - | with open(".{}{}since".format(sn, slug)) as f: | |
| 35 | - | since = f.read().strip() | |
| 36 | - | except: | |
| 37 | - | since = None | |
| 38 | - | timeline, since = get_list_since(since) | |
| 39 | - | if since: | |
| 40 | - | for t in timeline: | |
| 41 | - | post_to_discord(567697393169072140, "https://twitter.com/{}/status/{}".format(t.user.screen_name, t.id)) | |
| 42 | - | with open(".{}{}since".format(sn, slug)) as f: | |
| 43 | - | f.write(str(since)) | |
| 33 | + | with open(".since") as f: | |
| 34 | + | since = f.read().strip() | |
| 35 | + | timeline, since = get_list_since(since) | |
| 36 | + | if since: | |
| 37 | + | for t in timeline: | |
| 38 | + | post_to_discord(x, "https://twitter.com/{}/status/{}".format(t.user.screen_name, t.id)) | |
| 39 | + | with open(".since", "w") as f: | |
| 40 | + | f.write(str(since)) | |
S Smith revised this gist . Go to revision
1 file changed, 43 insertions
twitter-discord-lists.py(file created)
| @@ -0,0 +1,43 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | from requests import post | |
| 3 | + | from twitter import Api # pip install python-twitter | |
| 4 | + | from json import load | |
| 5 | + | ||
| 6 | + | lists = [("567697393169072140", "blha303", "reog")] | |
| 7 | + | ||
| 8 | + | with open("twitter.json") as f: | |
| 9 | + | api = Api(**load(f)) | |
| 10 | + | ||
| 11 | + | with open(".bottoken") as f: | |
| 12 | + | token = f.read().strip() | |
| 13 | + | ||
| 14 | + | ||
| 15 | + | def get_list_since(sn, slug, since): | |
| 16 | + | timeline = api.GetListTimeline(slug=slug, owner_screen_name=sn, since_id=since) | |
| 17 | + | return timeline, timeline[0].id if timeline else None | |
| 18 | + | ||
| 19 | + | def post_to_discord(channel, text): | |
| 20 | + | """ Posts to Discord like a boss """ | |
| 21 | + | if text is False: | |
| 22 | + | return "cool" | |
| 23 | + | print(text) | |
| 24 | + | d = post("https://discordapp.com/api/channels/{}/messages".format(channel), | |
| 25 | + | json={"content": text}, | |
| 26 | + | headers={"Authorization": "Bot " + token, | |
| 27 | + | "User-Agent": "twitter-discord-lists by suv" | |
| 28 | + | }).json() | |
| 29 | + | return "cool" | |
| 30 | + | ||
| 31 | + | if __name__ == "__main__": | |
| 32 | + | for channel, sn, slug in lists: | |
| 33 | + | try: | |
| 34 | + | with open(".{}{}since".format(sn, slug)) as f: | |
| 35 | + | since = f.read().strip() | |
| 36 | + | except: | |
| 37 | + | since = None | |
| 38 | + | timeline, since = get_list_since(since) | |
| 39 | + | if since: | |
| 40 | + | for t in timeline: | |
| 41 | + | post_to_discord(567697393169072140, "https://twitter.com/{}/status/{}".format(t.user.screen_name, t.id)) | |
| 42 | + | with open(".{}{}since".format(sn, slug)) as f: | |
| 43 | + | f.write(str(since)) | |