Quellcode durchsuchen

Use all contracts in jcdecaux_vls

Olivier Marty vor 8 Jahren
Ursprung
Commit
2961ec2a79
3 geänderte Dateien mit 42 neuen und 16 gelöschten Zeilen
  1. 1 1
      config.py
  2. 36 11
      find.py
  3. 5 4
      source.py

+ 1 - 1
config.py

@@ -10,7 +10,7 @@ api_key = {
 sources = {
   'ratp_trafic' : ['ligne_rer_B', 'ligne_metro_7'],
   'transilien' : ['RER-B', 'RER-C', 'Train-U'],
-  'jcdecaux_vls' : ['42707_full', '42707_empty', '42703_empty', '19001_full', '8038_empty']
+  'jcdecaux_vls' : ['paris_42707_full', 'paris_42707_empty', 'paris_42703_empty', 'paris_19001_full', 'paris_8038_empty', 'nantes_62_full']
 }
 
 # NOTIFICATIONS

+ 36 - 11
find.py

@@ -3,30 +3,55 @@ from class_xml import XML
 
 
 def find_id():
-  t = int(input('selectionnez\n\t1 : ratp\n\t2 : transilien\n\t3 : jcdecaux_vls (velib)\n'))
-  if t == 1:
-    print('Télechargement de la liste des lignes...')
+  t = int(input('Sélectionnez la source :\n\t1 : ratp\n\t2 : transilien\n\t3 : jcdecaux_vls (vélos en libre service)\n'))
+  if t == 1: # RATP
+    print('Téléchargement de la liste des lignes...')
     xml = XML(url='http://www.ratp.fr/meteo/', lang='html')
     dic = {tag['id']: tag['id'].replace('_', ' ') for tag in xml.data.select('.encadre_ligne')}
-  elif t == 2:
-    print('Télechargement de la liste des lignes...')
+
+  elif t == 2: # transilien
+    print('Téléchargement de la liste des lignes...')
     xml = XML(url='http://www.transilien.com/info-trafic/temps-reel', lang='html')
     dic = {}
     for line in xml.data.select('div.b_info_trafic')[0].find_all('div', recursive=False):
       id = line.select('.picto-transport')[1].get_text()
       dic[id] = id.replace('-', ' ')
-  elif t == 3:
-    print('Télechargement de la liste des stations...')
-    xml = XML(url='https://api.jcdecaux.com/vls/v1/stations?contract=paris&apiKey=' + config.api_key['jcdecaux'], lang='json')
-    dic = {sta.number.string: sta.find('name').string + ' (' + sta.address.string + ')' for sta in xml.data.find_all("item")} # we use find('name') because .name is the current tag name
+
+  elif t == 3: # jcdecaux_vls
+    print('Téléchargment de la liste des villes...')
+    xml_contracts = XML(url='https://api.jcdecaux.com/vls/v1/contracts?apiKey=' + config.api_key['jcdecaux'], lang='json')
+    contracts = {tag.find('name').string : tag.commercial_name.string for tag in xml_contracts.data.json.children}
+    while True:
+      print('Choisissez une ville :')
+      for (name, commercial_name) in contracts.items():
+        print(name + ' (' + commercial_name + ')')
+      print("toutes (toutes les villes)\n")
+      contract = input()
+      if contract == 'toutes':
+        contract = None
+        break;
+      if contract in contracts:
+        break;
+      print("Ville inconnue !\n\n")
+    print('Téléchargement de la liste des stations...')
+    if contract:
+      xml = XML(url='https://api.jcdecaux.com/vls/v1/stations?contract=' + contract + '&apiKey=' + config.api_key['jcdecaux'], lang='json')
+    else:
+      xml = XML(url='https://api.jcdecaux.com/vls/v1/stations?apiKey=' + config.api_key['jcdecaux'], lang='json')
+    dic = {}
+    for sta in xml.data.json.find_all("item", recursive=False):
+      dic[sta.contract_name.string.lower() + '_' + sta.number.string] =\
+        sta.find('name').string + ' (' + sta.address.get_text() + ')'
+      # we use find('name') because .name is the current tag name
   else:
-    raise ValueError('mauvaise réponsse !')
+    raise ValueError('mauvaise réponse !')
 
+  # find an id in dic
   while True:
     pat = input('Rechercher (ctrl+c pour quitter) : ').lower()
     print('Correspondances :')
     for key, value in dic.items():
       if pat in value.lower():
-        print('id ' + key + ' : ' + value)
+        print(key + ' :\t' + value)
 
 find_id()

+ 5 - 4
source.py

@@ -87,12 +87,13 @@ def ratp_trafic():
 
 
 def jcdecaux_vls():
-  ids = set(map(lambda s : s.split('_')[0], config.sources['jcdecaux_vls']))
+  ids = set(map(lambda s : s.rsplit('_', 1)[0], config.sources['jcdecaux_vls']))
   for station in ids:
-    xml = XML(url="https://api.jcdecaux.com/vls/v1/stations/" + station + "?contract=paris&apiKey="+config.api_key['jcdecaux'], lang="json")
+    (contract, number) = list(station.split('_'))
+    xml = XML(url="https://api.jcdecaux.com/vls/v1/stations/" + number + "?contract=" + contract + "&apiKey="+config.api_key['jcdecaux'], lang="json")
     tag = xml.data.json
-    yield Source_jcdecaux_vls_full(tag.number.string, tag.find('name').string, tag.last_update.string, tag.available_bike_stands.string, tag.status.string)
-    yield Source_jcdecaux_vls_empty(tag.number.string, tag.find('name').string, tag.last_update.string, tag.available_bikes.string, tag.status.string)
+    yield Source_jcdecaux_vls_full(contract + '_' + number, tag.find('name').string, tag.last_update.string, tag.available_bike_stands.string, tag.status.string)
+    yield Source_jcdecaux_vls_empty(contract + '_' + number, tag.find('name').string, tag.last_update.string, tag.available_bikes.string, tag.status.string)
 
 
 def transilien():