-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_word_from_dictionary.py
72 lines (57 loc) · 2.03 KB
/
tweet_word_from_dictionary.py
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
# -*- coding: utf-8 -*-
"""
This script is inspired by https://github.com/ZiTAL/bermiotarra/tree/master/bot by @ZiTAL
"""
import json
import datetime
import random
from TwitterAPI import TwitterAPI
from mastodon import Mastodon
def tweet_word_from_dictionary():
word = get_random_untweeted_word()
if word:
tweet_word(word)
toot_word(word)
def get_random_untweeted_word():
dictionary = json.load(open("dictionary.json"))
word = random.choice(dictionary)
while word and word.get("tweeted"):
word = random.choice(dictionary)
word["tweeted"] = True
word["tweeted_at"] = datetime.datetime.utcnow().isoformat()
dictionary.remove(word)
dictionary.append(word)
json.dump(dictionary, open("dictionary.json", "w"))
return word
def tweet_word(word):
text = "Egunian berba edo esamolde bat. Gaur {word}: {meaning} {url} #eibar #eibarkoeuskara"
text = text.format(
word=word["entry"], meaning=word.get("meaning", "").strip(), url=word["url"]
)
with open("credentials.twitter.json") as fp:
credentials = json.load(fp)
api = TwitterAPI(
credentials["API_KEY"],
credentials["API_SECRET"],
credentials["ACCESS_TOKEN_KEY"],
credentials["ACCESS_TOKEN_SECRET"],
)
res = api.request("statuses/update", {"status": text})
if res.response.ok:
print('Tweeted: "{}"'.format(text))
def toot_word(word):
text = "Egunian berba edo esamolde bat. Gaur {word}: {meaning} {url} #eibar #eibarkoeuskara"
text = text.format(
word=word["entry"], meaning=word.get("meaning", "").strip(), url=word["url"]
)
with open("credentials.mastodon.json") as fp:
credentials = json.load(fp)
api = Mastodon(
access_token=credentials["ACCESS_TOKEN"],
api_base_url="https://mastodon.eus"
)
res = api.toot(text)
if res.get('id'):
print('Tooted: "{}"'.format(text))
if __name__ == "__main__":
tweet_word_from_dictionary()