-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
147 lines (136 loc) · 4.08 KB
/
models.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
import MySQLdb
import json
def get_conn():
host='127.0.0.1'
port=3306
db='csuspider'
user='spider'
password='xyz'
conn=MySQLdb.connect(
host=host,
user=user,
passwd=password,
db=db,
port=port,
charset='utf8'
)
return conn
def get_all_locations():
conn=get_conn()
cursor=conn.cursor()
sql="select location_id,title,longitude,latitude from location"
cursor.execute(sql)
rows=cursor.fetchall()
locations=[]
for row in rows:
dic={}
dic['location_id']=row[0]
dic['title']=row[1].encode('utf-8')
dic['longitude']=row[2]
dic['latitude']=row[3]
locations.append(dic)
conn.commit()
cursor.close()
conn.close()
return locations
def parse_info_dict(rows):
results=[]
for row in rows:
dic={}
dic['aca_id']=row[0]
dic['title']=row[1].encode('utf-8')
dic['url']=row[2].encode('utf-8')
dic['time']=row[3].encode('utf-8')
dic['location']=row[4].encode('utf-8')
dic['longitude']=row[5]
dic['latitude']=row[6]
dic['academy']=row[7].encode('utf-8')
dic['type']=row[8].encode('utf-8')
dic['html_content']=row[9].encode('utf-8')
dic['date_sort']=row[10].encode('utf-8')
results.append(dic)
return results
def get_all_infos():
conn=get_conn()
cursor=conn.cursor()
sql="""
SELECT a.aca_id,a.title,a.url,a.time,a.location,l.longitude,l.latitude,a.academy,a.type,a.html_content,a.date_sort
FROM academic a
INNER JOIN location l
ON a.location_id=l.location_id
ORDER BY date_sort DESC"""
cursor.execute(sql)
rows=cursor.fetchall()
results=parse_info_dict(rows)
conn.commit()
cursor.close()
conn.close()
return results
def search_from_string(string):
conn=get_conn()
cursor=conn.cursor()
sql="""
SELECT a.aca_id,a.title,a.url,a.time,a.location,l.longitude,l.latitude,a.academy,a.type,a.html_content,a.date_sort
FROM academic a
INNER JOIN location l
ON a.location_id=l.location_id
WHERE a.title REGEXP "%s" OR l.match_string REGEXP "%s" OR a.academy REGEXP "%s" OR a.html_content REGEXP "%s"
ORDER BY date_sort DESC"""%(string,string,string,string)
cursor.execute(sql)
rows=cursor.fetchall()
results=parse_info_dict(rows)
conn.commit()
cursor.close()
conn.close()
return results
def search_from_type(info_type):
conn=get_conn()
cursor=conn.cursor()
sql="""
SELECT a.aca_id,a.title,a.url,a.time,a.location,l.longitude,l.latitude,a.academy,a.type,a.html_content,a.date_sort
FROM academic a
INNER JOIN location l
ON a.location_id=l.location_id
WHERE a.type REGEXP "%s"
ORDER BY date_sort DESC"""%(info_type)
cursor.execute(sql)
rows=cursor.fetchall()
results=parse_info_dict(rows)
conn.commit()
cursor.close()
conn.close()
return results
def search_from_date(begin,end):
conn=get_conn()
cursor=conn.cursor()
sql="""
SELECT a.aca_id,a.title,a.url,a.time,a.location,l.longitude,l.latitude,a.academy,a.type,a.html_content,a.date_sort
FROM academic a
INNER JOIN location l
ON a.location_id=l.location_id
WHERE a.date_sort BETWEEN "%s" AND "%s"
ORDER BY date_sort DESC"""%(begin,end)
cursor.execute(sql)
rows=cursor.fetchall()
results=parse_info_dict(rows)
conn.commit()
cursor.close()
conn.close()
return results
def search_from_location_id(location_id):
conn=get_conn()
cursor=conn.cursor()
sql="""
SELECT a.aca_id,a.title,a.url,a.time,a.location,l.longitude,l.latitude,a.academy,a.type,a.html_content,a.date_sort
FROM academic a
INNER JOIN location l
ON a.location_id=l.location_id
WHERE a.location_id="%s"
ORDER BY date_sort DESC"""%(location_id)
cursor.execute(sql)
rows=cursor.fetchall()
results=parse_info_dict(rows)
conn.commit()
cursor.close()
conn.close()
return results