def new_tweet_from_json(self, data): """ Converts a json string to a tweet instance. Args: data: json string containing twitter data Returns: twitter.models.status instance """ tweet = Status.NewFromJsonDict(data) if 'urls' in data: tweet.urls = [Url.NewFromJsonDict(u) for u in data['urls']] else: tweet.urls = [] if 'user_mentions' in data: tweet.user_mentions = [ User.NewFromJsonDict(u) for u in data['user_mentions'] ] else: tweet.user_mentions = [] if 'hashtags' in data: tweet.hashtags = [ Hashtag.NewFromJsonDict(h) for h in data['hashtags'] ] else: tweet.hashtags = [] return tweet
def NewFromJsonDict(data): """Create a new instance based on a JSON dict. Args: data: A JSON dict, as converted from the JSON in the twitter API Returns: A twitter.List instance """ if 'user' in data: user = User.NewFromJsonDict(data['user']) else: user = None return List(id=data.get('id', None), name=data.get('name', None), slug=data.get('slug', None), description=data.get('description', None), full_name=data.get('full_name', None), mode=data.get('mode', None), uri=data.get('uri', None), member_count=data.get('member_count', None), subscriber_count=data.get('subscriber_count', None), following=data.get('following', None), user=user)
def get_user_info(self, token): try: oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=token, http_url=TWITTER_CREDENTIALS_URL) oauth_request.sign_request(self.signature_method, self.consumer, token) return User.NewFromJsonDict(json.loads(oauth_response(oauth_request))) except: pass return None
def convert_dicts_in_status_to_obj(status: Status) -> Status: """Update each attribute of status with Twitter object""" keys_to_update = ["urls", "user", "user_mentions", "quoted_status"] for key in keys_to_update: if key == "urls": status.urls = [Url(**url) for url in status.__getattribute__(key)] elif key == "user": status.user = User(**status.__getattribute__(key)) elif key == "user_mentions": status.user_mentions = [ User(**user) for user in status.__getattribute__(key) ] elif key == "quoted_status": status.quoted_status = (convert_dicts_in_status_to_obj( status=Status(**status.__getattribute__(key))) if status.__getattribute__(key) else None) return status
def UsersLookup(twitter_ids): url = 'https://api.twitter.com/1/users/lookup.json?screen_name=%s' % twitter_ids #log.debug(url) f = urllib2.urlopen(url) json = f.read() f.close() data = _ParseAndCheckTwitter(json) return [User.NewFromJsonDict(u) for u in data]
def load(self): if not exists(self.filename): return False with open(self.filename, 'r') as infile: data = json.load(infile) self.followings = [User.NewFromJsonDict(f) for f in data['followings']] self.tweets = [Status.NewFromJsonDict(t) for t in data['tweets']] self.fetch_timestamp = data['fetch_timestamp'] return self.fetch_timestamp is not None
def GetUserInfo(self, url='https://twitter.com/account/verify_credentials.json'): '''Get user information from twitter Returns: Returns the twitter.User object ''' json = self._FetchUrl(url) data = simplejson.loads(json) self._CheckForTwitterError(data) return User.NewFromJsonDict(data)
def fetch_user(self, screen_name=None, id=None): assert (id is not None or screen_name is not None) userMongo = mongo.usersCollection.find( {'$or': [{ 'screen_name': screen_name }, { 'id': id }]}) if userMongo.count() == 0: screen_name = screen_name if screen_name is not None else id user = twitterInstance.fetchUser(screen_name) mongo.usersCollection.insert(user.__dict__) else: user = [user for user in userMongo] user = User(user[0]) return user
def GetUserss(self, user_id=None, screen_name=None, include_entities=False, return_json=False): url = '%s/users/show.json' % (self.base_url) parameters = {'include_entities': include_entities} if user_id: parameters['user_id'] = user_id elif screen_name: parameters['screen_name'] = screen_name else: raise TwitterError( "Specify at least one of user_id or screen_name.") resp = self._RequestUrl(url, 'GET', data=parameters) data = self._ParseAndCheckTwitter(resp.content.decode('utf-8')) if return_json: return data else: return User.NewFromJsonDict(data)
def random_picker(ids=CCAA.target, path="data/excahnge.json"): target = User(ids) exchange_list = target.follow_exchanger() random.shuffle(exchange_list) pick = exchange_list.copy() for _ in range(10): user = User(random.sample(set(pick), 1)[0]) user_info = user.user_info() if user_info is not None: ids, name = user_info["id"], user_info["name"] print(name) pick += user.follow_exchanger() random.shuffle(pick) for ids in pick[:50]: print("get ", ids, "'s tweet") user = User(ids) if not Twitter.api_use_start: Twitter.api_use_start = time.time() user.save_timeline() if Twitter.api_use_count >= 150: sleep_time = max(0, 900 - (time.time() - Twitter.api_use_start)) print("limit!! sleep : ", sleep_time, "s") time.sleep(sleep_time) Twitter.api_use_start = None Twitter.api_use_count = 0
def NewFromJsonDict(data): """Create a new instance based on a JSON dict. Args: data: A JSON dict, as converted from the JSON in the twitter API Returns: A twitter.Status instance """ if 'user' in data: from twitter import User # Have to do the import here to prevent cyclic imports in the __init__.py # file user = User.NewFromJsonDict(data['user']) else: user = None if 'retweeted_status' in data: retweeted_status = Status.NewFromJsonDict(data['retweeted_status']) else: retweeted_status = None if 'current_user_retweet' in data: current_user_retweet = data['current_user_retweet']['id'] else: current_user_retweet = None urls = None user_mentions = None hashtags = None media = None if 'entities' in data: if 'urls' in data['entities']: urls = [ Url.NewFromJsonDict(u) for u in data['entities']['urls'] ] if 'user_mentions' in data['entities']: from twitter import User user_mentions = [ User.NewFromJsonDict(u) for u in data['entities']['user_mentions'] ] if 'hashtags' in data['entities']: hashtags = [ Hashtag.NewFromJsonDict(h) for h in data['entities']['hashtags'] ] if 'media' in data['entities']: media = data['entities']['media'] else: media = [] # the new extended entities if 'extended_entities' in data: if 'media' in data['extended_entities']: media = [m for m in data['extended_entities']['media']] return Status( created_at=data.get('created_at', None), favorited=data.get('favorited', None), favorite_count=data.get('favorite_count', None), id=data.get('id', None), text=data.get('text', None), location=data.get('location', None), lang=data.get('lang', None), in_reply_to_screen_name=data.get('in_reply_to_screen_name', None), in_reply_to_user_id=data.get('in_reply_to_user_id', None), in_reply_to_status_id=data.get('in_reply_to_status_id', None), truncated=data.get('truncated', None), retweeted=data.get('retweeted', None), source=data.get('source', None), user=user, urls=urls, user_mentions=user_mentions, hashtags=hashtags, media=media, geo=data.get('geo', None), place=data.get('place', None), coordinates=data.get('coordinates', None), contributors=data.get('contributors', None), retweeted_status=retweeted_status, current_user_retweet=current_user_retweet, retweet_count=data.get('retweet_count', None), possibly_sensitive=data.get('possibly_sensitive', None), scopes=data.get('scopes', None), withheld_copyright=data.get('withheld_copyright', None), withheld_in_countries=data.get('withheld_in_countries', None), withheld_scope=data.get('withheld_scope', None))
False, 'retweeted_status': None, 'scopes': None, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'text': None, 'truncated': False, 'tweet_mode': 'extended', 'urls': [], 'user': User(ID=925031723233972224, ScreenName='moabridge'), 'user_mentions': [], 'withheld_copyright': None, 'withheld_in_countries': None, 'withheld_scope': None } retweet_without_comment = { '_json': { 'contributors': None, 'coordinates': None,