Browse Source

Add a dummy from_location, add gen_sources

Olivier Marty 8 years ago
parent
commit
d218e2355b
2 changed files with 28 additions and 12 deletions
  1. 9 10
      main.py
  2. 19 2
      source.py

+ 9 - 10
main.py

@@ -4,8 +4,6 @@ from event import Event, HeapEvent
 from datetime import datetime, timedelta
 from time import sleep
 import notification
-from itertools import chain
-
 
 def main():
     heap = HeapEvent()
@@ -28,15 +26,16 @@ def main():
         # next event
         if not heap.empty() and heap.top().date-datetime.now() < gap:
             event = heap.pop()
+
+            # get useful ids of sources for this location
+            ids_sources = source.from_location(event.location)
+            # grab info from internet for these sources
+            sources=source.gen_sources(ids_sources)
+            for src in sources:
+              if src.id in ids_sources and src.problem():
+                # there is a problem ! We notify the user...
+                notification.notify(src.message)
             print(event.description + " at " + event.location + ", " + str(event.date))
 
 if __name__ == "__main__":
     main()
-
-
-#sources=chain(source.ratp_trafic(), source.transilien(), source.jcdecaux_vls())
-
-#for source in sources:
-#  if source.id in config.sources.get(source.source, []):
-#    if source.problem():
-#      notification.notify(source.message)

+ 19 - 2
source.py

@@ -1,6 +1,7 @@
 from class_xml import XML
 import config
 import datetime
+from itertools import chain
 
 # SOURCE CLASSES
 
@@ -86,8 +87,8 @@ def ratp_trafic():
       tag['id'].replace('_', ' ') + ' : ' + tag.select('span.perturb_message')[0].string)
 
 
-def jcdecaux_vls():
-  ids = set(map(lambda s : s.rsplit('_', 1)[0], config.sources['jcdecaux_vls']))
+def jcdecaux_vls(ids):
+  ids = set(map(lambda s : s.rsplit('_', 1)[0], ids))
   for station in ids:
     (contract, number) = list(station.split('_'))
     xml = XML(url="https://api.jcdecaux.com/vls/v1/stations/" + number + "?contract=" + contract + "&apiKey="+config.api_key['jcdecaux_vls'], lang="json")
@@ -112,3 +113,19 @@ def transilien():
       message += det.get_text()
     message = " ".join(message.split()) # delete multiple spaces
     yield Source_transilien(id, message)
+
+
+
+# interface functions
+
+def from_location(location):
+    """return a list of source ids useful for location
+    TODO : for the moment returns all ids in config.sources"""
+    lists = [values for (name, values) in config.sources.items()]
+    # return flattened list
+    return [item for sublist in lists for item in sublist]
+
+def gen_sources(ids):
+  return chain(ratp_trafic(),\
+      transilien(),\
+      jcdecaux_vls(config.sources['jcdecaux_vls'])) # TODO filter out ids for jcdecaux