-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
152 lines (108 loc) · 4.27 KB
/
plot.py
File metadata and controls
152 lines (108 loc) · 4.27 KB
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
132
133
134
135
136
137
138
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np
import urllib.request
import sqlite3
import glob
import time
import re
today = time.strftime('%Y%m%d', time.gmtime(time.time()))
frontpage = ['b3s23/C1', 'b3s23/C2_1', 'b3s23/C4_1', 'b3s23/D8_1', 'b3s12/C1', 'b38s23/C1', 'b3s2-i34q/C1', 'b367s2-i34q/C1']
con = sqlite3.connect('catagolue.db')
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS rules (id INTEGER PRIMARY KEY, rule TEXT UNIQUE)")
cur.execute("CREATE TABLE IF NOT EXISTS objects (rule INTEGER, date TEXT, objcount INTEGER)")
cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_objects ON objects(date, objcount)")
def scrape(rule):
#cur.execute("SELECT id,rule FROM rules;")
#for rule in cur.fetchall():
fd = urllib.request.urlopen("https://catagolue.appspot.com/textcensus/%s/objcount" % rule[1])
page = fd.read().decode('utf-8')
cmatch = re.search('Total objects: ([0-9]+)', page)
if cmatch:
objcount = cmatch.group(1)
cur.execute("INSERT INTO objects VALUES(?, ?, ?) ON CONFLICT(date, objcount) DO NOTHING", (rule[0], today, objcount))
con.commit()
def filetorule(rule):
rule = rule.replace('_', '/', 1)
rule = rule.replace('catagolue/', '')
rule = rule.replace('.dat', '')
return rule
def ruletofile(rule):
rule = rule.replace('/', '_')
rule = rule.replace('catagolue/', '')
return rule
def plot_graph(rule):
cur.execute("select id from rules where rule=?", (rule,))
rid = cur.fetchone()[0]
cur.execute("SELECT date,max(objcount) from objects WHERE rule = ? group by date order by date asc", (rid,))
results = cur.fetchall()
xdata = [datetime.strptime(x[0], "%Y%m%d") for x in results]
#plt.title(rule)
fig, ax = plt.subplots()
#plt.title(rule)
ax.set_title(rule)
ax.plot(xdata, [x[1] for x in results])
ax.set_title(rule)
#plt.title(rule)
days = mdates.AutoDateLocator(maxticks=10, interval_multiples=False)
fuckoff = mdates.DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_locator(days)
ax.xaxis.set_major_formatter(fuckoff)
fig.autofmt_xdate()
plt.ylabel('objcount')
plt.xlabel('date')
filename = "/var/www/html/stats/%s.png" % ruletofile(rule)
plt.savefig(filename, format='png')
plt.close(fig)
#plt.show()
def convert_oldstyle(file):
rule = filetorule(file)
cur.execute("insert into rules(rule) values(?) ON CONFLICT(rule) DO NOTHING", (rule,))
con.commit()
cur.execute("SELECT id from rules WHERE rule=?", (rule,))
ruleid = cur.fetchone()[0]
fd = open(file, 'r')
for line in fd.read().split('\n'):
ff = line.split(' ')
if ff != ['']:
cur.execute("INSERT INTO objects(rule, date, objcount) VALUES (?, ?, ?) ON CONFLICT(date, objcount) DO NOTHING", (ruleid, ff[0], ff[1]))
con.commit()
def build_html():
index = ''
others = ''
with open('header.html') as h:
index += h.read()
cur.execute("SELECT rule FROM rules;")
for rule in cur.fetchall():
filest = ruletofile(rule[0])
if rule[0] in frontpage:
index += '''
<div style='float:left; overflow: hidden; text-align:center;'>
<strong><a href="https://catagolue.appspot.com/census/%s" target="_blank">%s</a></strong> <br />
<a href="%s.png">
<img src="%s.png" width=300 height=200>
</a>
</div>
''' % (rule[0], rule[0], filest, filest)
else:
others += '''
<a href="%s.png">%s</a> <br />
''' % (filest, rule[0])
index += "<div style='clear:both; width:100%;float:none;'>\n"
index += "<br /> <br /><span>Other rules</span> <br /> \n"
index += others
index += "</div>\n"
with open('footer.html') as f:
index += f.read()
with open('/var/www/html/stats/index.html', 'w') as ind:
ind.write(index)
if __name__ == '__main__':
cur.execute("SELECT id,rule FROM rules")
for rule in cur.fetchall():
scrape(rule)
#for i in glob.glob("catagolue/*.dat"):
# convert_oldstyle(i)
plot_graph(rule[1])
build_html()