2015-04-13 20:58:32 +00:00
|
|
|
#!/usr/bin/env python3
|
2015-03-09 17:32:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-04-13 20:58:32 +00:00
|
|
|
import html.parser, sqlite3, time, tweepy, os
|
2015-03-09 17:32:24 +00:00
|
|
|
|
|
|
|
user = "Username"
|
|
|
|
cke = "Consumer Key"
|
|
|
|
cse = "Consumer Secret"
|
|
|
|
ato = "Access Token"
|
|
|
|
ase = "Access Secret"
|
|
|
|
|
|
|
|
search = "from:" + user
|
|
|
|
|
|
|
|
database_filename = "Database.db"
|
|
|
|
|
|
|
|
sql_conn = sqlite3.connect(database_filename)
|
|
|
|
cur = sql_conn.cursor()
|
|
|
|
|
2015-04-12 23:58:32 +00:00
|
|
|
try:
|
|
|
|
cur.execute("SELECT max(tweet_id) FROM tweets")
|
2015-04-13 20:58:32 +00:00
|
|
|
except:
|
|
|
|
print("Please create a database using ./makedb.py or ./csvdb.py before trying to populate it.")
|
2015-04-12 23:58:32 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
savepoint = int(cur.fetchone()[0])
|
|
|
|
except:
|
|
|
|
savepoint = 0
|
2015-03-09 17:32:24 +00:00
|
|
|
|
|
|
|
auth = tweepy.OAuthHandler(cke, cse)
|
|
|
|
auth.set_access_token(ato, ase)
|
|
|
|
api = tweepy.API(auth)
|
|
|
|
|
2015-04-13 20:58:32 +00:00
|
|
|
timelineIterator = list(tweepy.Cursor(api.search, q=search, since_id=savepoint).items())
|
2015-03-09 17:32:24 +00:00
|
|
|
|
|
|
|
timeline = []
|
|
|
|
|
|
|
|
for status in timelineIterator:
|
|
|
|
timeline.append(status)
|
|
|
|
|
|
|
|
timeline.reverse()
|
|
|
|
|
|
|
|
tw_counter = 0
|
|
|
|
|
|
|
|
for status in timeline:
|
2015-04-13 20:58:32 +00:00
|
|
|
print("(%(date)s) %(name)s: %(message)s\n" % \
|
2015-03-09 17:32:24 +00:00
|
|
|
{ "date" : status.created_at,
|
|
|
|
"name" : status.author.screen_name.encode('utf-8'),
|
2015-04-13 20:58:32 +00:00
|
|
|
"message" : status.text.encode('utf-8') })
|
2015-03-09 17:32:24 +00:00
|
|
|
|
|
|
|
timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000"
|
2015-04-13 20:58:32 +00:00
|
|
|
text = html.parser.HTMLParser().unescape(status.text).replace("'", "''")
|
2015-03-09 17:32:24 +00:00
|
|
|
|
|
|
|
cur.execute("INSERT INTO tweets('tweet_id','timestamp','text') VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')")
|
|
|
|
tw_counter = tw_counter + 1
|
|
|
|
|
|
|
|
sql_conn.commit()
|
|
|
|
sql_conn.close()
|
|
|
|
|
2015-04-13 20:58:32 +00:00
|
|
|
print("Finished. %d Tweets stored" % (tw_counter))
|