Last active
July 8, 2016 05:04
-
-
Save azimut/9262f105c05d7133372ceb6f69175ef8 to your computer and use it in GitHub Desktop.
Obtener el estado del subte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# TODO: return a json to obtain all states on one request | |
import argparse | |
from BeautifulSoup import BeautifulSoup | |
import urllib2 | |
import random | |
from datetime import datetime | |
STATUS_OK = '#AAAAAA' | |
STATUS_NOTOK = '#FFFFFF' | |
STATUS_OFF = '#000000' | |
def get_metrovias(subway_line): | |
page = urllib2.urlopen("http://www.metrovias.com.ar/") | |
soup = BeautifulSoup(page.read()) | |
subway_status = soup.find("table")\ | |
.find("span", id="status-line-" + subway_line.upper() )\ | |
.text | |
if subway_status == 'Normal': | |
color_status = STATUS_OK | |
else: | |
color_status = STATUS_NOTOK | |
return color_status | |
def get_buenosaires(subway_line): | |
page = urllib2.urlopen("http://www.buenosaires.gob.ar/subte") | |
soup = BeautifulSoup(page.read()) | |
subway_status = soup.find("div",id="linea" + subway_line.upper() )\ | |
.find("span",{"class":"msg"})\ | |
.text | |
if subway_status == 'Servicio normal': | |
color_status = STATUS_OK | |
else: | |
color_status = STATUS_NOTOK | |
return color_status | |
subway_functions = [get_metrovias,get_buenosaires] | |
parser = argparse.ArgumentParser() | |
parser.add_argument("transporte",choices=["a","b","c","d","e","h"]) | |
args = parser.parse_args() | |
weekday = datetime.today().weekday() | |
hour = datetime.now().hour | |
if 0 <= weekday <=5 and 9 <= hour <= 21: | |
print random.choice(subway_functions)(args.transporte) | |
else: | |
print STATUS_OFF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment