-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadb.py
162 lines (133 loc) · 6.29 KB
/
datadb.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
from kivy.logger import Logger
from urlparse import urlparse
import urllib
import requests
import json
#References
#Request maker
#http://requestmaker.com/
#documentation
#http://www.mytransport.sg/content/dam/mytransport/DataMall_StaticData/LTA_DataMall_API_User_Guide.pdf
#LTA Datamall API key
#1DxxvzbHwydZ3uw6UKNA9w==
#GUID
#687ef57c-810a-414a-b0f8-df4b78bf9ef6
#Fb id example:884587561622028
#BUS ARRIVAL API
#API URL: http://datamall2.mytransport.sg/ltaodataservice/BusArrival
_user='DREAMFACTORY USERNAME HERE'
_pass='DREAMFACTORY PASSWORD HERE'
class GetDBInfo(object):
def requestInfo(self,tables):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
response = requests.get('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/{}'.format(str(tables)),
auth=(_user, _pass),headers=headers, verify=False)
data = response.json()
return response
#Request record using single identifier
#getRecord(). Only when identifier is a unique key
def requestRecordByIdentifier(self,tables,identifier):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
response = requests.get('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/{}/{}'.format(str(tables),str(identifier)),
auth=(_user, _pass),headers=headers, verify=False)
data = response.json()
#print data
Logger.info('requestRecordByIdentifier: {}'.format(response))
return response
#Request list (one or more records) using identifier
#Works when identifier is not unique
def requestSavedBusRecord(self, _id_field):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
url = 'https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2'
path = '/SavedBuses?'
params = {'filter': str(_id_field)}
target = urlparse(url+path+urllib.urlencode(params))
response = requests.get(target.geturl(),
auth=(_user, _pass),headers=headers, verify=False)
#print data
Logger.info('requestRecordByIdentifier: {}'.format(response))
data = response.json()
return data
def getUser(self, facebook_ID):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
url = 'https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2'
path = '/users?'
params = {'ids': str(facebook_ID)}
target = urlparse(url+path+urllib.urlencode(params))
response = requests.get(target.geturl(),
auth=(_user, _pass),headers=headers, verify=False)
Logger.info('getUser {}'.format(response.text))
class PostDBInfo(object):
def createBusTableRecords(self, bus_no):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
payload = {"record": [
{"bus_no": '{}'.format(bus_no)}
]
}
response = requests.post('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/buses',
auth=(_user, _pass),headers=headers, data=json.dumps(payload),verify=False)
print response.text
return response
def createUserTableRecords(self, facebook_ID, username, firstname, lastname):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
payload = {"record": [
{"facebook_ID": str(facebook_ID),
"username": str(username),
"firstname": str(firstname),
"lastname": str(lastname)
}
]
}
response = requests.post('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/users',
auth=(_user, _pass), headers=headers, data=json.dumps(payload),verify=False)
print response.text
return response
def createUserSaveBusRecords(self, busno, busstopNo, users_facebook_ID):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
payload = {"record": [
{"busno": str(busno),
"busstopNo": str(busstopNo),
"Users_facebook_ID": str(users_facebook_ID)
}
]
}
response = requests.post('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/SavedBuses',
auth=(_user, _pass), headers=headers, data=json.dumps(payload),verify=False)
print response.text
return response
class DeleteDBInfo(object):
def deleteTableRecords(self):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
payload = {"record": [
{"bus_no": '999'}
]
}
response = requests.delete('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/'+str(tables),
auth=(_user, _pass), headers=headers, data=json.dumps(payload),verify=False)
print response.text
return response
#User unticks a bus to delete bus
def deleteUserSavebusRecords(self, busno, busstopNo, users_facebook_ID):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
payload = {"record": [
{"busno": str(busno),
"busstopNo": str(busstopNo),
"Users_facebook_ID": str(users_facebook_ID)
}
]
}
response = requests.delete('https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2/SavedBuses',
auth=(_user, _pass), headers=headers, data=json.dumps(payload),verify=False)
print response.text
Logger.info('deleteUserSavebusRecord {}'.format(response.text))
return response
def deleteAllUserRecords(self, users_facebook_ID):
headers = {'X-DreamFactory-Application-Name' : 'nextbus2'}
url = 'https://dsp-samjhyip.cloud.dreamfactory.com/rest/nextbus2'
path = '/SavedBuses?'
params = {'filter': str(users_facebook_ID)}
target = urlparse(url+path+urllib.urlencode(params))
response = requests.delete(target.geturl(),
auth=(_user, _pass),headers=headers, verify=False)
Logger.info('deleteUserSavebusRecord {}'.format(response.text))
return response