├── README.md └── open311SNSL.py /README.md: -------------------------------------------------------------------------------- 1 |
3 | Open311 is a form of technology that provides open channels of communication for issues that concern public space and public services. Primarily, Open311 refers to a standardized protocol for location-based collaborative issue-tracking. Open311 provides access to Services, Facilities, and Frequently Asked Questions (FAQs). Open311 also provides the 311 Today feed which provides daily status messages regarding Public Schools, Alternate Side Parking, and Garbage/Recycling pick up. Important City announcements based on current events may also be provided through this feed. 4 |
5 | 6 |import open311SNSL
10 |
11 | If the library fails to import, make sure python is set to search the directory that the file is located in. Then restart python and try again.
12 |
13 |
14 | ServListObj = open311SNSL.servicelist('API ID' , 'API Key')
17 |
18 |
19 | Changing the API Key or ID for a servicelist object that was already created:
21 | ServListObj.setID('NewID')
22 | ServLisObj.setKey('NewKey')
23 |
24 |
25 |
26 | Getting the API Key or ID for a DOT object that was already created:
28 | ServListObj.getID()
29 | ServListObj.getKey()
30 |
31 |
32 |
33 | Using an already created object, search for "all" service lists and format as a JSON or XML
35 | serviceListOutput = ServListObj.getServiceList(ServListObj.s_all , ServListObj.json)
36 |
39 | serviceListOutputServListObj.getServiceList(ServListObj.s_all , ServListObj.xml)
40 |
41 |
42 | To output the result in text format:
44 | print serviceListOutput.text
45 |
46 |
47 |
48 |
49 |
52 | serviceObject = open311SNSL.service('API ID' , 'API Key' , 'service_id')
53 |
56 | serviceInfo = serviceObject.getService(serviceObject.xml)
57 |
60 | serviceInfo = serviceObject.getService(serviceObject.json)
61 |
65 | print serviceInfo.text
66 |
67 |
68 |
69 |
70 |
71 | For further documentation and examples, see the integrated help Doc in python:help(open311SNSL)
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/open311SNSL.py:
--------------------------------------------------------------------------------
1 | '''
2 | Open311 API Library
3 |
4 | '''
5 | import requests
6 |
7 | print ('In open311')
8 | class serviceList:
9 | def __init__(self,ID, Key):
10 |
11 | '''
12 | Creating Object:
13 |
14 | obj = Open311SNSL.serviceList('API ID ','API Key')
15 |
16 | obj = Open311SNSL.serviceList('12345','987654')
17 | '''
18 | self.id = ID
19 | self.key = Key
20 |
21 |
22 |
23 | #Content Type Constants
24 | self.json = 'json'
25 | self.xml = 'xml'
26 | #Service List Constants
27 | self.s_all = 'all'
28 | self.s_Business = 'Business'
29 | self.s_Civic_Services = 'Civic Services'
30 | self.s_Culture_and_Recreation = 'Culture and Recreation'
31 | self.s_Education = 'Education'
32 | self.s_Environment = 'Environment'
33 | self.s_Health = 'Health'
34 | self.s_Housing_and_Development = 'Housing and Development'
35 | self.s_Public_Safety = 'Public Safety'
36 | self.s_Social_Services = 'Social Services'
37 | self.s_Transportation = 'Transportation'
38 |
39 |
40 | def validation(self,statusCode, category):
41 | '''
42 | Validates whether the search was successful or not.
43 |
44 | '''
45 | if statusCode == 403:
46 | print ('Error Code:403 \n Make sure your APP ID/APP Key and Category are correct')
47 | print ('APP ID: ', self.id,' App Key: ',self.key , ' Category: ', category)
48 |
49 | elif statusCode == 0:
50 | print ('Error Code:0 \n Make sure your firewall isn\'t blocking any connections')
51 |
52 | elif statusCode == 200:
53 | return True
54 |
55 |
56 | def getID(self):
57 | '''
58 | (Check to make sure the ID entered for an object was correct)
59 |
60 | (Example)
61 |
62 | object.getID()
63 | ==> Returns the ID number for object
64 | '''
65 | print (self.id)
66 | return self.id
67 |
68 | def getKey(self):
69 | '''
70 | (Check to make sure the Key entered for an object was correct)
71 |
72 | (Example)
73 |
74 | object.getKey()
75 | ==> Returns the key for object
76 | '''
77 | print (self.key)
78 | return self.key
79 |
80 | def setID(self, newID):
81 | '''
82 | (Set a new ID for an object)
83 |
84 | (Example)
85 |
86 | object.setID(12345)
87 | ==> Assigns a new ID number(12345) to the object
88 | '''
89 | self.id = newID
90 | print (self.id)
91 |
92 | def setKey(self,newKey):
93 | '''
94 | (Set a new key for an object)
95 |
96 | (Example)
97 |
98 | object.setKey(56789)
99 | ==> Assigns a new key number(56789) to the object
100 | '''
101 | self.key = newKey
102 | print (self.key)
103 |
104 |
105 |
106 | def getServiceList(self,serviceFilter , contentType):
107 | '''
108 | (Call to search for a specific service/list)
109 |
110 | (Examples)
111 |
112 | OBJECT.getServiceList(OBJECT.s_all, OBJECT.json)
113 | ==> Searches for all services in the service list : Output JSON
114 |
115 | object.getServiceList(object.s_web , OBJECT.xml)
116 | ==> Searches for "web" in the service list : Output XML
117 |
118 |
119 | (Service List Filters)
120 |
121 | self.s_all
122 | ==> Search 'all'
123 | self.s_Environment_Sanitation
124 | ==> Search 'Environment and Sanitation'
125 | self.s_Property_Building_Home
126 | ==> Search 'Property, Buildings and Homes'
127 | self.s_Education_Employment
128 | ==> Search 'Education and Employment'
129 | self.s_Business_Finance
130 | ==> Search 'Business and Finance'
131 | self.s_Social_Services
132 | ==> Search 'Social Services'
133 | self.s_Health_Medicine
134 | ==> Search 'Health and Medicine'
135 | self.s_PubSafe_Law
136 | ==> Search 'Public Safety and Law'
137 | self.s_Gov_Civ_Services
138 | ==> 'Search Government and Civil Services'
139 |
140 |
141 | self.s_Business
142 | ==> Search 'Business'
143 | self.s_Civic_Services
144 | ==> Search 'Civic Services'
145 | self.s_Culture_and_Recreation
146 | ==> Search 'Culture and Recreation'
147 | self.s_Education
148 | ==> Search 'Education'
149 | self.s_Environment
150 | ==> Search 'Environment'
151 | self.s_Health
152 | ==> Search 'Health'
153 | self.s_Housing_and_Development
154 | ==> Search 'Housing and Development'
155 | self.s_Public_Safety
156 | ==> Search 'Public Safety'
157 | self.s_Social_Services
158 | ==> Search 'Social Services'
159 | self.s_Transportation
160 | ==> Search 'Transportation'
161 | '''
162 | if contentType == self.json:
163 | self.r = requests.get("https://api.cityofnewyork.us/311/v1/services/"+
164 | serviceFilter + '.json'
165 | "?app_id="+self.id+
166 | "&app_key="+self.key)
167 |
168 | elif contentType == self.xml:
169 | self.r = requests.get("https://api.cityofnewyork.us/311/v1/services/"+
170 | serviceFilter + '.xml'
171 | "?app_id="+self.id+
172 | "&app_key="+self.key)
173 |
174 | self.sCode = self.r.status_code
175 | print (self.sCode)
176 | self.validator = self.validation(self.sCode , serviceFilter)
177 | if self.validator == True:
178 | return self.r
179 |
180 | class service:
181 | def __init__(self, ID, Key, service_id):
182 | '''
183 | (Creating Service Object)
184 |
185 | obj = Open311SNSL.service('API ID ','API Key','serv_id')
186 |
187 | obj = Open311SNSL.service('12345','987654','20-4-7')
188 | '''
189 | self.id = ID
190 | self.key = Key
191 | self.servID = service_id
192 |
193 | #Content Type Constants
194 | self.json = 'json'
195 | self.xml = 'xml'
196 |
197 | def getService(self,contentType):
198 | '''
199 | (Search service based on service ID)
200 |
201 | (Examples)
202 |
203 | object.getService(object.json)
204 | ==> Searches for service based on ID : Output JSON
205 | object.getService(object.xml)
206 | ==> Searches for service based on ID : Output XML
207 |
208 | '''
209 |
210 | if contentType == self.json:
211 | self.r = requests.get("https://api.cityofnewyork.us/311/v1/services/"+
212 | self.servID + '.json'
213 | "?app_id="+self.id+
214 | "&app_key="+self.key)
215 |
216 | elif contentType == self.xml:
217 | self.r = requests.get("https://api.cityofnewyork.us/311/v1/services/"+
218 | self.servID + '.xml'
219 | "?app_id="+self.id+
220 | "&app_key="+self.key)
221 |
222 | self.sCode = self.r.status_code
223 | print (self.sCode)
224 | self.validator = self.validation(self.sCode)
225 | if self.validator == True:
226 | return self.r
227 |
228 | def validation(self,statusCode):
229 | '''
230 | Validates whether the search was successful or not.
231 |
232 | '''
233 | if statusCode == 403:
234 | print ('Error Code:403 \n Make sure your APP ID/APP Key and Category are correct')
235 | print ('APP ID: ', self.id,' App Key: ',self.key , 'Service ID',self.servID)
236 |
237 | elif statusCode == 0:
238 | print ('Error Code:0 \n Make sure your firewall isn\'t blocking any connections')
239 |
240 | elif statusCode == 200:
241 | return True
242 |
243 |
--------------------------------------------------------------------------------