forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrubjay-p-t
executable file
·87 lines (70 loc) · 2.97 KB
/
scrubjay-p-t
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
#!/usr/bin/env python
# An example of how to use tweepy for the Twitter API.
# Unfortunately, OAuth seems to rely on every user having their own
# secret app code; that means that to use this, you have to register
# a new app with a new name, then get your own consumer and consumer_secret
# keys; then you can use this app to get your own token and secret token.
# Why OAuth requires four tokens if it can only ever be used for one
# user at a time and the tokens aren't supposed to be shared is beyond me.
#
# Get your tokens when you register your app on Twitter's website,
# then set up ~/.config/YOURAPPNAME/ to look like this:
# consumer YOUR_CONSUMER_KEY
# consumer_secret YOUR_CONSUMER_SECRET_KEY
# access_token YOUR_ACCESS_TOKEN
# access_token_secret YOUR_ACCESS_TOKEN_SECRET
# Module Documentation:
# https://python-twitter.readthedocs.io/en/latest/twitter.html
import twitter
import sys
import os
import time
class TwitAPI:
def __init__(self, appname):
self.api = None
self.tweets_seen = set()
if 'XDG_CONFIG_HOME' in os.environ:
confdir = os.path.join(os.environ['XDG_CONFIG_HOME'], appname)
else:
confdir = os.path.expanduser("~/.config/%s" % appname)
conffile = os.path.join(confdir, "auth")
oauthtokens = {}
with open(conffile) as conf:
for line in conf:
line = line.split()
oauthtokens[line[0]] = line[1]
if "consumer" not in oauthtokens or \
"consumer_secret" not in oauthtokens or \
"access_token" not in oauthtokens or \
"access_token_secret" not in oauthtokens:
self.api = None
raise(RuntimeError("Need all four OAuth tokens in %s" \
% conffile))
self.api = twitter.Api(consumer_key=oauthtokens["consumer"],
consumer_secret=oauthtokens["consumer_secret"],
access_token_key=oauthtokens["access_token"],
access_token_secret=oauthtokens["access_token_secret"])
# By this point we should be authenticated.
def print_timeline(self):
timeline = self.api.GetHomeTimeline() # Returns a twitter.Status
print("\n========================================================")
for tweet in timeline:
if tweet.id in self.tweets_seen:
continue
print("\n=== %s (%s) ===" % (tweet.user.screen_name,
tweet.user.name))
print(tweet.text)
print("\n %s" % tweet.created_at)
self.tweets_seen.add(tweet.id)
if __name__ == '__main__':
appname = os.path.basename(sys.argv[0])
twitter = TwitAPI(appname)
print("Authorized")
# try:
# twitter = TwitAPI(appname)
# except:
# print("Authentication failed, sorry")
# sys.exit(1)
while True:
twitter.print_timeline()
time.sleep(60)