Commit c32948c8 authored by Francesco Poldi's avatar Francesco Poldi

Added searches table

parent f47d011e
...@@ -15,6 +15,8 @@ import re ...@@ -15,6 +15,8 @@ import re
import sys import sys
import sqlite3 import sqlite3
user_list = {}
## clean some output ## clean some output
class RecycleObject(object): class RecycleObject(object):
def write(self, junk): pass def write(self, junk): pass
...@@ -52,12 +54,24 @@ def initdb(db): ...@@ -52,12 +54,24 @@ def initdb(db):
table_users = """ table_users = """
CREATE TABLE IF NOT EXISTS CREATE TABLE IF NOT EXISTS
users ( users (
user text primary key, user text,
date_update text not null, date_update text not null,
num_tweets integer num_tweets integer,
PRIMARY KEY (user, date_update)
); );
""" """
cursor.execute(table_users) cursor.execute(table_users)
table_search = """
CREATE TABLE IF NOT EXISTS
searches (
user text,
date_update text not null,
num_tweets integer,
search_keyword text,
PRIMARY KEY (user, date_update, search_keyword)
);
"""
cursor.execute(table_search)
return conn return conn
except Exception as e: except Exception as e:
return str(e) return str(e)
...@@ -218,6 +232,11 @@ async def outTweet(tweet): ...@@ -218,6 +232,11 @@ async def outTweet(tweet):
entry = (tweetid, date, time, timezone, username, text, replies, likes, retweets, hashtags,) entry = (tweetid, date, time, timezone, username, text, replies, likes, retweets, hashtags,)
cursor.execute('INSERT INTO tweets VALUES(?,?,?,?,?,?,?,?,?,?)', entry) cursor.execute('INSERT INTO tweets VALUES(?,?,?,?,?,?,?,?,?,?)', entry)
conn.commit() conn.commit()
if username in list(user_list):
old_tot = user_list[list(user_list)[list(user_list).index(username)]]
user_list.update({username: old_tot + 1})
else:
user_list.update({username: 1})
except sqlite3.IntegrityError: # this happens if the tweet is already in the db except sqlite3.IntegrityError: # this happens if the tweet is already in the db
return "" return ""
...@@ -395,7 +414,7 @@ async def main(): ...@@ -395,7 +414,7 @@ async def main():
''' '''
if arg.elasticsearch: if arg.elasticsearch:
print("Indexing to Elasticsearch @" + str(arg.elasticsearch)) print("Indexing to Elasticsearch @ " + str(arg.elasticsearch))
if arg.database: if arg.database:
print("Inserting into Database: " + str(arg.database)) print("Inserting into Database: " + str(arg.database))
...@@ -427,10 +446,18 @@ async def main(): ...@@ -427,10 +446,18 @@ async def main():
break break
if arg.database: if arg.database:
now = str(datetime.datetime.now())
cursor = conn.cursor() cursor = conn.cursor()
entry = (str(arg.u), str(datetime.datetime.now()), num,) if arg.s:
cursor.execute('INSERT OR REPLACE INTO users VALUES(?,?,?)', entry) for user in list(user_list):
conn.commit() tot = user_list[list(user_list)[list(user_list).index(user)]]
entry = (user, now, tot, str(arg.s),)
cursor.execute('INSERT OR REPLACE INTO searches VALUES(?,?,?,?)', entry)
conn.commit()
else:
entry = (str(arg.u), now, num,)
cursor.execute('INSERT OR REPLACE INTO users VALUES(?,?,?)', entry)
conn.commit()
conn.close() conn.close()
if arg.count: if arg.count:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment