def main(memc, op, username = '', password = '', newfriendname = ''): cache = Cache(memc) if username and password: api = twitter.Api(username=username, password=password) else: api = twitter.Api() if op == 'save': status = get_twitter_status(cache, api) save_chains(cache, status) elif op == 'load_user': status = load_user(cache, api, newfriendname) save_chains(cache, status) elif op == 'commands': process_commands(cache, api) elif op == 'tweet': while True: x = make_tweet(cache) if x: print 'tweeting: %r' % x try: api.PostUpdate(x) time.sleep(60*60) # post one per hour except urllib2.HTTPError, e: print "Couldn't tweet, will retry", e time.sleep(120) # try again in a couple of minutes
def process_commands(cache, api): while True: try: dms = api.GetDirectMessages() except urllib2.HTTPError, e: print "Couldn't get direct messages, will retry", e time.sleep(120) continue for dm in cache.seen_iterator(dms, _seen_key): follow_cmd_match = follow_cmd_re.match(dm.text.lower()) tweetme_cmd_match = not follow_cmd_match and tweetme_cmd_re.match(dm.text.lower()) if follow_cmd_match: # one of our friends has given us the command to # follow someone else newfriendname = follow_cmd_match.group(1) try: print '%r has instructed me to follow %r' % (dm.sender_screen_name, newfriendname) api.CreateFriendship(newfriendname) except (ValueError, twitter.TwitterError, urllib2.HTTPError), e: print "couldn't follow %r" % (newfriendname,) else: status = load_user(cache, api, newfriendname) save_chains(cache, status) elif tweetme_cmd_match: # someone wants us to send them a one-time tweet tweet = make_tweet(cache) if tweet: print 'Tweeting to %r: %r' % (dm.sender_screen_name, tweet) try: api.PostDirectMessage(dm.sender_screen_name, tweet) except (ValueError, twitter.TwitterError, urllib2.HTTPError), e: print "couldn't tweetme to %r (%r)" % (dm.sender_screen_name, tweet)
def update(cache): url = urls.pop(0) urls.append(url) news = parse_feed(cache, url) save_chains(cache, news) return True
def main(memc): cache = Cache(memc) comments = get_reddit_comments(cache) save_chains(cache, comments)