Here I come, there I am.

Quotation Crawler към мобилен

at 10:05 by nofearinc
Category: techies

Като фен на сентенциите и афоризмите, често отварям сайта http://brainyquote.com . Не веднъж ми е хрумвало, че би било готино да получавам през определен период от време цитати (подобно на услугите на мобилните оператори). Написах си едно скриптче на Питон, което си пускам в баша като daemon и ми праща ежедневно цитат по СМС. Ако на някой му върши работа, може да го ползва:
 
import sys
import urllib
import httplib
from xml.etree import ElementTree
 
quotes = []
countryCode = 359 # country code
operatorCode = 881 # operator code
telphone = 987654 # tel phone
 
# 
 
def initQuotes():
    #Brainyquote Quote of the day feed
    tree = ElementTree.parse(urllib.urlopen('http://feeds.feedburner.com/brainyquote/QUOTEBR'))
    for item in tree.findall('//item'):
        quotes.append({
                       'title': item[0].text,
                       'quote': item[1].text
                       })
 
 
def lookupSendQuote(index):
    smsportal = httplib.HTTPConnection('smsbg.net')
    aQuote = quotes[index] #The index selects one of the 4 quotations in the RSS 
    params = urllib.urlencode({'MN1': countryCode, 'MN2': operatorCode, 'MN3': telphone, 'FROM': aQuote['title'], 
                               'SM': aQuote['quote']})
    params = params + '&num=0&sbm=%C8%E7%EF%F0%E0%F9%E0%ED%E5'
    print 'params: ' + params
    headers = {'Host': 'smsbg.net', 'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14',
                'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                'Keep-Alive': '300', 'Connection': 'keep-alive', 'Referer': 'http://smsbg.net/', 'Content-Type': 'application/x-www-form-urlencoded'}
    smsportal.request('get', 'http://smsbg.net/', '', headers)
    response = smsportal.getresponse()
    cookieHeader = response.getheader('Set-Cookie', '')
    print 'cookie: ' + cookieHeader
    headers = {'Host': 'smsbg.net', 'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14',
                'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
                'Keep-Alive': '300', 'Connection': 'keep-alive', 'Referer': 'http://smsbg.net/', 'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie': cookieHeader}
 
    smsportal.request('post', '/sms.php?' + params, '',  headers)
 
    response = smsportal.getresponse()
    print response.status, response.reason
    data = response.read()
    #print data
    smsportal.close()
 
initQuotes()
# a check for console argument, otherwise sends the second quote by default (well, why not?)
if(len(sys.argv) > 1):
    lookupSendQuote(int(sys.argv[1]))
else:
    lookupSendQuote(1)                       
 
Махнах личния си телефон, за да не получавам излишен спам :) За съжаление работи само с Мтел, а и спазваме ограничението от 160 символа, но все пак в повечето случаи работи достатъчно кадърно ;) С повече желание може да се пренапише като макрос и за други оператори, или да се ползва някой сървис за какъвто и да било оператор. Все пак, ако на някой му върши работа - пия Каменица :)
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • MySpace
  • Slashdot
  • Technorati
  • TwitThis
del.icio.us Digg DZone Facebook Google Google Reader Magnolia reddit SlashDot Technorati ReadMe.ru Dobavi.com Dao.bg Lubimi.com Ping.bg Pipe.bg Svejo.net Web-bg.com

Безподобния пост.

Related posts brought to you by Yet Another Related Posts Plugin.

Comments

2 Responses to “Quotation Crawler към мобилен”

  1. Konstantin on December 14th, 2008 16:59 [#]

    Спасибо классная статья ;)

  2. Сентенции в личната философия - блог мийм | Марио Пешев : Mario's cosy cavern on June 11th, 2011 9:13 [#]

    [...] Един от първите ми постове в блога бе за скрипт, който агрегира дневните цитати от сайт и ги праща като SMS. Повечето сайтове онлайн са [...]

Leave a Reply