Skip to content

Instantly share code, notes, and snippets.

@shotasenga
Last active July 3, 2020 04:30
Show Gist options
  • Save shotasenga/d6f00d0f3549cb7bea7d31bcf04f1c08 to your computer and use it in GitHub Desktop.
Save shotasenga/d6f00d0f3549cb7bea7d31bcf04f1c08 to your computer and use it in GitHub Desktop.
Send notification through IFTTT when ARCH ENEMY ticket information has been updated
import json
import urllib.request
from html.parser import HTMLParser
# Define URLs
SOURCE_URL = 'https://www.creativeman.co.jp/event/arch_e2018/'
IFTTT_ENDPOINT = 'https://maker.ifttt.com/trigger/arch_enemy_concert_parsed/with/key/cTU_U2Z-OUABzEYyhS3EFe'
class Parser(HTMLParser):
'''Parse the ticket information page'''
def __init__(self):
HTMLParser.__init__(self)
self.events = []
self.__event = None
self.__key = 0
self.__inside_the_table = False
self.__node = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == 'table' and attrs['class'] == 'event-table':
self.__inside_the_table = True
self.__key = -1
self.__event = []
if self.__inside_the_table:
self.__node = {'tag': tag, 'attrs': attrs}
def handle_endtag(self, tag):
if tag == 'table' and self.__inside_the_table:
self.__inside_the_table = False
self.events.append(self.__event)
self.__event = None
def handle_data(self, data):
if self.__inside_the_table == False:
return
if self.__node['tag'] == 'tr':
self.__key += 1
self.__event.append([])
if data.strip():
self.__event[self.__key].append(data.strip())
def ifttt_parameter(parameters, new_value):
'''add new_value to given parameters dict'''
index = str(len(parameters.keys()) + 1)
parameters['value' + index] = new_value
def ifttt_notify(parameters):
'''send notification on IFTTT'''
params_json = json.dumps(parameters).encode('utf8')
req = urllib.request.Request(
IFTTT_ENDPOINT,
data=params_json,
headers={'content-type': 'application/json'}
)
response = urllib.request.urlopen(req)
if response.status != 200:
raise Exception('IFTTT dosen\'t response 200')
def fetch_html(url):
'''Fetch a html document from the web'''
response = urllib.request.urlopen(url)
if response.status != 200:
raise Exception('The souce page dosen\'t response 200')
return response.read().decode('utf8')
def fake_html(*args):
contet = None
with open('/tmp/fake.html', 'r') as fp:
content = fp.read()
return content
# main
def lambda_handler(event, context):
try:
content = fetch_html(SOURCE_URL)
# content = fake_html(SOURCE_URL)
parser = Parser()
parser.feed(content)
# - build notification parameters
notification_parameters = {}
for event in parser.events:
if '東京' in event[0][0] and event[3][1] != '調整中':
ifttt_parameter(notification_parameters, event[0][0][:12] + ':' + ':'.join(event[3][1:]))
except(Error):
ifttt_parameter(notification_parameters, 'Failed to parse the page.')
if len(notification_parameters.keys()) > 0:
ifttt_parameter(notification_parameters, SOURCE_URL)
return "Something has changed!"
else:
return "Nothing has change.."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment