application: rzdzstan1 version: 1 runtime: python27 threadsafe: false api_version: 1 handlers: - url: /favicon.ico static_files: favicon.ico upload: favicon.ico - url: /.* script: web.py libraries: - name: webapp2 version: "latest"
import webapp2 application = webapp2.WSGIApplication([ ('/', MainPage), ('/trains', TrainListPage), ('/suggester', SuggesterPage), ], debug=True) def main(): application.run() if __name__ == "__main__": main()
def getCityId(city, s): req = 'http://pass.rzd.ru/suggester?lang=ru&stationNamePart=' + urllib.quote(city.encode('utf-8')) city = city.lower() respData = getResponse(req) rJson = json.loads(respData) for item in rJson: if item['name'].lower() == city: s.response.out.write(u': '+item['name']+' -> '+str(item['id'])+'<br>') return str(item['id']) s.response.out.write(u' : '+city+'<br>') s.response.out.write(u' , :<a href="../"></a><br>') for item in rJson: s.response.out.write(item['name']+'<br>') return None
def getResponse(url): good = False while not good: try: resp = opener.open(url, timeout=5) if resp.getcode() in [httplib.OK, httplib.CREATED, httplib.ACCEPTED]: good = True except (urllib2.HTTPError, HTTPException): pass return resp.read() def getResponseStub(url): r = json.loads(getResponse(url)) cnt = 0 while (r['result']!='OK' and cnt < 5): sleep(1) cnt+=1 r = json.loads(getResponse(url)) return r def getFinalRequest(): req1 = 'http://pass.rzd.ru/timetable/public/ru?STRUCTURE_ID=735&layer_id=5371&dir=0&tfl=3&checkSeats=1&\ st0='+st0+'&code0='+id0+'&dt0='+date+'&st1='+st1+'&code1='+id1+'&dt1='+date r = json.loads(getResponse(req1)) if (r['result']=='OK'): s.response.out.write(r['tp'][0]['msgList'][0]['message']) #errType s.response.out.write('<br>') return sid = str(r['SESSION_ID']) rid = str(r['rid']) req2 = 'http://pass.rzd.ru/timetable/public/ru?STRUCTURE_ID=735&layer_id=5371&dir=0&tfl=3&checkSeats=1&\ st0='+st0+'&code0='+id0+'&dt0='+date+'&st1='+st1+'&code1='+id1+'&dt1='+date+'&rid='+rid+'&SESSION_ID='+sid r = getResponseStub(req2)
Source: https://habr.com/ru/post/210420/
All Articles