-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
74 lines (57 loc) · 2.07 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
from flask_sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash,check_password_hash
import geocoder
import urllib2
import json
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))
def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
# p = Place()
# places = p.query("1600 Amphitheater Parkway Mountain View CA")
class Place(object):
def meters_to_walking_time(self, meters):
# 80 meters is one minute walking time
return int(meters / 80)
def wiki_path(self, slug):
return urllib2.urlparse.urljoin("http://en.wikipedia.org/wiki/", slug.replace(' ', '_'))
def address_to_latlng(self, address):
g = geocoder.google(address)
return (g.lat, g.lng)
def query(self, address):
lat, lng = self.address_to_latlng(address)
query_url = 'https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=5000&gscoord={0}%7C{1}&gslimit=20&format=json'.format(lat, lng)
g = urllib2.urlopen(query_url)
results = g.read()
g.close()
data = json.loads(results)
places = []
for place in data['query']['geosearch']:
name = place['title']
meters = place['dist']
lat = place['lat']
lng = place['lon']
wiki_url = self.wiki_path(name)
walking_time = self.meters_to_walking_time(meters)
d = {
'name': name,
'url': wiki_url,
'time': walking_time,
'lat': lat,
'lng': lng
}
places.append(d)
return places