twitter-discord-lists.py
· 1.4 KiB · Python
Raw
#!/usr/bin/env python3
from requests import post
from twitter import Api # pip install python-twitter
from json import load
lists = [("567697393169072140", "blha303", "reog")]
with open("twitter.json") as f:
api = Api(**load(f))
with open(".bottoken") as f:
token = f.read().strip()
def get_list_since(sn, slug, since):
timeline = api.GetListTimeline(slug=slug, owner_screen_name=sn, since_id=since)
return timeline, timeline[0].id if timeline else None
def post_to_discord(channel, text):
""" Posts to Discord like a boss """
if text is False:
return "cool"
print(text)
d = post("https://discordapp.com/api/channels/{}/messages".format(channel),
json={"content": text},
headers={"Authorization": "Bot " + token,
"User-Agent": "twitter-discord-lists by suv"
}).json()
return "cool"
if __name__ == "__main__":
for channel, sn, slug in lists:
try:
with open(".{}{}since".format(sn, slug)) as f:
since = f.read().strip()
except:
since = None
timeline, since = get_list_since(since)
if since:
for t in timeline:
post_to_discord(567697393169072140, "https://twitter.com/{}/status/{}".format(t.user.screen_name, t.id))
with open(".{}{}since".format(sn, slug)) as f:
f.write(str(since))
| 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)) |
| 44 |