<s> import urllib <EOL> import sqlite3 <EOL> import datetime <EOL> import tweepy <EOL> from xml . dom . minidom import parseString <EOL> from settings import * <EOL> class Item : <EOL> title = "<STR_LIT>" <EOL> link = "<STR_LIT>" <EOL> class CustomUrlOpener ( urllib . FancyURLopener ) : <EOL> version = "<STR_LIT>" <EOL> def getFeedData ( ) : <EOL> urllib . _urlopener = CustomUrlOpener ( ) <EOL> feedRequest = urllib . urlopen ( HACKER_NEWS_RSS_URL ) <EOL> feedString = feedRequest . read ( ) <EOL> feedRequest . close ( ) <EOL> return parseString ( feedString ) <EOL> def iterateItems ( feedData ) : <EOL> results = [ ] <EOL> for element in feedData . getElementsByTagName ( "<STR_LIT>" ) : <EOL> item = Item ( ) <EOL> item . title = element . getElementsByTagName ( "<STR_LIT:title>" ) [ <NUM_LIT:0> ] . childNodes [ <NUM_LIT:0> ] . data <EOL> item . link = element . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . childNodes [ <NUM_LIT:0> ] . data <EOL> item . commentLink = element . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . childNodes [ <NUM_LIT:0> ] . data <EOL> results . append ( item ) <EOL> return results <EOL> def isSchemaThere ( conn ) : <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" ) <EOL> if len ( cur . fetchall ( ) ) > <NUM_LIT:0> : <EOL> return True <EOL> return False <EOL> def createSchema ( conn ) : <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" ) <EOL> def isNewLink ( item ) : <EOL> conn = sqlite3 . connect ( LOCAL_LINK_DB ) <EOL> if isSchemaThere ( conn ) == False : <EOL> createSchema ( conn ) <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" , ( item . link , ) ) <EOL> isNew = len ( cur . fetchall ( ) ) == <NUM_LIT:0> <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> return isNew <EOL> def insertLink ( item ) : <EOL> conn = sqlite3 . connect ( LOCAL_LINK_DB ) <EOL> cur = conn . cursor ( ) <EOL> cur . execute ( "<STR_LIT>" , ( item . title , item . link , ) ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> def fixCommentLink ( link ) : <EOL> protocolEnd = <NUM_LIT:0> <EOL> if "<STR_LIT>" in link : <EOL> protocolEnd = link . index ( "<STR_LIT>" ) + len ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" + link [ protocolEnd : ] <EOL> else : <EOL> return "<STR_LIT>" + link . lstrip ( "<STR_LIT:/>" ) <EOL> def getTweetText ( item ) : <EOL> maxTitleTextLength = TWITTER_MAX - ( TCO_SHORT_URL_LENGTH + len ( DIVIDER_TEXT ) ) <EOL> if item . link < > item . commentLink : <EOL> maxTitleTextLength -= ( len ( COMMENT_TEXT ) + TCO_SHORT_URL_LENGTH ) <EOL> tweetText = item . title . strip ( "<STR_LIT>" ) [ : maxTitleTextLength ] + DIVIDER_TEXT + item . link <EOL> if item . link < > item . commentLink : <EOL> tweetText += COMMENT_TEXT + fixCommentLink ( item . commentLink ) <EOL> return tweetText <EOL> def submitTweet ( tweetText ) : <EOL> if DEBUG_MODE == False : <EOL> auth = tweepy . OAuthHandler ( TWITTER_CUSTOMER_KEY , TWITTER_CUSTOMER_SECRET ) <EOL> auth . set_access_token ( TWITTER_OAUTH_KEY , TWITTER_OAUTH_SECRET ) <EOL> api = tweepy . API ( auth ) <EOL> api . update_status ( tweetText ) <EOL> else : <EOL> print tweetText <EOL> for item in iterateItems ( getFeedData ( ) ) : <EOL> if isNewLink ( item ) : <EOL> insertLink ( item ) <EOL> tweetText = getTweetText ( item ) <EOL> submitTweet ( tweetText ) </s>
