#!/usr/bin/env python
# -*- conding: utf8 -*-

import re
import os
import ConfigParser
import urllib2
import xml.etree.ElementTree as ET
import subprocess

config = ConfigParser.RawConfigParser()
config.readfp(open(r'settings.conf'))
username = config.get('Main', 'username')
password = config.get('Main', 'password')

out_dir = "files/"
if not os.path.exists(os.path.dirname(out_dir)):
    os.makedirs(os.path.dirname(out_dir))

url = 'http://xmltv.s-tv.ru/pers/%s/index.php?pass=%s' % (username, password)

file = "stv.xml"
response = urllib2.urlopen(url)
with open(file, 'wb') as f:
    f.write(response.read())

tree = ET.parse(file)
root = tree.getroot()
procs = []

print "Downloading EPG files for %s channels. Please wait a bit" % (len(root))

for child in root:
    channel_src = re.sub('&sh=.*', '', child.attrib['src'])
    channel_id = child.attrib['channel_id']
    procs.append(subprocess.Popen(['/usr/bin/curl', '-s', '-o',
                                  out_dir+"/"+channel_id+".xml", channel_src]))
for proc in procs:
    proc.wait()

os.remove(file)
print "All done. Have a nice day!"

@@ -60,16 +60,27 @@
 port os%0A
+import sys%0A
 import C
@@ -200,16 +200,25 @@
 arser()%0A
+try:%0A    
 config.r
@@ -247,16 +247,179 @@
 conf'))%0A
+except IOError as e:%0A    print %22Can't find settings.conf! Please create it%22%0A    print %22I/O error (%7B0%7D): %7B1%7D%22.format(e.errno, e.strerror)%0A    sys.exit(1)%0A%0Atry:%0A    
 username
@@ -452,16 +452,20 @@
 rname')%0A
+    
 password
@@ -497,16 +497,161 @@
 ssword')
+%0Aexcept (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:%0A    print %22Error in your settings.conf file:%22%0A    print e%0A    sys.exit(1)
 %0A%0Aout_di
@@ -788,18 +788,19 @@
 ru/pers/
-%25s
+%7B0%7D
 /index.p
@@ -811,24 +811,94 @@
 ass=
-%25s' %25 (username,
+%7B1%7D'.format(username, %0A                                                               
  pas
@@ -922,16 +922,25 @@
 tv.xml%22%0A
+try:%0A    
 response
@@ -963,16 +963,155 @@
 en(url)%0A
+except urllib2.HTTPError as e:%0A    print e%0A    print %22Probably wrong username and/or password. Check your settings.conf.%22%0A    sys.exit(1)%0A%0A
 with ope
@@ -1216,16 +1216,37 @@
 s = %5B%5D%0A%0A
+channels = len(root)%0A
 print %22D
@@ -1274,10 +1274,11 @@
 for 
-%25s
+%7B0%7D
  cha
@@ -1299,28 +1299,28 @@
 wait
- a bit%22 %25 (len(root)
+...%22.format(channels
 )%0A%0Af

Handle some possible errors