-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_stream.py
55 lines (37 loc) · 1.03 KB
/
insert_stream.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
import sqlite3
import sys
import json
import os
from os import rename
import time
from helper import *
db=sys.argv[1]
if not os.path.exists(db):
os.makedirs('./'+db)
rename('./'+db+'/stream_'+db+'.json', './'+db+'/ready_to_insert.json')
print('File renamed, sleeping one minute to allow for streamer to switch')
print("\n")
##sleep for the time the streamer switches to a new json file
time.sleep(60)
print('Ready to insert database')
print("\n")
conn = sqlite3.connect('./'+db+'/'+db+'.db')
c = conn.cursor()
print('Creating tables if not exist')
print("\n")
create_tables(c,conn)
create_tweet_tables(c,conn)
print('Inserting tweet objects')
print("\n")
tweets = []
jsonfile = open('./'+db+'/ready_to_insert.json', 'r').read().split('\n')
for f in jsonfile[1:-1]:
file = open('./'+db+'/temp.json', 'w')
file.write(f)
file.close()
file = open('./'+db+'/temp.json', 'r')
tweet=json.load(file, strict=False)
tweets.append(tweet)
##get rid of non tweets
tweets=[i for i in tweets if 'created_at' in i]
insertTweets(conn,c, tweets)