-
Notifications
You must be signed in to change notification settings - Fork 0
/
daily_sentiment.py
68 lines (52 loc) · 2.49 KB
/
daily_sentiment.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
56
57
58
59
60
61
62
63
64
65
66
67
68
from contextlib import closing
import mysql.connector as mysql
import re
from datetime import datetime
import argparse
import csv
def use_db(db, db_name):
with closing(db.cursor()) as cursor:
cursor.execute(f'use {db_name}')
def get_dates(cursor):
query = "select distinct date_format(datetime, '%Y-%m-%d') from tweetstime where year(datetime) = 2017 or year(datetime) = 2018 order by datetime;"
cursor.execute(query)
return [d[0] for d in cursor.fetchall()]
def days_between(d1, d2):
d1 = datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.strptime(d2, "%Y-%m-%d")
return abs((d2 - d1).days)
def volume_to_dates(cursor, d1, d2=None):
start_date = d1 + " 18:00:00"
end_date = (d2 if d2 else d1) + " " + ("17:59:59" if d2 else "11:59:59")
query = f"""select count(s.txt) as total, count(case when s.txt > 0 then 1 end) as positivos, count(case when s.txt < 0 then 1 end) as negativos, avg(s.txt) as media
from tweetstime as t
inner join sentiments as s
on t.id = s.id
where t.datetime >= '{start_date}' and t.datetime <= '{end_date}';"""
cursor.execute(query)
data = cursor.fetchone()
row = [f'{start_date} - {end_date}'] + [d for d in data]
return row
def main(host, user, password, db_name, terms_file):
with closing(mysql.connect(host=host, user=user, password=password)) as db:
use_db(db, db_name)
cursor = db.cursor()
dates = get_dates(cursor)
with open("sentimentos-periodo.csv", 'w', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(["periodo", "total", "positivos", "negativos", "media"])
for i, date in enumerate(dates):
next_date = dates[i+1] if i < len(dates) - 1 else None
print(date)
writer.writerow(volume_to_dates(cursor, date, next_date))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--host', help='Host name', type=str)
parser.add_argument('-u', '--user', help='Username', type=str)
parser.add_argument('-p', '--password', help='Password', type=str)
parser.add_argument('-d', '--database', help='Database name', type=str)
parser.add_argument('-t', '--terms', help='Terms file path', type=str)
args = parser.parse_args()
main(host=args.host, user=args.user,
password=args.password, db_name=args.database,
terms_file=args.terms)