· 6 years ago · Oct 18, 2018, 03:26 AM
1aprsfsq.py 0000775 0001750 0001750 00000005227 13354101034 012124 0 ustar bryan bryan import json, requests
2import re
3
4class foursquare:
5 def __init__(self, oauth, lat, lon, shout, lastVenue, lastVenueName):
6 self.oauth = oauth
7 self.lat = lat
8 self.lon = lon
9 self.shout = shout
10 self.lastVenue = lastVenue
11 self.lastVenueName = lastVenueName
12
13 def foursquareCheckin(self):
14 regex = re.compile('[^a-zA-Z0-9_ ]')
15
16 oldLocation = self.lastVenue
17 oldLocationName = self.lastVenueName
18 lookupURL = 'https://api.foursquare.com/v2/venues/search'
19 checkinURL = 'https://api.foursquare.com/v2/checkins/add'
20
21 lookupParams = dict(
22 oauth_token=self.oauth,
23 ll=self.lat + "," + self.lon,
24 limit=1,
25 v=20180801
26 )
27
28 lookup = requests.get(url=lookupURL, params=lookupParams)
29 lookupData = json.loads(lookup.text)
30
31 venuID = lookupData["response"]["venues"][0]["id"]
32 lookupCity = lookupData["response"]["venues"][0]["location"]["city"]
33 lookupState = lookupData["response"]["venues"][0]["location"]["state"]
34 lookupCountry = lookupData["response"]["venues"][0]["location"]["cc"]
35 lookupVenue = regex.sub('', lookupData["response"]["venues"][0]["name"])
36
37 if (oldLocation != venuID) and (oldLocationName != lookupVenue):
38 print "Checking in at new venue"
39 checkinParams = dict(
40 ll=self.lat + "," + self.lon,
41 limit=1,
42 venueId=venuID,
43 shout=self.shout,
44 oauth_token = self.oauth,
45 v=20180801
46 )
47
48 checkin = requests.post(url=checkinURL, params=checkinParams)
49 checkinData = json.loads(checkin.text)
50
51 with open('settings.py', 'r+') as variableFile:
52 content = variableFile.read()
53 variableFile.seek(0)
54 variableFile.truncate()
55 variableFile.write(content.replace("lastVenue='" + oldLocation + "\'", "lastVenue=\'" + venuID + "\'"))
56 with open('settings.py', 'r+') as variableFile:
57 content = variableFile.read()
58 variableFile.seek(0)
59 variableFile.truncate()
60 variableFile.write(content.replace("lastVenueName='" + oldLocationName + "\'", "lastVenueName=\'" + lookupVenue + "\'"))
61
62 else:
63 print "Still at same venue. No need to checkin."
64
65 foursquareCheckin = {
66 "fsqCity":lookupCity,
67 "fsqState":lookupState,
68 "fsqCountry":lookupCountry,
69 "fsqVenue":lookupVenue
70 }
71 return foursquareCheckin
72