Created
April 15, 2012 23:28
-
-
Save metaphox/2395314 to your computer and use it in GitHub Desktop.
watch shanghai german consulate termin
This file contains hidden or 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 python | |
#-*- coding: utf-8 -*-# | |
import smtplib, time, urllib2 | |
from bs4 import BeautifulSoup | |
from email.mime.text import MIMEText | |
def mailme(message): | |
msg = MIMEText(message) | |
msg['Subject'] = 'German Consu Termin!' | |
msg['From'] = '[email protected]' | |
msg['To'] = '<targeemailaddress>' | |
s = smtplib.SMTP('gmail-smtp-in.l.google.com') | |
s.sendmail('[email protected]', ['<targeemailaddress>'], msg.as_string()) | |
s.quit() | |
def tellme(message, direct=False): | |
#print message | |
mailme(message) | |
def watch(): | |
MONATE = ['06.2012', '07.2012'] | |
FAILTXT = [u'An diesem Tag k\xf6nnen keine Termine gebucht werden', | |
u'An diesem Tag sind alle Termine belegt', | |
u'An diesem Tag werden keine Termine angeboten', | |
u'Termine f\xfcr diesen Tag k\xf6nnen nicht gebucht werden', | |
u'All appointments for this date are already taken', | |
u'On this date bookings are not possible', | |
u'Appointments for this date cant be booked', | |
u'For this date no appointments are offered'] | |
FAILTXT_DAY = [u'In diesem Zeitraum sind keine Termine frei', | |
u'No appointments available for this period'] | |
#BASEURL = 'https://service2.diplo.de/rktermin/extern/appointment_showMonth.do?locationCode=shan&realmId=96&categoryId=183&dateStr=01.{0}.{1}' | |
#cate 183 is for "normal" termin, cate 460 is for the "special" ones | |
BASEURL = "https://service2.diplo.de/rktermin/extern/appointment_showMonth.do?locationCode=shan&realmId=96&categoryId=460&dateStr=01.{0}.{1}" | |
DATEURL = 'https://service2.diplo.de/rktermin/extern/appointment_showDay.do?locationCode=shan&realmId=96&categoryId=460&dateStr={0}.{1}.{2}' | |
dates = [] | |
for mon in MONATE: | |
mon_view_f = urllib2.urlopen( BASEURL.format( *mon.split('.') ) ) | |
soup = BeautifulSoup(mon_view_f.read()) | |
for each in soup.find_all('h4'): | |
txt = each.find_parent('div').find_next_sibling('div').get_text().strip() | |
#see if txt has any of FAILTXT, then reduce the result boolean list | |
if not True in map(lambda x : x in txt, FAILTXT): | |
print txt | |
dates.append(each.get_text()) | |
if len(dates): | |
days = [day.split(' ')[1] for day in dates] | |
urls = [(day, DATEURL.format( *day.split('.') )) for day in days] | |
links = [] | |
for day, url in urls: | |
tellme('{0} -- {1}'.format(day, url)) | |
""" | |
UNTESTED | |
# print "Opening %s" % url | |
day_view_f = urllib2.urlopen(url) | |
soup_day = BeautifulSoup(day_view_f.read()) | |
idx = 1 | |
for each in soup_day.find_all('h4'): | |
anchortag = each.find_parent('div').find_next_sibling('div').find('a') | |
if anchortag: | |
tellme('{0} ({2}) https://service2.diplo.de/rktermin/{1}'.format(day, anchortag.get('href'), idx)) | |
idx += 1 | |
""" | |
if __name__ == '__main__': | |
cnt = 0 | |
error_cnt = 0 | |
tellme('terminwatch starts to run') | |
while 1: | |
try: | |
watch() | |
except urllib2.URLError: | |
error_cnt += 1 | |
if error_cnt % 20 == 0: | |
tellme('Termin watch is still running, %s times checked, %s errors occured.' % (cnt, error_cnt)) | |
cnt += 1 | |
if cnt % 480 == 0: | |
tellme('Termin watch is still running, %s times checked so far, %s errors occured.' % (cnt, error_cnt)) | |
time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is it still alive?) cause i have tested but it doesn't work)