-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
131 lines (92 loc) · 2.26 KB
/
data.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python
import MySQLdb
import datetime
import csv
# open connection to database
# fill up with needed credentials
db = MySQLdb.connect(host="localhost", user="root", passwd="",
db="ProductHunt")
# Executing queries
try:
cursor = db.cursor() # cursor to manipulate database
# sql query
cursor.execute("""
select * from Posts;
""")
#fetch | save the result
rows = cursor.fetchall()
# Mon, Tu, Wed, Th, Fri, Sat, Sun
weekdays_count = [ 0, 0, 0, 0, 0, 0, 0] #Posts
votes_count = [ 0, 0, 0, 0, 0, 0, 0]
comments_count = [ 0, 0, 0, 0, 0, 0, 0]
##########
#Processing data from database
##########
for row in rows:
# Days of Week
day_of_week = row[1].weekday()
if day_of_week == 0:
weekdays_count[0] += 1
votes_count[0] += row[6]
comments_count[0] += row[7]
elif day_of_week == 1:
weekdays_count[1] += 1
votes_count[1] += row[6]
comments_count[1] += row[7]
elif day_of_week == 2:
weekdays_count[2] += 1
votes_count[2] += row[6]
comments_count[2] += row[7]
elif day_of_week == 3:
weekdays_count[3] += 1
votes_count[3] += row[6]
comments_count[3] += row[7]
elif day_of_week == 4:
weekdays_count[4] += 1
votes_count[4] += row[6]
comments_count[4] += row[7]
elif day_of_week == 5:
weekdays_count[5] += 1
votes_count[5] += row[6]
comments_count[5] += row[7]
elif day_of_week == 6:
weekdays_count[6] += 1
votes_count[6] += row[6]
comments_count[6] += row[7]
##########
# Storing data in .csv file
##########
out = open('out.csv', 'w')
for i in range(-1,7):
if (i == -1):
out.write('Day;')
out.write('Posts;')
out.write('Votes;')
out.write('Comments')
out.write('\n')
else:
#if i == 0:
# out.write('Monday;')
#elif i == 1:
# out.write('Tuesday;')
#elif i == 2:
# out.write('Wednesday;')
#elif i == 3:
# out.write('Thursday;')
#elif i == 4:
# out.write('Friday;')
#elif i == 5:
# out.write('Saturday;')
#elif i == 6:
# out.write('Sunday;')
out.write('%d;' % i)
out.write('%d;' % weekdays_count[i])
out.write('%d;' % votes_count[i])
out.write('%d;' % comments_count[i])
out.write('\n')
out.close()
##########
##########
#close database
finally:
db.close()