Created
April 18, 2017 18:45
-
-
Save abihf/281aca653422a292b4edb6ad4efb64b2 to your computer and use it in GitHub Desktop.
Generate xmltv from Firstmedia EPG
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 | |
import sys | |
import httplib | |
import json | |
import xml.etree.ElementTree as ET | |
from datetime import datetime | |
channels = { | |
'net': 'NET.Digital', | |
'kompas-tv': 'KOMPAS TV', | |
'metro-tv': 'METRO TV HD', | |
'tv-one': 'TVONE DIGITAL', | |
'trans-tv': 'TRANS TV HD', | |
'trans-7': 'TRANS7 HD TV', | |
'tvri-nasional': 'TVRI HD', | |
'indosiar': 'INDOSIAR NETWORK' | |
} | |
def main(): | |
if len(sys.argv) > 2: | |
current_date = sys.argv[1] | |
else: | |
current_date = datetime.now().strftime('%Y-%m-%d') | |
epg = fetch_epg(','.join(channels.keys()), current_date) | |
xml = build_xml(epg, current_date) | |
#print('<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE tv SYSTEM "xmltv.dtd">') | |
ET.dump(xml) | |
def fetch_epg(channel, date): | |
client = httplib.HTTPConnection('jkt.firstmedia.com') | |
url = '/tv-guides/selected_date:%s/selected_channels:%s/get.json' % (date, channel) | |
headers = {"X-Requested-With": "XMLHttpRequest", "Referrer": "http://jkt.firstmedia.com/tv-guides/"} | |
client.request('GET', url, headers=headers) | |
resp = client.getresponse().read() | |
return json.loads(resp) | |
def build_xml(epg, current_date): | |
root = ET.Element('tv', attrib={'source-info-url': 'http://jkt.firstmedia.com/tv-guides/'}) | |
for channel in epg: | |
channel_id = channel['Channel']['slug'] | |
xch = ET.SubElement(root, 'channel', attrib={'id': channel_id}) | |
if channel_id in channels: | |
ET.SubElement(xch, 'display-name').text=channels[channel_id] | |
ET.SubElement(xch, 'display-name').text=channel['Channel']['title'] | |
programs = channel['Schedule'] | |
if isinstance(programs, dict): | |
programs = programs.values() | |
for program in programs: | |
duration = int(program['length']) * 60 | |
# bugshit | |
if duration > 0 and program['date'][0:10] == current_date and program['title'] != 'no schedule': | |
start_time_unix = int(program['unix_date']) / 1000 | |
start_time = datetime.fromtimestamp(start_time_unix) | |
stop_time = datetime.fromtimestamp(start_time_unix + duration) | |
attrib = { | |
'channel': channel_id, | |
'start': start_time.strftime('%Y%m%d%H%M%S +0700'), | |
'stop': stop_time.strftime('%Y%m%d%H%M%S +0700') | |
} | |
xprog = ET.SubElement(root, 'programme', attrib=attrib) | |
ET.SubElement(xprog, 'title', attrib={'lang':'id'}).text = program['title'] | |
ET.SubElement(xprog, 'desc', attrib={'lang':'id'}).text=program['long_description'] | |
return root | |
def log(s): | |
sys.stderr.write(s + "\n") | |
main() |
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
#!/bin/sh | |
set -euo pipefail | |
SCRIPT=$(readlink -f "$0") | |
SCRIPTPATH=$(dirname "$SCRIPT") | |
PYSCRIPT=$SCRIPTPATH/fetch.py | |
CACHEDIR=$SCRIPTPATH/cache | |
DATE=$(date +%Y-%m-%d) | |
CACHEFILE=$CACHEDIR/$DATE.xml | |
mkdir -p $CACHEDIR | |
[ -f $CACHEFILE ] || $PYSCRIPT $DATE > $CACHEFILE | |
cat $CACHEFILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment