Skip to content

Instantly share code, notes, and snippets.

@efaisal
Last active December 11, 2015 09:59
Show Gist options
  • Save efaisal/4583986 to your computer and use it in GitHub Desktop.
Save efaisal/4583986 to your computer and use it in GitHub Desktop.
Skrip untuk muat turun data waktu solat Jakim
#!/usr/bin/env python
"""
Muat turun data waktu solat Jakim
"""
import re
import requests
from lxml import etree
# Tukar pembolehubah tahun & zon jika perlu
tahun = '2013'
zon = 'SGR01'
url = 'http://www.e-solat.gov.my/waktusolat.php?zone=%s&state=&year=%s&jenis=year&bulan=%s&LG=BM'
peta_bulan = {'Januari': '01', 'Februari': '02', 'Mac': '03', 'April': '04',
'Mei': '05', 'Jun': '06', 'Julai': '07', 'Ogos': '08',
'September': '09', 'Oktober': '10', 'November': '11',
'Disember': '12'}
regex = re.compile("([\d]{1,2}) ([\w]+)")
data = []
for i in range(1, 13):
bulan = "%02d" % i
print("Muat turun data untuk bulan %d" % i)
r = requests.get(url % (zon, tahun, bulan))
c = r.text
root = etree.HTML(c)
print("Memproses data yand di muatturun")
for c in root.getchildren()[0].findall('table')[1][1][0].findall('table')[1][0][0][1].getchildren()[1:-1]:
tmp = []
for ctd in c.getchildren():
tmp.append(''.join([t.strip() for t in ctd.itertext()]))
data.append(tmp)
jadual = []
for d in data:
hari, bulan = regex.match(d[0]).groups()
bulan = peta_bulan[bulan]
tarikh = "%s-%s-%02d" % (tahun, bulan, int(hari))
imsak = d[2] if len(d[2]) == 5 else '0' + d[2]
subuh = d[3] if len(d[3]) == 5 else '0' + d[3]
syuruk = d[4] if len(d[4]) == 5 else '0' + d[4]
zohor = d[5] if len(d[5]) == 5 else '0' + d[5]
asar = d[6] if len(d[6]) == 5 else '0' + d[6]
maghrib = d[7] if len(d[7]) == 5 else '0' + d[7]
isya = d[8] if len(d[8]) == 5 else '0' + d[8]
waktu = "%s|Imsak\\t:\\t%s\\nSubuh\\t:\\t%s\\nSyuruk\\t:\\t%s\\nZohor\\t:\\t%s\\nAsar\\t:\\t%s\\nMaghrib\\t:\\t%s\\nIsya\\t\\t:\\t%s" \
% (tarikh, imsak, subuh, syuruk, zohor, asar, maghrib, isya)
jadual.append(waktu)
print('')
with open('jadual', 'w') as fp:
fp.write("\n".join(jadual))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment