-
Notifications
You must be signed in to change notification settings - Fork 4
/
save_to_db.py
40 lines (33 loc) · 1.19 KB
/
save_to_db.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
import MySQLdb
# import elasticsearch
db = MySQLdb.connect(host="",
user="",
passwd="",
db="",
charset="utf8")
def save_to_db(author, text, url, id_str):
cur = db.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Tweets(Id INT PRIMARY KEY AUTO_INCREMENT, \
Author VARCHAR(255), \
Text VARCHAR(255), \
Url VARCHAR(255), \
Tweet_Id VARCHAR(255), \
Screenshot INT, \
Deleted INT)")
try:
cur.execute("""INSERT INTO Tweets(Author, Text, Url, Tweet_Id, Screenshot, Deleted)
VALUES (%s, %s, %s, %s, %s, %s)""",
(author, text, url, id_str, 0, 0))
db.commit()
print "Wrote to database:", author, id_str
except MySQLdb.Error, e:
print "Error", e.args[0], e.args[1]
db.rollback()
print "ERROR writing database"
# es = elasticsearch.Elasticsearch()
# es.index(index="tweets", doc_type="tweet", id=id, body={
# "Author": author,
# "Text": text.encode('utf-8'),
# "Url": url,
# "Tweet_Id": id_str
# })