-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwotdbot.py
136 lines (115 loc) · 3.56 KB
/
wotdbot.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
"""
Pick a random [Finnish] word from a word list,
open its Wiktionary page and tweet it
"""
import argparse
import random
import sys
import webbrowser
from urllib.parse import quote
import yaml # pip install pyyaml
from twitter import OAuth, Twitter # pip install twitter
def load_yaml(filename):
with open(filename) as f:
data = yaml.safe_load(f)
if not data.keys() >= {
"oauth_token",
"oauth_token_secret",
"consumer_key",
"consumer_secret",
}:
sys.exit("Twitter credentials missing from YAML: " + filename)
return data
def random_word(filename):
words = []
with open(filename, encoding="utf-8") as infile:
for line in infile:
words.append(line.rstrip())
print("Loaded", len(words), "words")
randnum = random.randrange(len(words))
print("Random number:", randnum)
word = words[randnum]
print(word)
return word
def open_url(url):
print(url)
if not args.no_web:
webbrowser.open(url, new=2) # 2 = open in a new tab, if possible
def tweet_it(string, credentials):
if len(string) <= 0:
return
# Create and authorise an app with (read and) write access at:
# https://dev.twitter.com/apps/new
# Store credentials in YAML file. See data/onthisday_example.yaml
t = Twitter(
auth=OAuth(
credentials["oauth_token"],
credentials["oauth_token_secret"],
credentials["consumer_key"],
credentials["consumer_secret"],
)
)
print("TWEETING THIS:\n", string)
if args.test:
print("(Test mode, not actually tweeting)")
else:
result = t.statuses.update(status=string)
url = (
"http://twitter.com/"
+ result["user"]["screen_name"]
+ "/status/"
+ result["id_str"]
)
print("Tweeted:\n" + url)
if not args.no_web:
webbrowser.open(url, new=2) # 2 = open in a new tab, if possible
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Pick a random word from a word list, open its "
"Wiktionary page and tweet it",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-y",
"--yaml",
default="/Users/hugo/Dropbox/bin/data/wotdbot.yaml",
help="YAML file location containing Twitter keys and secrets",
)
parser.add_argument(
"-w",
"--wordlist",
default="data/finnish.txt",
help="Filename of word list with a single word per line",
)
parser.add_argument(
"-x", "--test", action="store_true", help="Test mode: don't tweet"
)
parser.add_argument(
"-nw",
"--no-web",
action="store_true",
help="Don't open a web browser to show the tweeted tweet",
)
args = parser.parse_args()
twitter_credentials = load_yaml(args.yaml)
# Can generate word lists with wotdbot_extract_words.py
word = random_word(args.wordlist)
url_word = quote(word.encode("utf8"))
foreign_url = "https://fi.wiktionary.org/wiki/" + url_word + "#Suomi"
open_url(foreign_url)
native_url = "https://en.wiktionary.org/wiki/" + url_word + "#Finnish"
open_url(native_url)
tweet = (
"Finnish word of the day: "
+ word
+ " "
+ native_url
+ " "
+ foreign_url
+ " #Finnish #WOTD #Suomi #"
+ word.replace(" ", "")
)
print("Tweet this:\n", tweet)
tweet_it(tweet, twitter_credentials)
# End of file