forked from anthonydb/python-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-api-sqlite.py
55 lines (49 loc) · 1.69 KB
/
twitter-api-sqlite.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
import twitter
import sqlite3
import os
from datetime import datetime
# Set the Twitter API authentication
api = twitter.Api(consumer_key='your-key',
consumer_secret='your-secret-key',
access_token_key='your-access-token',
access_token_secret='your-token-secret')
# These are the accounts for which you will fetch data
# It's a list of lists, with a category added for each handle
handles_list = [
['chrisschnaars', 'journalist'],
['anthonydb', 'journalist'],
['usatoday', 'newsroom']
]
# Function to add row to accounts table
def insert_db(handle, category, followers, description):
conn = sqlite3.connect('social_data2.db')
cur = conn.cursor()
cur.execute('''
INSERT INTO twaccounts VALUES (?,?,?,?,?);
''', (datetime.now(), handle, category, followers, description))
conn.commit()
conn.close()
# Create the database if it doesn't exist
if not os.path.exists('social_data2.db'):
conn = sqlite3.connect('social_data2.db')
conn.close()
else:
pass
# Create the table if it's not in the db
conn = sqlite3.connect('social_data2.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS twaccounts
(FetchDate Date, Handle Text, Category Text, Followers Integer, Description Text)
''')
conn.commit()
conn.close()
#Iterate over handles and hit the API with each
for handle in handles_list:
print 'Fetching @' + handle[0]
try:
user = api.GetUser(screen_name=handle[0])
followers = user.GetFollowersCount()
description = user.GetDescription()
insert_db(handle[0], handle[1], followers, description)
except:
print '-- ' + handle[0] + ' not found'