Jump to content
hades

[Tutorial] [PYTHON] Messing around with Twitter API

Recommended Posts

Posted (edited)

De urmatoarea problema m-am lovit recent, lucrand putin cu libraria Python Tweepy, folosita pentru request-urile prin Twitter API.

In momentul efectuarii unui search pe twitter prin API, dupa un anumit keyword, rezultatele stocate in liste vor reprezenta obiecte de tip tweepy. Dar, in cazul in care doriti sa manipulati datele din perspectiva unui fron-end developer, v-ar ajuta mai mult ca sigur un json cu acestea.

Ei bine, aceste obiecte ar trebui transformate in dictionare, initial, iar apoi generat jsonul. Problema e ca, nu dispun de metoda 'to_dict', deci ar trebui cautat un workaround.

>>> dir(tweets[0])
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api', 'author', 'contributors', 'coordinates', 'created_at', 'destroy', 'entities', 'favorite', 'favorite_count', 'favorited', 'first_parse', 'geo', 'id', 'id_str', 'in_reply_to_screen_name', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'json', 'lang', 'metadata', 'parse', 'parse_list', 'place', 'possibly_sensitive', 'retweet', 'retweet_count', 'retweeted', 'retweeted_status', 'retweets', 'source', 'source_url', 'text', 'truncated', 'user']

Pentru a facilita accesul la continului unui tweet, vom scrie un mic patch pentru parsare. Asa ca, intr-un fisier patch.py vom avea:


import tweepy
import json

classmethod
def parse(cls, api, raw):
status = cls.first_parse(api, raw)
setattr(status, 'json', json.dumps(raw))
return status

tweepy.models.Status.first_parse = tweepy.models.Status.parse
tweepy.models.Status.parse = parse

Apoi, in orice fisier in care vom avea requesturi prin API-ul de la twitter, vom putea importa acest patch, pentru a avea acces la tweet.json; Metoda care ne va returna un json cu toate detaliile despre tweet-ul respectiv.


import patch
import tweepy

auth = tweepy.OAuthHandler(client_secret, client_key)
auth.set_acces_token(token, token_secret)
client = tweepy.API(auth)
tweets = client.search('keyword')
for tweet in tweets
print tweet.json

Functionalitatile acestui workaround sunt destule. Mai ales daca vreti s-aveti de-a face cu scrapere pe retelele sociale.

Later Edit 1: Classmethod-ul se foloseste ca si decorator cu '@' in fata. Dar vad ca daac folosesc asa intre tag-urile CODE, imi vede ca si mention.

Later Edi 2: access token, access token secret, client secret si client key se vor lua din aplicatia pe care o creati pe contul de twitter developer.

Edited by hades

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...