def run(self): """ Request new tweets from the Twitter API. """ urls = { QUERY_TYPE_USER: ("http://api.twitter.com/1/statuses/" "user_timeline/%s.json?include_rts=true" % self.value.lstrip("@")), QUERY_TYPE_LIST: ("http://api.twitter.com/1/%s/statuses.json" "?include_rts=true" % self.value.lstrip("@").replace("/", "/lists/")), QUERY_TYPE_SEARCH: "http://search.twitter.com/search.json?q=%s" % quote(self.value.encode("utf-8")), } try: url = urls[self.type] except KeyError: return try: tweets = loads(urlopen(url).read()) except: return if self.type == "search": tweets = tweets["results"] for tweet_json in tweets: remote_id = str(tweet_json["id"]) tweet, created = self.tweets.get_or_create(remote_id=remote_id) if not created: continue if "retweeted_status" in tweet_json: user = tweet_json["user"] tweet.retweeter_user_name = user["screen_name"] tweet.retweeter_full_name = user["name"] tweet.retweeter_profile_image_url = user["profile_image_url"] tweet_json = tweet_json["retweeted_status"] if self.type == QUERY_TYPE_SEARCH: tweet.user_name = tweet_json["from_user"] tweet.full_name = tweet_json["from_user"] tweet.profile_image_url = tweet_json["profile_image_url"] date_format = "%a, %d %b %Y %H:%M:%S +0000" else: user = tweet_json["user"] tweet.user_name = user["screen_name"] tweet.full_name = user["name"] tweet.profile_image_url = user["profile_image_url"] date_format = "%a %b %d %H:%M:%S +0000 %Y" tweet.text = urlize(tweet_json["text"]) tweet.text = re_usernames.sub(replace_usernames, tweet.text) tweet.text = re_hashtags.sub(replace_hashtags, tweet.text) if getattr(settings, 'TWITTER_STRIP_HIGH_MULTIBYTE', False): chars = [ch for ch in tweet.text if ord(ch) < 0x800] tweet.text = ''.join(chars) d = datetime.strptime(tweet_json["created_at"], date_format) d -= timedelta(seconds=timezone) tweet.created_at = make_aware(d) tweet.save() self.interested = False self.save()
def run(self): """ Request new tweets from the Twitter API. """ if self.type == "user": url = ("http://api.twitter.com/1/statuses/user_timeline/%s.json?" "include_rts=true" % self.value.lstrip("@")) elif self.type == "list": url = ("http://api.twitter.com/1/%s/statuses.json?include_rts" "=true" % self.value.lstrip("@").replace("/", "/lists/")) elif self.type == "search": url = ("http://search.twitter.com/search.json?q=%s" % quote(self.value)) else: return try: tweets = loads(urlopen(url).read()) except: return if self.type == "search": tweets = tweets["results"] for tweet_json in tweets: tweet, created = self.tweets.get_or_create( remote_id=str(tweet_json["id"])) if not created: continue if "retweeted_status" in tweet_json: user = tweet_json["user"] tweet.retweeter_user_name = user["screen_name"] tweet.retweeter_full_name = user["name"] tweet.retweeter_profile_image_url = user["profile_image_url"] tweet_json = tweet_json["retweeted_status"] if self.type == "search": tweet.user_name = tweet_json["from_user"] tweet.full_name = tweet_json["from_user"] tweet.profile_image_url = tweet_json["profile_image_url"] date_format = "%a, %d %b %Y %H:%M:%S +0000" else: user = tweet_json["user"] tweet.user_name = user["screen_name"] tweet.full_name = user["name"] tweet.profile_image_url = user["profile_image_url"] date_format = "%a %b %d %H:%M:%S +0000 %Y" tweet.text = urlize(tweet_json["text"]) tweet.text = re_usernames.sub(replace_usernames, tweet.text) tweet.text = re_hashtags.sub(replace_hashtags, tweet.text) d = datetime.strptime(tweet_json["created_at"], date_format) d -= timedelta(seconds=timezone) tweet.created_at = make_aware(d) tweet.save() self.interested = False self.save()