-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.py
173 lines (143 loc) · 5.9 KB
/
lib.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf8 -*-
import codecs
import hashlib
import json
import os
import re
import shutil
from bs4 import BeautifulSoup
import dataset
import config
def recreate_website():
orig = config.base_folder
dest = config.www_folder
shutil.copy2(os.path.join(orig,"jumbotron-narrow.css"), os.path.join(dest,"jumbotron-narrow.css"))
shutil.copy2(os.path.join(orig,"config.py"), os.path.join(dest,"config.py"))
shutil.copy2(os.path.join(orig,"lib.py"), os.path.join(dest,"lib.py"))
shutil.copy2(os.path.join(orig,"visitas.db"), os.path.join(dest,"visitas.db"))
shutil.copy2(os.path.join(orig,"bootstrap.min.css"), os.path.join(dest,"bootstrap.min.css"))
shutil.copy2(os.path.join(orig,"highlighter.js"), os.path.join(dest,"highlighter.js"))
shutil.copy2(os.path.join(orig,"index.html"), os.path.join(dest,"index.html"))
shutil.copy2(os.path.join(orig,"search.py"), os.path.join(dest,"search.py"))
shutil.copy2(os.path.join(orig,"base.html"), os.path.join(dest,"base.html"))
shutil.copy2(os.path.join(orig,"jquery-1.10.2.min.js"), os.path.join(dest,"jquery-1.10.2.min.js"))
shutil.copy2(os.path.join(orig,"bootstrap.min.js"), os.path.join(dest,"bootstrap.min.js"))
def html_to_json(html):
###
# taken from http://stackoverflow.com/a/14167916
soup = BeautifulSoup(html)
table = soup.find('table', attrs={'class': 'items'})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr'):
rows.append([val.text for val in row.find_all('td')])
###
filename = os.path.join(config.base_folder, "output.json")
f = codecs.open(filename, "a", "utf8")
for i in rows:
if len(i) > 1:
out = dict()
out['date'] = i[0].strip()
out['visitor'] = i[1].strip()
out['id_document'] = i[2].strip()
out['entity'] = i[3].strip()
out['objective'] = i[4].strip()
out['host'] = i[5].strip()
out['office'] = i[6].strip()
out['meeting_place'] = i[7].strip()
out['time_start'] = i[8].strip()
try:
out['time_end'] = i[9].strip()
except:
out['time_end'] = ""
f.write(json.dumps(out) + "\n")
f.close()
def get_data():
# get data from json file
filename = os.path.join(config.base_folder, "visitas.db")
db = dataset.connect("sqlite:///" + filename)
table = db['visitas']
filename = os.path.join(config.base_folder, "output.json")
f = codecs.open(filename, "r", "utf8")
data = f.readlines()
f.close()
items = []
for line in data:
line = line.strip()
item = json.loads(line)
if 'id_document' in item:
id_doc_number = re.search("([0-9]+)", item['id_document'])
if id_doc_number:
id_doc_number = id_doc_number.groups()[0]
else:
id_doc_number = ""
else:
id_doc_number = ""
string = str(item['date']) + str(id_doc_number) + str(item['time_start'])
m = hashlib.sha1()
m.update(string)
item['sha512'] = m.hexdigest()
items.append(item)
return items
def prettify(item):
out = "<tr>"
out += "<td><a href='search?q=" + item['date'] + "'>" + item['date'] + "</a></td>"
out += "<td><a href='search?q=" + item['visitor'] + "'>" + item['visitor'] + "</a></td>"
out += "<td><a href='search?q=" + item['id_document'] + "'>" + item['id_document'] + "</a></td>"
out += "<td><a href='search?q=" + item['entity'] + "'>" + item['entity'] +"</a></td>"
out += "<td><a href='search?q=" + item['objective'] + "'>" + item['objective'] + "</a></td>"
out += "<td><a href='search?q=" + item['host'] + "'>" + item['host'] + "</a></td>"
out += "<td><a href='search?q=" + item['office'] + "'>" + item['office'] +"</a></td>"
out += "<td><a href='search?q=" + item['meeting_place'] + "'>" + item['meeting_place'] + "</a></td>"
out += "<td>" + item['time_start'] + "</td>"
out += "<td>" + item['time_end'] + "</td>"
out += "</tr>\n"
return out
def insert_to_db(line):
filename = os.path.join(config.base_folder, "visitas.db")
db = dataset.connect("sqlite:///" + filename)
table = db['visitas']
# line is a line of downloaded data
i = line.split(",")
item = dict()
item['date'] = i[0]
item['visitor'] = i[1]
item['id_document'] = i[2]
item['entity'] = i[3]
item['objective'] = i[4]
item['host'] = i[5]
item['office'] = i[6]
item['meeting_place'] = i[7]
item['time_start'] = i[8]
item['time_end'] = i[9]
string = str(item['date']) + str(item['id_document']) + str(item['time_start'])
m = hashlib.sha512()
m.update(string)
item['sha512'] = m.hexdigest()
print "uploading %s" % str(item['date']) + "_" + str(item['time_start'])
table.insert(item)
def create_database():
import sqlalchemy
print "Creating database"
filename = os.path.join(config.base_folder, "visitas.db")
if not os.path.isfile(filename):
try:
db = dataset.connect('sqlite:///' + filename)
table = db.create_table("visitas")
table.create_column('sha512', sqlalchemy.String)
table.create_column('date', sqlalchemy.String)
table.create_column('visitor', sqlalchemy.Text)
table.create_column('id_document', sqlalchemy.String)
table.create_column('entity', sqlalchemy.Text)
table.create_column('objective', sqlalchemy.Text)
table.create_column('host', sqlalchemy.Text)
table.create_column('office', sqlalchemy.Text)
table.create_column('meeting_place', sqlalchemy.Text)
table.create_column('time_start', sqlalchemy.Time)
table.create_column('time_end', sqlalchemy.Time)
except:
pass
def main():
print ""
if __name__ == "__main__":
main()