Skip to content

Instantly share code, notes, and snippets.

@chesster
Created March 1, 2011 00:06
Show Gist options
  • Select an option

  • Save chesster/848331 to your computer and use it in GitHub Desktop.

Select an option

Save chesster/848331 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
from string import maketrans
from lxml import html
import codecs
import random
import datetime
import os
import time
class pyNtv:
@staticmethod
def listFiles(path):
return [("%s/%s" % (path, i)) for i in os.listdir(path)]
@staticmethod
def read(file_name):
list = []
file = codecs.open(file_name,"r", "utf-8")
for line in file:
line_split = line.split(';')
if len(line_split) == 1 :
list.append(line_split[0])
else :
list.append(line_split)
file.close()
return list
@staticmethod
def write(file_name, list):
file = codecs.open(file_name,"a","utf-8")
for i in list:
file.write("%s\n" % (i))
file.close()
@staticmethod
def getChanellList(url):
program = html.parse(url)
x = [i.get("href").split('/')[2] for i in program.xpath("//a[re:match(@href, 'ramowka/')]",
namespaces={"re": "http://exslt.org/regular-expressions"})]
return x
@staticmethod
def getProgram(url):
pr = html.parse(url).findall(".//div[@class='tvguide-index-channelday']")[0].findall(".//div[@class='event']/p")
r={}
for i in pr:
r[i.text.strip()] = i.findall('.//a')[0].text.strip()
return r
@staticmethod
def readProgramFile(file):
time = strftime("%H%M")
list = pyNtv.read(file)
last = None
nextdate = None
planner = {}
for listitem in list:
planner[listitem[0]]=listitem[1]
planner_keys = planner.keys()
planner_keys.sort()
for plan in planner_keys:
if plan < time:
last = planner[plan].strip()
else:
nextdate = plan
break
if last:
if nextdate:
print (u'%s: %s do %s:%s' % (file.split("/")[1], last, nextdate[:2], nextdate[2:])).encode('ascii', 'ignore')
else:
print (u'%s: %s' % (file.split("/")[0], last)).encode('ascii', 'replace')
@staticmethod
def programToFiles():
urls = pyNtv.getChanellList('http://n.pl/ramowka.html')
for i in urls:
fname = i.split(".")[0]
print fname
try:
prlist = []
pr = pyNtv.getProgram('http://n.pl/ramowka/index/%s' % (i))
pr_keys = pr.keys()
pr_keys.sort()
for k in pr_keys:
ks = k.split(":")
prlist.append(u'%s%s;%s' % (ks[0],ks[1],pr[k]))
pyNtv.write((u'tv/%s' % fname),prlist)
except:
print "...."
@staticmethod
def getAll():
#sync co dzien
ddir = 'tv'
files = pyNtv.listFiles(ddir)
mod = datetime.datetime.fromtimestamp(os.path.getmtime(files[0]))
if int(time.strftime("%m%d")) > int(("%s%s") % (mod.month,mod.day)):
for remove in files:
os.remove(remove)
pyNtv.programToFiles()
# wyswietlaj
for tvfile in files:
pyNtv.readProgramFile(("%s/%s") % (ddir, tvfile))
if __name__ == '__main__':
pyNtv.getAll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment