-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterAPI.py
More file actions
104 lines (96 loc) · 3.81 KB
/
twitterAPI.py
File metadata and controls
104 lines (96 loc) · 3.81 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
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
import urllib.request, urllib.parse, urllib.error
from twurl import augment
import ssl
import json
import sys
import sqlite3
def main():
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
if len(sys.argv) <= 1:
print('Not a valid command. Help:')
print('-stat: gives the user status.')
print('-friends: gives the user friends.')
print('-svfr: save users friend in a data base.')
else:
if len(sys.argv) == 2 and sys.argv[1] == "-stat":
account = input('Enter account name:')
num_stat = input('Enter numbers of status:')
user_status(account,num_stat,ctx)
if len(sys.argv) == 2 and sys.argv[1] == "-friends":
account = input('Enter account name:')
num_friends = input('Enter numbers of friends:')
user_friends(account,num_friends,ctx)
if len(sys.argv) == 2 and sys.argv[1] == "-svfr":
account = input('Enter account name:')
num_friends = input('Enter numbers of friends:')
save_user_friends(account,num_friends,ctx)
def save_user_friends(account,num_friends,ctx):
conn = sqlite3.connect('twitter.db')
cur = conn.cursor()
create_sql = "CREATE TABLE IF NOT EXISTS " + account + " (id INTEGER PRIMARY KEY, friend_name TEXT UNIQUE)"
cur.execute(create_sql)
print("Calling Twitter...")
if not num_friends.isnumeric():
num_friends = 50
url = 'https://api.twitter.com/1.1/friends/list.json'
formed_url = augment(url,{'screen_name': account,'count': num_friends})
try:
connection = urllib.request.urlopen(formed_url, context=ctx)
except:
print('Not a valid account')
quit()
data = connection.read().decode()
headers = dict(connection.getheaders())
print('Remaining:',headers['x-rate-limit-remaining'])
print('------------------------------------------------')
js = json.loads(data)
for user in js['users']:
select_sql = "SELECT * FROM " + account + " WHERE friend_name = ?"
cur.execute(select_sql,(user['screen_name'],))
row = cur.fetchone()
if row is None:
insert_sql = "INSERT INTO " + account + " (friend_name) VALUES (?)"
cur.execute(insert_sql,(user['screen_name'],))
print(user['screen_name'])
conn.commit()
def user_friends(account,num_friends,ctx):
print("Calling Twitter...")
if not num_friends.isnumeric():
num_friends = 20
url = 'https://api.twitter.com/1.1/friends/list.json'
formed_url = augment(url,{'screen_name': account,'count': num_friends})
try:
connection = urllib.request.urlopen(formed_url, context=ctx)
except:
print('Not a valid account')
quit()
data = connection.read().decode()
headers = dict(connection.getheaders())
print('Remaining:',headers['x-rate-limit-remaining'])
print('------------------------------------------------')
js = json.loads(data)
for user in js['users']:
print(user['screen_name'])
def user_status(account,num_stat,ctx):
print("Calling Twitter...")
if not num_stat.isnumeric():
num_stat = 50
url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
formed_url = augment(url,{'screen_name': account,'count': num_stat})
try:
connection = urllib.request.urlopen(formed_url, context=ctx)
except:
print('Not a valid account')
quit()
data = connection.read().decode()
headers = dict(connection.getheaders())
print('Remaining:',headers['x-rate-limit-remaining'])
print('------------------------------------------------')
js = json.loads(data)
for stat in js:
print("Date:",stat['created_at'])
print("Status:",stat['text'])
main()