Skip to content

Instantly share code, notes, and snippets.

@d33tah
Created September 29, 2010 22:13
Show Gist options
  • Select an option

  • Save d33tah/603664 to your computer and use it in GitHub Desktop.

Select an option

Save d33tah/603664 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import pysqlite2.dbapi2 as sqlite3
import urllib
import PyRSS2Gen
from lxml import html
import MySQLdb
db_config = {'user':'some_user','passwd':'some_password','db':'some_db'}
#from config import db_config
frequency = 30 #for caching purposes
xpath_contains = "contains(., 'semestr 2') and contains(., 'anie,')" + \
"and not(contains(., 'niestacjonarne'))"
def get_usage():
return """
Skrypt uruchamiamy podajac mu ID profilu pracownika, np: <br />
http://jakis-serwer/katalog/materialy.wsgi?123
"""
def try_from_cache(url):
now = int(time.time())
conn = MySQLdb.connect(**db_config)
c = conn.cursor()
c.execute("create table IF NOT EXISTS `cache` (url VARCHAR(256) CHARA" + \
"CTER SET utf8 COLLATE utf8_unicode_ci UNIQUE, value BLOB, lasttime BLOB);")
query = c.execute("SELECT * FROM cache WHERE url = %s", (url,) )
entry = c.fetchone()
if entry:
if now - int(entry[2]) < frequency:
ret = entry[1]
else:
ret = urllib.urlopen(url).read()#.decode('iso8859-2')#.encode('utf-8')
c.execute("UPDATE cache SET lasttime = %s, value = %s" \
+ "WHERE url = %s", (now,ret,url))
conn.commit()
else:
ret = urllib.urlopen(url).read()#.decode('iso8859-2')#.encode('utf-8')
c.execute("INSERT INTO cache VALUES (%s,%s,%s)", (url,ret,now))
conn.commit()
return ret
def application(environ, start_response):
try:
lecturer_id = int(environ["QUERY_STRING"])
except:
start_response('200 OK', [('Content-type','text/html')])
return get_usage()
filelist_url = "http://zarzadzanie.uni.lodz.pl/Stronag%C5%82%C3%B3wna/" + \
"WyszukiwanieMateria%C5%82%C3%B3w/tabid/167/ctl/results/" + \
"mid/552/language/pl-PL/Default.aspx%suid="+str(lecturer_id)
page = try_from_cache(filelist_url)
page = page.decode('utf8')
materials_id = "dnn_ctr552_Results_TabContainer2_TabPanel4_grvWykladowca"
xpath_str = "//table[@id='%s']//*[%s]//td" % (materials_id, xpath_contains)
tree = html.fromstring(page)
entries = tree.xpath(xpath_str)
if len(entries)>0:
rss_title = entries[0][1][0].text
else:
rss_title = "WZ UŁ - materiały".decode('utf8')
rss = PyRSS2Gen.RSS2(
title = rss_title,
link = "http://deetah.jogger.pl",
description = "Kanał RSS zawiera najnowsze materiały od wybranych "
"wykładowców Wydziału Zarządzania Uniwersytetu Łódzkiego.".decode('utf-8'),
)
for entry in reversed(entries):
file_name = entry[4].text
lecturer = entry[1][0].text
file_title = entry[1][1].text or file_name
#subject = entry[1][0].tail.split(', ')[1]
rss_title = '[%s] %s' % (lecturer, file_title)
profile_url = "http://zarzadzanie.uni.lodz.pl/tabid/173/" + \
"language/pl-PL/Default.aspx?uid="+str(environ["QUERY_STRING"])
rss_description = ((u'<b>Nazwa pliku: </b>%s<br /><b>Wykładowca: </b>' + \
'<a href="%s">%s</a><br />') % (file_name, profile_url, lecturer) \
).decode('utf8')
rss.items.append(PyRSS2Gen.RSSItem(
title = rss_title, link = filelist_url, description = rss_description,
guid = PyRSS2Gen.Guid( rss_title ),
))
start_response('200 OK', [('Content-type','application/rss+xml')])
return rss.to_xml(encoding='iso-8859-2')
if __name__ == "__main__":
print application({"QUERY_STRING":"137"},'')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment