Skip to content

Instantly share code, notes, and snippets.

@maliqq
Created March 5, 2010 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maliqq/322632 to your computer and use it in GitHub Desktop.
Save maliqq/322632 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import feedparser
import re
import urllib, urllib2
import datetime, time
import os
FEED_URL = "http://kino.motor.kg/Rss.aspx"
d = feedparser.parse(FEED_URL)
POST_URL = "http://twitter.com/statuses/update.xml"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, "http://twitter.com/", "KinoMotorKg", "*********")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
entries = d['entries']
entries.reverse()
def read_latest_update_datetime():
ts = 0
try:
f = open(os.path.join(os.path.dirname(__file__), ".updated"), 'r')
contents = f.read()
f.close()
ts = int(float(contents))
except:
pass
return datetime.datetime.fromtimestamp(ts)
def write_latest_update_datetime(dt):
f = open(os.path.join(os.path.dirname(__file__), ".updated"), 'w')
f.write(str(time.mktime(dt.timetuple())))
f.close()
dt = latest_update_datetime = read_latest_update_datetime()
for entry in entries:
update_datetime = datetime.datetime(*(entry.updated_parsed[0:6]))
if update_datetime <= latest_update_datetime:
continue
dt = update_datetime
description = entry.description
title = entry.title
link = entry.link
year = None
m = re.search(ur"<tr><td valign=\"top\" align=\"right\"><b>Дата&nbsp;выхода:<\/b><\/td><td valign=\"top\">.*?(\d{4})<\/td><\/tr>", description, re.M)
if m:
year = m.group(1)
imdb = None
m = re.search(ur"<tr><td valign=\"top\" align=\"right\"><b>Рейтинг&nbsp;IMDB:<\/b><\/td><td valign=\"top\">(.*?)<\/td><\/tr>", description, re.M)
if m:
imdb = m.group(1)
genre = None
m = re.search(ur"<tr><td valign=\"top\" align=\"right\"><b>Жанр:<\/b><\/td><td valign=\"top\">(.*?)<\/td><\/tr>", description, re.M)
if m:
genre = m.group(1)
message = title + " (" + year + ") [" + genre + "] " + " IMDB: " + imdb + " " + link
req = urllib2.Request(POST_URL, urllib.urlencode({"status": message.encode("utf-8")}), )
urllib2.urlopen(req)
write_latest_update_datetime(dt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment