<br><br><div class="gmail_quote">On Mon, Feb 22, 2010 at 10:46 PM, Stefan Behnel <span dir="ltr"><<a href="mailto:stefan_ml@behnel.de">stefan_ml@behnel.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">sharifah ummu kulthum, 22.02.2010 14:24:<br>
</div><div class="im">> File "grabmy.py", line 63, in get_html<br>
> return BeautifulSoup(content)<br>
> File "build/bdist.linux-i686/egg/BeautifulSoup.py", line 1499, in __init__<br>
> File "build/bdist.linux-i686/egg/BeautifulSoup.py", line 1230, in __init__<br>
> File "build/bdist.linux-i686/egg/BeautifulSoup.py", line 1263, in _feed<br>
> File "/usr/lib/python2.6/HTMLParser.py", line 108, in feed<br>
> self.goahead(0)<br>
> File "/usr/lib/python2.6/HTMLParser.py", line 148, in goahead<br>
> k = self.parse_starttag(i)<br>
> File "/usr/lib/python2.6/HTMLParser.py", line 226, in parse_starttag<br>
> endpos = self.check_for_whole_start_tag(i)<br>
> File "/usr/lib/python2.6/HTMLParser.py", line 301, in<br>
> check_for_whole_start_tag<br>
> self.error("malformed start tag")<br>
> File "/usr/lib/python2.6/HTMLParser.py", line 115, in error<br>
> raise HTMLParseError(message, self.getpos())<br>
> HTMLParser.HTMLParseError: malformed start tag, at line 830, column 36<br>
<br>
</div>Just noticed this now - you seem to be using BeautifulSoup, likely version<br>
3.1. This version does not support parsing broken HTML any well, so use<br>
version 3.0.8 instead, or switch to the tools I indicated.<br>
<br>
Note that switching tools means that you need to change your code to use<br>
them. Just installing them is not enough.<br>
<font color="#888888"><br>
Stefan<br>
<br>
</font></blockquote></div><br>I am so sorry but I really don't know how to change the code as I have just learn python. How am I going to switch the version or to change the code? Because I don't really understand the code.<br>
<br>Here is the code:<br><br><span style="font-family: courier new,monospace;">'''</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Copyright (c) 2008 Yap Sok Ann <<a href="mailto:sayap@sayap.com">sayap@sayap.com</a>></span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">This module contains xmltv grabbers for Malaysia channels.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">'''</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">__author__ = 'Yap Sok Ann <<a href="mailto:sayap@sayap.com">sayap@sayap.com</a>>'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">__license__ = 'PSF License'</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import logging</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">from datetime import date as dt</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">from datetime import datetime, time, timedelta</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">from <a href="http://dateutil.tz">dateutil.tz</a> import tzlocal</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">from httplib2 import Http</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">from lxml import etree</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">from urllib import urlencode</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">from BeautifulSoup import BeautifulSoup</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">channels = ['rtm1', 'rtm2', 'tv3', 'ntv7', '8tv', 'tv9']</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">datetime_format = '%Y%m%d%H%M%S %z'</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">h = Http()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">h.force_exception_to_status_code = True</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">#h.timeout = 15</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">logging.basicConfig(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> level=logging.DEBUG,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> format='%(asctime)s %(levelname)-8s %(process)d %(message)s',</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">log = logging.getLogger(__name__)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def strclean(s):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> s = s.strip().replace('&lsquo;', '\'').replace('&rsquo;', '\'')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if s != '&nbsp;':</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return s</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class Grabber(object):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> base_url = None</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def __init__(self, channel):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.channel = channel</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.url = self.base_url</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def qs_params(self, date, **kwargs):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> '''Returns a dict of params to form the url's query string</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> '''</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> raise NotImplementedError</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def _parse_html(self, date, html):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> '''Returns a list of dicts with the following keys:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> - mandatory: title, start</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> - optional: stop, sub_title, desc, episode_number, episode_system</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> '''</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> raise NotImplementedError</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def get_html(self, date, **kwargs):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> params = self.qs_params(date, **kwargs)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> response, content = h.request(self.url + '?' + urlencode(params))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if response.status == 200:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return BeautifulSoup(content)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> log.error('Status: %s\nContent: %s' % (response.status, content))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def parse_html(self, date, html):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> prev_schedule = None</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> try:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for schedule in self._parse_html(date, html):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if 'stop' in schedule:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> yield schedule</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> elif prev_schedule:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> prev_schedule['stop'] = schedule['start']</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> yield prev_schedule</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> prev_schedule = schedule</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> except:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> log.exception('Cannot parse html for date %s' % date)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def to_xml(self, schedules):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for schedule in schedules:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> program = etree.Element('programme', channel=self.channel,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> start=schedule['start'].strftime(datetime_format),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> stop=schedule['stop'].strftime(datetime_format))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> title = etree.SubElement(program, 'title')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> title.text = schedule['title']</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if schedule.get('episode_num'):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> episode_num = etree.SubElement(program, 'episode-num')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> episode_num.set('system', schedule.get('episode_system'))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> episode_num.text = schedule['episode_num']</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for field in ['sub_title', 'desc']:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if schedule.get(field):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> elem = etree.SubElement(program, field.replace('_', '-'))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> elem.text = schedule[field]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> yield program</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def grab(self, date, **kwargs):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> html = self.get_html(date, **kwargs)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if html:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return self.to_xml(self.parse_html(date, html))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class Astro(Grabber):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> base_url = '<a href="http://www.astro.com.my/channels/%(channel)s/Default.asp">http://www.astro.com.my/channels/%(channel)s/Default.asp</a>'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> params_dicts = [dict(batch=1),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> dict(batch=2)]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ignores = ['No Transmission', 'Transmission Ends']</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def __init__(self, channel):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> self.channel = channel</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.url = self.base_url % dict(channel=channel)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def qs_params(self, date, **kwargs):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> kwargs['sDate'] = date.strftime('%d-%b-%Y')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return kwargs</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def _parse_html(self, date, html):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> header_row = html.find('tr', bgcolor='#29487F')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for tr in header_row.fetchNextSiblings('tr'):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> tds = tr.findChildren('td')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> title = strclean(tds[1].find('a').string)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if title in self.ignores:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> continue</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> # start time, '21:00' -> 9 PM</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> hour, minute = [int(x) for x in tds[0].string.split(':')]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> start = datetime.combine(date,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> time(hour, minute, tzinfo=tzlocal()))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # duration, '00:30' -> 30 minutes</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> hours, minutes = [int(x) for x in tds[2].string.split(':')]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> stop = start + timedelta(hours=hours, minutes=minutes)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> yield dict(title=title, start=start, stop=stop)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class TheStar(Grabber):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> base_url = '<a href="http://star-ecentral.com/tvnradio/tvguide/guide.asp">http://star-ecentral.com/tvnradio/tvguide/guide.asp</a>'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> params_dicts = [dict(db='live')]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> def qs_params(self, date, **kwargs):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> kwargs['pdate'] = date.strftime('%m/%d/%Y')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> kwargs['chn'] = self.channel.replace('rtm', 'tv')</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return kwargs</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def _parse_html(self, date, html):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> last_ampm = None</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> header_row = html.find('tr', bgcolor='#5e789c')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for tr in header_row.fetchNextSiblings('tr'):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> tds = tr.findChildren('td')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> schedule = {}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> schedule['title'] = strclean(tds[1].find('b').find('font').string)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> schedule['desc'] = strclean(tds[2].find('font').string)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> episode_num = strclean(tds[3].find('font').string)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if episode_num:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> try:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> episode_num = int(episode_num) - 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> episode_num = '.' + str(episode_num) + '.'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> episode_system = 'xmltv_ns'</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> except ValueError:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> episode_system = 'onscreen'</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> schedule['episode_num'] = episode_num</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> schedule['episode_system'] = episode_system</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> # start time, '9.00pm' -> 9 PM</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> time_str = tds[0].find('font').string</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ampm = time_str[-2:]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> hour, minute = [int(x) for x in time_str[:-2].split('.')]</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if ampm == 'pm' and hour < 12:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> hour += 12</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> elif ampm =='am' and hour == 12:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> hour = 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if last_ampm == 'pm' and ampm == 'am':</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> date = date + timedelta(1)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> schedule['start'] = datetime.combine(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> date, time(hour, minute, tzinfo=tzlocal()))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> last_ampm = ampm</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> yield schedule</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def main():</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> from optparse import OptionParser</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> parser = OptionParser()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> parser.add_option('-s', '--source', dest='source',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> help='SOURCE to grab from: Astro, TheStar. Default: TheStar')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> parser.add_option('-d', '--date', dest='date',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> help='Start DATE to grab schedules for (YYYY-MM-DD). Default: today')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> parser.add_option('-n', '--days', dest='days',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> help='Number of DAYS to grab schedules for. Default: 1')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> parser.add_option('-f', '--file', dest='filename', metavar='FILE',</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> help='Output FILE to write to. Default: stdout')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> options, args = parser.parse_args()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if options.source is None:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> cls = TheStar</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> cls = globals()[options.source]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if options.date is None:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> date = dt.today()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> date = dt(*[int(x) for x in options.date.split('-')])</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if options.days is None:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> days = 1</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> days = int(options.days)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> root = etree.Element('tv')</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for channel in channels:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> grabber = cls(channel)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for i in range(days):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> for params_dict in cls.params_dicts:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for elem in grabber.grab(date + timedelta(i), **params_dict):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> root.append(elem)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> xml = etree.tostring(root, encoding='UTF-8', xml_declaration=True,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pretty_print=True)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> if options.filename is None:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print xml</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> open(options.filename, 'w').write(xml)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">if __name__ == '__main__':</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> main()</span><br style="font-family: courier new,monospace;"><br>