-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
executable file
·181 lines (156 loc) · 6.28 KB
/
twitter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python3
#!/usr/bin/env python
#requirement BeautifulSoup (http://www.crummy.com/software/BeautifulSoup)
# get it via pip install beautifulsoup4
from bs4 import BeautifulSoup
from datetime import datetime
from subprocess import call
import requests
import os.path
from time import sleep
import sys
def read_new_tweets(twitter_accountname):
"""fetchtes tweets from twitter.com and puts timestamp and tweettext into an array."""
url = "http://twitter.com/" + twitter_accountname
doc = "";
additional_headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5"
}
try:
doc = requests.get(url, headers=additional_headers)
except KeyboardInterrupt:
sys.exit()
#write backup
myfile = open(twitter_accountname + '.html', 'w')
myfile.write(doc.text)
myfile.close()
#parse html
soup = BeautifulSoup(doc.text, 'html.parser')
lots = []
rows = soup.select(".js-stream-tweet")
if len(rows) > 0:
for row in rows:
tweet_timestamp = int(row.select(".js-short-timestamp")[0]['data-time'])
tweet_text = row.select(".js-tweet-text")
if len(tweet_text) > 0:
tweet_text = tweet_text[0].text
lots.append((tweet_timestamp, tweet_text))
else:
print("No tweets found in HTML")
return lots
def escape_str(orig_str):
escaped_str = []
for cur_char in orig_str:
if "\n" == cur_char:
escaped_str.append('\\')
escaped_str.append("n")
elif "\\" == cur_char:
escaped_str.append('\\')
escaped_str.append('\\')
else:
escaped_str.append(cur_char)
return ''.join(escaped_str)
def unescape_str(orig_str):
unescaped_str = []
encountered_backslash = False
for cur_char in orig_str:
if encountered_backslash:
if "n" == cur_char:
unescaped_str.append("\n")
elif "\\" == cur_char:
unescaped_str.append("\\")
else:
unescaped_str.append("\\")
unescaped_str.append(cur_char)
encountered_backslash = False
else:
if "\\" == cur_char:
encountered_backslash = True
else:
unescaped_str.append(cur_char)
return ''.join(unescaped_str)
def read_old_tweets(twitter_accountname):
"""reads in the corresponding .csv file and returns an array with the read data"""
tweet_list = []
my_filename = twitter_accountname + ".csv"
if os.path.isfile(my_filename):
with open(my_filename, 'r') as myfile:
for curline in myfile:
try:
pos_of_comma = curline.find(',')
if pos_of_comma > -1:
tweet_timestamp = int(curline[0:pos_of_comma])
tweet_text = unescape_str(curline[pos_of_comma + 1:])
tweet_list.append((tweet_timestamp, tweet_text))
except ValueError:
pass
return tweet_list
def diff_on_tweet_list(old_tweet_list, new_tweet_list):
"""returns new tweets from new_tweet_list which are not in old_tweet_list"""
list_not_in_old_tweets = []
for cur_new_tweet in new_tweet_list:
found_in_both = False
for cur_old_tweet in old_tweet_list:
if cur_old_tweet[0] == cur_new_tweet[0]:
found_in_both = True
break
if not found_in_both:
list_not_in_old_tweets.append(cur_new_tweet)
return list_not_in_old_tweets
def write_tweets(twitter_accountname, list_of_tweets):
"""writes a .csv file with the list of tweets"""
with open(twitter_accountname + ".csv", 'w') as tweet_file:
for cur_tweet in list_of_tweets:
tweet_file.write(str(cur_tweet[0]) + "," + escape_str(cur_tweet[1]))
tweet_file.write("\n")
def notify_about_new_tweets(twitter_accountname, list_of_new_tweets, batch_mode):
"""Uses system functionality to notify the user of new tweets"""
perform_sleep_wait = True
first_iteration = True
path_to_image = get_cwd() + "/apple-touch-icon.png"
for cur_tweet in list_of_new_tweets:
cur_delay = 11000 + int(len(cur_tweet[1]) / 40)
tweet_timestr = datetime.fromtimestamp(cur_tweet[0]).strftime("%d. %b %H:%I:%S")
notify_title = "@" + twitter_accountname + " " + tweet_timestr + ":"
notify_text = cur_tweet[1]
if not batch_mode:
try:
if perform_sleep_wait:
if first_iteration:
first_iteration = False
else:
sleep(cur_delay / 1000)
except KeyboardInterrupt:
perform_sleep_wait = False
call(["notify-send", "-c", "low", "-t", str(cur_delay - 500), "-i", path_to_image, notify_title, notify_text ])
print("Notification: " + notify_title + " " + notify_text)
def get_cwd():
return os.path.dirname(os.path.realpath(__file__))
def main(twitter_accountname, batch_mode):
current_tweets = read_new_tweets(twitter_accountname)
old_tweets = read_old_tweets(twitter_accountname)
list_of_new_tweets = diff_on_tweet_list(old_tweets, current_tweets)
write_tweets(twitter_accountname, old_tweets + list_of_new_tweets)
if len(list_of_new_tweets) > 0:
notify_about_new_tweets(twitter_accountname, list_of_new_tweets, batch_mode)
if __name__ == "__main__":
if len(sys.argv) > 1:
batch_mode = False
list_of_non_hyphen_arguments = []
i = 0
for cur_arg in sys.argv:
if i > 0:
if cur_arg[0] == "-":
if cur_arg == "-b":
batch_mode = True
else:
list_of_non_hyphen_arguments.append(cur_arg);
i = i + 1
if len(list_of_non_hyphen_arguments) > 0:
main(list_of_non_hyphen_arguments[0], batch_mode)
else:
print("Twitter accountname required. None given. Aborting.")
else:
print("Twitter accountname required. None given. Aborting.")