Tuesday, December 6, 2011

Телевизийн мэдээ хусдаг скрипт хөөрхөн шөл ашиглаж хийв.

#!/usr/bin/python
# -*- coding: UTF8 -*-
from datetime import datetime, timedelta
from urllib import urlopen
from BeautifulSoup import BeautifulSoup


channel_list = [
(83,'МҮОНРТ'),
(84,'UBS-1'),
(85,'MN25 телевиз'),
(138,'Eagle Телевиз'),
(87,'TV5'),
(88,'TV9'),
(89,'TV8'),
(90,'ТМ телевиз'),
(91,'C1 телевиз'),
(92,'SBN телевиз'),
(93,'NTV '),
(95,'Боловсрол Суваг'),
(98,'BTV'),
(99,'Эх Орон Телевиз'),
(111,'МОНГОЛ ТВ'),
(112,'ETV'),
(113,'Айст Телевиз'),
(120,'SCH'),
(122,'UBS-2'),
(123,'UBS-3'),
(124,'MN25-2'),
(125,'TV5-2'),
(126,'TV9-2'),
(127,'NBS-1'),
(128,'MOVIEbox'),
(129,'SPORTbox'),
(131,'Star TV'),
(132,'Шинэ Дэлхий'),
(133,'MUSICbox'),
(134,'MBO'),
(135,'NBS-2'),
(136,'Za ТV'),
(137,'TV7')]

def get_channel_name(cid):
    cid = int(cid)
    for code,val in channel_list:
        if code==cid:
            return val

def find(soup):
    days = set([])
    for div in soup.findAll('div'):
        if div and div.has_key('class'):
            if div['class']=='tdiv' and div.has_key('onclick'):
                a=div['onclick'].split('rateprograme')[1][:-2][1:]
                a=a.split(',')
                ch_id = a[-1]
                ch_time = datetime.fromtimestamp(int(a[-3]))-timedelta(hours=15)
                pr_name = ''.join(a[:-3])
                channel = get_channel_name(ch_id)
                days.add(ch_time.isoweekday())
                print channel, pr_name, ch_time.strftime('%H:%M'), ch_time.isoweekday()


if __name__=='__main__':
    for cid, val in channel_list:
        html = urlopen('http://zuragt.mn/?channel={0}'.format(cid)).read()
        soup = BeautifulSoup(html)
        find(soup)