-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_cmd_line.py
78 lines (63 loc) · 2.89 KB
/
twitter_cmd_line.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
import twitter
import os
import sys
import re
import json
class TwitterCmdLine(object):
#Twitter Api initialization
def __init__(self):
consumer_key = None
consumer_secret = os.environ.get('CONSUMER_SECRET')
access_token = os.environ.get('ACCESS_TOKEN')
access_secret_token = os.environ.get('ACCESS_SECRET_TOKEN')
#read credentials from text file if exists
json_file = open('credentials.txt', 'a+')
json_data = json_file.read()
#Enter the access tokens if no token exists
if not consumer_key or not consumer_secret or \
not access_token or not access_secret_token:
if json_data:
credentials = json.loads(json_data)
consumer_key = credentials['consumer_key']
consumer_secret = credentials['consumer_secret']
access_token = credentials['access_token']
access_secret_token = credentials['access_secret_token']
else:
consumer_key = raw_input("please enter your twitter consumer key")
consumer_secret = raw_input("Please enter your twitter consumer secret")
access_token = raw_input("Please enter your twitter access token")
access_secret_token = raw_input("Please enter your twitter secret token")
#Twitter api initialization
self.api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token,
access_token_secret=access_secret_token)
#if no credentials available create and store in a file
if not json_data:
self.api.VerifyCredentials()
json_data = {'consumer_key': consumer_key, 'consumer_secret': consumer_secret,
'access_token': access_token, 'access_secret_token': access_secret_token}
json_str = json.dumps(json_data)
json_file.write(json_str)
json_file.close()
#Displays all the friends list
def show_friends(self):
friends_list = self.api.GetFriends()
for friend in friends_list:
print friend.name
def show_timeline_tweets(self, tweet_number):
tweets = self.api.GetHomeTimeline(count=10)
print len(tweets)
if __name__ == '__main__':
twitter_cmd = TwitterCmdLine()
try:
twitter_cmd.api.VerifyCredentials()
tweet_number = re.match('\d+', sys.argv[1])
if tweet_number:
tweet_number = tweet_number.group(0)
twitter_cmd.show_timeline_tweets(tweet_number)
#python twitter_cmd_line.py friends will display friends
if sys.argv[1] == 'friends':
twitter_cmd.show_friends()
except twitter.TwitterError as errors:
print errors.message[0]['message']