-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtwitterSays.py
More file actions
executable file
·50 lines (45 loc) · 1.64 KB
/
Copy pathtwitterSays.py
File metadata and controls
executable file
·50 lines (45 loc) · 1.64 KB
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
import os, time
import cPickle as pickle
import twython as Twython
from urllib import quote
from SETTINGS import *
api = Twython.Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
latest_tweet_id = 0
def first_run():
file_exists = os.path.exists('sav.p')
if file_exists is False:
user_timeline = api.get_user_timeline(screen_name=user_name, count=2)#This 'count' is how many tweets back from the most recent tweet that will be fowarded to telegram
tweet_id = user_timeline[1]['id']
file_pickle(tweet_id)
def get_timeline(latest_tweet_id):
user_timeline = api.get_user_timeline(screen_name=user_name, since_id=latest_tweet_id)
return user_timeline
def read_latest_id():
line = file_unpickle()
if len(str(line)) < 2:
return 0
else:
return line
def send_message(msg):
msg = quote(msg, safe='')
link = 'https://api.telegram.org/bot'+telegram_token+'/sendMessage?chat_id=@'+channel_name+'\&text="' + msg + '"'
os.system('curl '+ link)
def file_pickle(var):
pickle.dump(var, open("sav.p", "wb"))
def file_unpickle():
saved = pickle.load(open('sav.p', "rb"))
return saved
def main():
latest_tweet_id = read_latest_id()
user_timeline = get_timeline(latest_tweet_id)
number_of_tweets = len(user_timeline)
if number_of_tweets > 0:
for i in reversed(range(0,number_of_tweets)):
if user_timeline[i]['text']:
print user_timeline[i]['text']
send_message(user_timeline[i]['text'])
time.sleep(4)
latest_tweet_id = user_timeline[0]['id']
file_pickle(latest_tweet_id)
first_run()
main()