Use requests module instead of urllib. - lefrecce - Retrieve information about next trains and stations via lefrecce.it
(HTM) hg clone https://bitbucket.org/iamleot/lefrecce
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) changeset 4e3b85bec737283570628a0344059dd1a2f693bd
(DIR) parent beb3201986f4ae506bf6fed02edc1bbc71e6bbd8
(HTM) Author: Leonardo Taccari <iamleot@gmail.com>
Date: Sun, 13 May 2018 15:42:03
Use requests module instead of urllib.
Unfortunately solutions() if verbose=True was passed was broken
because probably now also cookies should be passed to get "details"
about the single solution. Give up in using urllib and just use
requests via a single session.
Apart an extra dependency, now the kludge for verbose=True is no more needed!
Diffstat:
lefrecce.py | 61 ++++++++++++++++++++++---------------------------------------
1 files changed, 22 insertions(+), 39 deletions(-)
---
diff -r beb3201986f4 -r 4e3b85bec737 lefrecce.py
--- a/lefrecce.py Sun May 13 14:49:39 2018 +0200
+++ b/lefrecce.py Sun May 13 15:42:03 2018 +0200
@@ -38,7 +38,8 @@
from datetime import datetime
import json
-from urllib import parse, request
+
+import requests
def print_solution(solution):
@@ -59,19 +60,17 @@
def solutions(origin, destination, adate, atime, verbose=True):
url = 'https://www.lefrecce.it/msite/api/solutions?' \
- + 'origin=' + parse.quote(origin) + '&' \
- + 'destination=' + parse.quote(destination) + '&' \
+ + 'origin=' + origin + '&' \
+ + 'destination=' + destination + '&' \
+ 'arflag=A' + '&' \
- + 'adate=' + parse.quote(adate) + '&' \
- + 'atime=' + parse.quote(atime) + '&' \
+ + 'adate=' + adate + '&' \
+ + 'atime=' + atime + '&' \
+ 'adultno=1&childno=0&direction=A&frecce=false&onlyRegional=false'
- res = ''
- req = request.Request(url, headers={'Accept-Language': 'en-US'})
- with request.urlopen(req) as r:
- for l in r:
- res += l.decode()
- j = json.loads(res)
+ sess = requests.Session()
+ sess.headers['Accept-Language'] = 'en-US'
+ r = sess.get(url)
+ j = json.loads(r.text)
for solution in j:
s = {}
@@ -83,21 +82,9 @@
s['trains'] = []
if verbose:
- trains_url = 'https://www.lefrecce.it/msite/api/solutions/' + parse.quote(solution['idsolution']) + '/info'
- res = ''
- req = request.Request(trains_url, headers={'Accept-Language': 'en-US'})
- # XXX: Often the request trains information regarding the current
- # XXX: solution just return an empty string... Recheck until we have
- # XXX: something useful...
- while res == '':
- try:
- r = request.urlopen(req)
- for l in r:
- res += l.decode()
- except:
- pass
-
- trains = json.loads(res)
+ trains_url = 'https://www.lefrecce.it/msite/api/solutions/' + solution['idsolution'] + '/info'
+ r = sess.get(trains_url)
+ trains = json.loads(r.text)
for train in trains:
stoplist = []
@@ -129,14 +116,12 @@
def search_stations(name):
url = 'https://www.lefrecce.it/msite/api/geolocations/locations?name=' + \
- parse.quote(name)
+ name
- res = ''
- req = request.Request(url, headers={'Accept-Language': 'en-US'})
- with request.urlopen(req) as r:
- for l in r:
- res += l.decode()
- j = json.loads(res)
+ sess = requests.Session()
+ sess.headers['Accept-Language'] = 'en-US'
+ r = sess.get(url)
+ j = json.loads(r.text)
stations = []
for station in j:
@@ -148,12 +133,10 @@
def list_stations():
url = 'http://www.trenitalia.com/cms-file/common/js/themes/trenitalia_2014/001/list_json.js'
- res = ''
- req = request.Request(url, headers={'Accept-Language': 'en-US'})
- with request.urlopen(req) as r:
- for l in r:
- res += l.decode()
- j = json.loads(res)
+ sess = requests.Session()
+ sess.headers['Accept-Language'] = 'en-US'
+ r = sess.get(url)
+ j = json.loads(r.text)
stations = set()
for station in j: