VR-Online (Август-сентябрь 2010)

Page 38

38

Кодинг title.decode('cp1251').encode('utf-8') link = re.findall(r'<link>(.+?)</link>', item)[0] link = string.replace(link, "&", "&") rid = re.findall(r'rid=([^&]*)', link)[0] rid = int(rid) desc = re.findall(r'<description>(.*?)</description>', item, re.DOTALL)[0] # если rss'ка в виндовой кодировке обращаем в utf-8 desc = desc.decode('cp1251').encode('utf-8') return title, link, rid, desc

легким движением руки мы сварганили свой мини­твиттер фид. Удачи!

# твитим def tweet(txt): txt = txt[:139] data = StringIO.StringIO() curl = pycurl.Curl()

curl.setopt(pycurl.URL,'http://twitter.com/stat uses/update.xml') curl.setopt(pycurl.WRITEFUNCTION, data.write) curl.setopt(pycurl.USERPWD,'логин:пароль')

curl.setopt(pycurl.POSTFIELDS,'status='+txt) curl.setopt(pycurl.POST,1) curl.perform() curl.close() # ==========================================собст венно сама прога # формируем массив твитанутых id-шников try: fposted = 'posted_tweets.txt' f = open(fposted, "r") posted = f.readlines() f.close() for i in xrange(len(posted)): posted[i] = int(posted[i]) except: posted = [] # получаем rss rss = urllib.urlopen('адрес rss`ки').read() # делим на items items = re.findall(r'<item>(.+?)</item>', rss, re.DOTALL) # пробегаемся по всем элементам rss'ки for item in items: # из items выделяем ссылку, номер, заголовок title, link, rid, desc = get_fields(item) print str(rid)+"\n" # проверяем rid на твитнутость if rid not in posted: # сокращаем ссылку short = shorter(link) # и твитим короткую ссылку, заголовок и описание tweet(short+' '+title+' '+desc) # запоминаем что затвитили f = open(fposted, "a+") f.write(str(rid)+"\n") f.close()

Вместо заключения

Ставим скрипт в cron на каждые полчаса и смотрим, как все делается за нас. Вот таким

vr­online | сентябрь 2010


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.