-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_digest.py
49 lines (42 loc) · 1.74 KB
/
email_digest.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
from time import time
from gallery.models import record, rating, user, user_lookup
import pysq.apiv2 as psq
import settings
from haversine import *
authenticator = psq.FSAuthenticator(settings.CLIENT_ID, settings.CLIENT_SECRET, settings.CALLBACK_URL)
def htmlemail(lat,lon, radius):
recipients=user.objects.filter(last_lat__lt=lat+5, last_lat__gt=lat-5, last_lon__lt=lon+5, last_lon__gt=lon-5)
recips={}
for recipient in recipients:
recips[recipient.fsq_id]=recipient.first_name, recipient.last_name, recipient.email
the_records = record.objects.filter(time__lt=time(),time__gt=time()-86400)
the_set={}
for item in the_records:
v=authenticator.userless_query("/venues/"+item.venue_id)
vlat=v['venue']['location']['lat']
vlon=v['venue']['location']['lng']
venue_name=v['venue']['name']
try:
venue_address=v['venue']['location']['address']+' '+v['venue']['location']['city']+', '+v['venue']['location']['state']+' '+v['venue']['location']['postalCode']
except:
pass
if item.venue_id in the_set.keys():
the_set[item.venue_id][2]+=item.target.quotient()
the_set[item.venue_id][3]+=1
elif haversine(float(vlon),float(vlat),float(lon),float(lat))<=int(radius):
the_set[item.venue_id]=[venue_name,venue_address,item.target.quotient(),1]
else:
pass
averages={}
for item in the_set.iteritems():
averages[item[0]]=[item[1][2]/item[1][3],{'female':[],'male':[]}]
for item in the_records:
try:
u=user_lookup.objects.get(pic_id=item.target.pic_id)
g=authenticator.query("users/"+u.fsq_id, token="1A0ESOQC4W2RIGY442CJTKKFJEM04BYCEHSG0SNCVIWMKPII")
averages[item.venue_id][1][g['user']['gender']].append(item.target.pic_id)
except:
pass
for item in the_set:
the_set[item].append(averages[item])
return the_set