def run(self): self.logger.debug("Fetching tweets...") # fetch the tw users' tweets for tw_user in TwitterUser.select(): if tw_user.subscriptions.count() == 0: self.logger.debug( "Skipping {} because 0 subscriptions".format(tw_user.screen_name)) continue try: if tw_user.last_tweet_id == 0: # get just the latest tweet self.logger.debug( "Fetching the latest tweet from {}".format(tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, count=1) else: # get the fresh tweets self.logger.debug( "Fetching new tweets from {}".format(tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, since_id=tw_user.last_tweet_id) except tweepy.error.TweepError: self.logger.debug( "Whoops, I couldn't get tweets from {}!".format(tw_user.screen_name)) continue for tweet in tweets: self.logger.debug("Got tweet: {}".format(tweet.text)) # Check if tweet contains media, else check if it contains a link to an image extensions = ('.jpg', '.jpeg', '.png', '.gif') pattern = '[(%s)]$' % ')('.join(extensions) photo_url = '' if 'media' in tweet.entities: photo_url = tweet.entities['media'][0]['media_url_https'] else: for url in tweet.entities['urls']: if re.search(pattern, url['expanded_url']): photo_url = url['expanded_url'] break if photo_url: self.logger.debug("Found media URL in tweet: " + photo_url) tw, _created = Tweet.get_or_create( tw_id=tweet.id, text=html.unescape(tweet.text), created_at=tweet.created_at, twitter_user=tw_user, photo_url=photo_url, ) # send the new tweets to subscribers for s in Subscription.select(): # are there new tweets? send em all! self.logger.debug( "Checking subscription {} {}".format(s.tg_chat.chat_id, s.tw_user.screen_name)) if s.last_tweet_id == 0: # didn't receive any tweet yet try: tw = (s.tw_user.tweets.select() .order_by(Tweet.tw_id.desc()) .limit(1))[0] self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = tw.tw_id s.save() except IndexError: self.logger.debug("No tweets available yet on {}".format(s.tw_user.screen_name)) continue if s.tw_user.last_tweet_id > s.last_tweet_id: self.logger.debug("Some fresh tweets here!") for tw in (s.tw_user.tweets.select() .where(Tweet.tw_id > s.last_tweet_id) .order_by(Tweet.tw_id.desc()) ): self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = s.tw_user.last_tweet_id s.save() continue self.logger.debug("No new tweets here.")
def run(self): self.logger.debug("Fetching tweets...") # fetch the tw users' tweets tw_users = (TwitterUser.select() .join(Subscription) .group_by(TwitterUser) .order_by(TwitterUser.last_fetched)) for tw_user in tw_users: try: if tw_user.last_tweet_id == 0: # get just the latest tweet self.logger.debug( "Fetching latest tweet by {}".format(tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, count=1) else: # get the fresh tweets self.logger.debug( "Fetching new tweets from {}".format(tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, since_id=tw_user.last_tweet_id) tw_user.last_fetched = datetime.now() tw_user.save() except tweepy.error.TweepError as e: sc = e.response.status_code if sc == 429: self.logger.debug("- Hit ratelimit, breaking.") break if sc == 401: self.logger.debug("- Protected tweets here.") continue if sc == 404: self.logger.debug("- 404? Maybe screen name changed?") continue self.logger.debug( "- Unknown exception, Status code {}".format(sc)) continue for tweet in tweets: self.logger.debug("- Got tweet: {}".format(tweet.text)) # Check if tweet contains media, else check if it contains a link to an image extensions = ('.jpg', '.jpeg', '.png', '.gif') pattern = '[(%s)]$' % ')('.join(extensions) photo_url = '' if 'media' in tweet.entities: photo_url = tweet.entities['media'][0]['media_url_https'] else: for url in tweet.entities['urls']: if re.search(pattern, url['expanded_url']): photo_url = url['expanded_url'] break if photo_url: self.logger.debug("- - Found media URL in tweet: " + photo_url) tw, _created = Tweet.get_or_create( tw_id=tweet.id, text=html.unescape(tweet.text), created_at=tweet.created_at, twitter_user=tw_user, photo_url=photo_url, ) # send the new tweets to subscribers for s in Subscription.select(): # are there new tweets? send em all! self.logger.debug( "Checking subscription {} {}".format(s.tg_chat.chat_id, s.tw_user.screen_name)) if s.last_tweet_id == 0: # didn't receive any tweet yet try: tw = (s.tw_user.tweets.select() .order_by(Tweet.tw_id.desc()) .limit(1))[0] self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = tw.tw_id s.save() except IndexError: self.logger.debug("- No tweets available yet on {}".format(s.tw_user.screen_name)) continue if s.tw_user.last_tweet_id > s.last_tweet_id: self.logger.debug("- Some fresh tweets here!") for tw in (s.tw_user.tweets.select() .where(Tweet.tw_id > s.last_tweet_id) .order_by(Tweet.tw_id.desc()) ): self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = s.tw_user.last_tweet_id s.save() continue self.logger.debug("- No new tweets here.")
def run(self): self.logger.debug("Fetching tweets...") # fetch the tw users' tweets for tw_user in TwitterUser.select(): if tw_user.subscriptions.count() == 0: self.logger.debug("Skipping {} because 0 subscriptions".format( tw_user.screen_name)) continue try: if tw_user.last_tweet_id == 0: # get just the latest tweet self.logger.debug( "Fetching the latest tweet from {}".format( tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, count=1) else: # get the fresh tweets self.logger.debug("Fetching new tweets from {}".format( tw_user.screen_name)) tweets = self.bot.tw.user_timeline( screen_name=tw_user.screen_name, since_id=tw_user.last_tweet_id) except tweepy.error.TweepError: self.logger.debug( "Whoops, I couldn't get tweets from {}!".format( tw_user.screen_name)) continue for tweet in tweets: self.logger.debug("Got tweet: {}".format(tweet.text)) # Check if tweet contains media, else check if it contains a link to an image extensions = ('.jpg', '.jpeg', '.png', '.gif') pattern = '[(%s)]$' % ')('.join(extensions) photo_url = '' if 'media' in tweet.entities: photo_url = tweet.entities['media'][0]['media_url_https'] else: for url in tweet.entities['urls']: if re.search(pattern, url['expanded_url']): photo_url = url['expanded_url'] break if photo_url: self.logger.debug("Found media URL in tweet: " + photo_url) tw, _created = Tweet.get_or_create( tw_id=tweet.id, text=html.unescape(tweet.text), created_at=tweet.created_at, twitter_user=tw_user, photo_url=photo_url, ) # send the new tweets to subscribers for s in Subscription.select(): # are there new tweets? send em all! self.logger.debug("Checking subscription {} {}".format( s.tg_chat.chat_id, s.tw_user.screen_name)) if s.last_tweet_id == 0: # didn't receive any tweet yet try: tw = (s.tw_user.tweets.select().order_by( Tweet.tw_id.desc()).limit(1))[0] self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = tw.tw_id s.save() except IndexError: self.logger.debug("No tweets available yet on {}".format( s.tw_user.screen_name)) continue if s.tw_user.last_tweet_id > s.last_tweet_id: self.logger.debug("Some fresh tweets here!") for tw in (s.tw_user.tweets.select().where( Tweet.tw_id > s.last_tweet_id).order_by( Tweet.tw_id.desc())): self.bot.send_tweet(s.tg_chat, tw) # save the latest tweet sent on this subscription s.last_tweet_id = s.tw_user.last_tweet_id s.save() continue self.logger.debug("No new tweets here.")