Jugando con la API de Twitter [Parte 2]


Hola no es habitual que escriba sobre el mismo tema tan seguido pero lo haré por lo menos en esta serie “Jugando con las API de las redes sociales”, y próximamente “Jugando con la API  de SPN” para que aquellas personas que les gustan algunos números que no salen en los periódicos tengan acceso a estos… claro voy a terminar primero con la mayor parte de las redes sociales antes de comenzar otra serie.

En esta entrada vamos a intentar refinar un poco la información que obtuvimos en la entrada anterior, vamos a usar algunas rutinas de json y vamos a usar también python para ir depurando nuestra información, ademas vamos a tener un breve acercamiento a sqlite3 para obtener una base de datos que realmente no hace nada pero nos ayudara a ir entendiendo la información y guardarla para reutilizarla, mas adelante vamos a crear bases de datos mas elegantes y funcionales donde no vamos a tener q repetir los datos una vez si no 1 sola vez y de diferentes modos 😀

En el siguiente ejemplo vamos a implementar una rutina de JSON para formatear los datos que hemos recibido anteriormente y asi tener una mirada mas sencilla a estos :D, puedes colocarle ej2_twitterAPI_json.py:

 

import twitter, json
# Lo primero que debemos hacer es ir a http://dev.twitter.com/apps/new y crear una app
#puedes ver https://dev.twitter.com/docs/auth/oauth para mas informacion
#las credenciales que necesitamos debemos copiarlas de XXXXXXXXXXXXXXXXXXXXXX y usarlas tal como se muestrasn en las imagenes de esta entrada http://wp.me/p3nJhK-ev

CONSUMER_KEY = 'Datos de A'

CONSUMER_SECRET = 'Datos de B'

OAUTH_TOKEN = 'Datos de C'

OAUTH_TOKEN_SECRET = 'Datos de D'


auth = twitter.oauth.OAuth( OAUTH_TOKEN , OAUTH_TOKEN_SECRET, CONSUMER_KEY , CONSUMER_SECRET )

twitter_api = twitter.Twitter(auth=auth)

worl_trends = twitter_api.trends.place(_id=WORL_WOE_ID)

us_trends = twitter_api.trends.place(_id=US_WOE_ID)

print json.dumps(worl_trends, indent =4)
print 
print json.dumps(us_trends, indent =4)

Lo nuevo son estas lineas, estas lineas usan rutinas de JSON para formatear el Diccionario que nos devolvía el programa en la entrada anterior, el 4 no es mas que el identado con el que queremos ver la información 😀

 

print json.dumps(worl_trends, indent =4)
print 
print json.dumps(us_trends, indent =4)

Si corremos el código anterior obtenemos la salida siguiente:

 

[
    {
        "created_at": "2016-08-05T19:13:35Z", 
        "trends": [
            {
                "url": "http://twitter.com/search?q=%22BEM+FEITO+JUSTIN%22", 
                "query": "%22BEM+FEITO+JUSTIN%22", 
                "tweet_volume": 55828, 
                "name": "BEM FEITO JUSTIN", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23CerimoniaDeAbertura", 
                "query": "%23CerimoniaDeAbertura", 
                "tweet_volume": 38173, 
                "name": "#CerimoniaDeAbertura", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D8%A7%D9%84%D8%A7%D8%AA%D8%AD%D8%A7%D8%AF_%D8%A7%D9%84%D9%86%D8%B5%D8%B1", 
                "query": "%23%D8%A7%D9%84%D8%A7%D8%AA%D8%AD%D8%A7%D8%AF_%D8%A7%D9%84%D9%86%D8%B5%D8%B1", 
                "tweet_volume": 67776, 
                "name": "#\u0627\u0644\u0627\u062a\u062d\u0627\u062f_\u0627\u0644\u0646\u0635\u0631", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23OpeningCeremony", 
                "query": "%23OpeningCeremony", 
                "tweet_volume": 49306, 
                "name": "#OpeningCeremony", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D8%A7%D9%84%D8%B1%D8%A7%D8%AA%D8%A8_%D9%85%D8%A7_%D9%8A%D9%83%D9%81%D9%8A_%D8%A7%D9%84%D9%85%D9%8A%D9%83_%D8%A7%D8%A8", 
                "query": "%23%D8%A7%D9%84%D8%B1%D8%A7%D8%AA%D8%A8_%D9%85%D8%A7_%D9%8A%D9%83%D9%81%D9%8A_%D8%A7%D9%84%D9%85%D9%8A%D9%83_%D8%A7%D8%A8", 
                "tweet_volume": 11458, 
                "name": "#\u0627\u0644\u0631\u0627\u062a\u0628_\u0645\u0627_\u064a\u0643\u0641\u064a_\u0627\u0644\u0645\u064a\u0643_\u0627\u0628", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23PaulONeal", 
                "query": "%23PaulONeal", 
                "tweet_volume": 29285, 
                "name": "#PaulONeal", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%EB%AA%A8%EC%97%90%ED%99%94", 
                "query": "%EB%AA%A8%EC%97%90%ED%99%94", 
                "tweet_volume": null, 
                "name": "\ubaa8\uc5d0\ud654", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=Pel%C3%A9", 
                "query": "Pel%C3%A9", 
                "tweet_volume": 49187, 
                "name": "Pel\u00e9", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Are+Camila+and+Lauren%22", 
                "query": "%22Are+Camila+and+Lauren%22", 
                "tweet_volume": 13455, 
                "name": "Are Camila and Lauren", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Ba%C5%9Fkomutan+Y%C3%BCceATAT%C3%9CRK%22", 
                "query": "%22Ba%C5%9Fkomutan+Y%C3%BCceATAT%C3%9CRK%22", 
                "tweet_volume": null, 
                "name": "Ba\u015fkomutan Y\u00fcceATAT\u00dcRK", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22%C3%9Clk%C3%BCc%C3%BCOlmak+%C5%9Eereftir%22", 
                "query": "%22%C3%9Clk%C3%BCc%C3%BCOlmak+%C5%9Eereftir%22", 
                "tweet_volume": 10797, 
                "name": "\u00dclk\u00fcc\u00fcOlmak \u015eereftir", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%EC%9E%90%EA%B8%B0%ED%88%AC%EC%98%81", 
                "query": "%EC%9E%90%EA%B8%B0%ED%88%AC%EC%98%81", 
                "tweet_volume": null, 
                "name": "\uc790\uae30\ud22c\uc601", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23DeberiaSerDeporteOlimpico", 
                "query": "%23DeberiaSerDeporteOlimpico", 
                "tweet_volume": 11589, 
                "name": "#DeberiaSerDeporteOlimpico", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23HalilKantarc%C4%B1Yetimhanesi", 
                "query": "%23HalilKantarc%C4%B1Yetimhanesi", 
                "tweet_volume": null, 
                "name": "#HalilKantarc\u0131Yetimhanesi", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MentalHealthCareSoPoor", 
                "query": "%23MentalHealthCareSoPoor", 
                "tweet_volume": 13565, 
                "name": "#MentalHealthCareSoPoor", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23BuenViernes", 
                "query": "%23BuenViernes", 
                "tweet_volume": 42202, 
                "name": "#BuenViernes", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NormaniIsImportant", 
                "query": "%23NormaniIsImportant", 
                "tweet_volume": 15562, 
                "name": "#NormaniIsImportant", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%C5%9EehidineSahip%C3%87%C4%B1k", 
                "query": "%23%C5%9EehidineSahip%C3%87%C4%B1k", 
                "tweet_volume": 41874, 
                "name": "#\u015eehidineSahip\u00c7\u0131k", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23ClickMyFingers", 
                "query": "%23ClickMyFingers", 
                "tweet_volume": 19458, 
                "name": "#ClickMyFingers", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D0%A2%D0%B2%D0%B8%D1%82%D1%82%D0%B5%D1%80%D1%81%D0%BA%D0%B8%D0%B9%D0%98%D0%B7%D0%A2%D0%B2%D0%BE%D0%B5%D0%B3%D0%BE%D0%93%D0%BE%D1%80%D0%BE%D0%B4%D0%B0", 
                "query": "%23%D0%A2%D0%B2%D0%B8%D1%82%D1%82%D0%B5%D1%80%D1%81%D0%BA%D0%B8%D0%B9%D0%98%D0%B7%D0%A2%D0%B2%D0%BE%D0%B5%D0%B3%D0%BE%D0%93%D0%BE%D1%80%D0%BE%D0%B4%D0%B0", 
                "tweet_volume": null, 
                "name": "#\u0422\u0432\u0438\u0442\u0442\u0435\u0440\u0441\u043a\u0438\u0439\u0418\u0437\u0422\u0432\u043e\u0435\u0433\u043e\u0413\u043e\u0440\u043e\u0434\u0430", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23VitorPereiraVarsaBizYokuz", 
                "query": "%23VitorPereiraVarsaBizYokuz", 
                "tweet_volume": 29927, 
                "name": "#VitorPereiraVarsaBizYokuz", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D8%B5%D8%AD%D9%8A%D9%81%D9%87_%D8%A7%D9%84%D8%B1%D9%8A%D8%A7%D8%B6_%D8%AA%D8%AF%D8%B9%D9%85_%D8%A7%D9%84%D8%A7%D8%B3%D8%AF", 
                "query": "%23%D8%B5%D8%AD%D9%8A%D9%81%D9%87_%D8%A7%D9%84%D8%B1%D9%8A%D8%A7%D8%B6_%D8%AA%D8%AF%D8%B9%D9%85_%D8%A7%D9%84%D8%A7%D8%B3%D8%AF", 
                "tweet_volume": 52905, 
                "name": "#\u0635\u062d\u064a\u0641\u0647_\u0627\u0644\u0631\u064a\u0627\u0636_\u062a\u062f\u0639\u0645_\u0627\u0644\u0627\u0633\u062f", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FridayFeeling", 
                "query": "%23FridayFeeling", 
                "tweet_volume": 85572, 
                "name": "#FridayFeeling", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23CuentaWYLS", 
                "query": "%23CuentaWYLS", 
                "tweet_volume": 27538, 
                "name": "#CuentaWYLS", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FlashbackFriday", 
                "query": "%23FlashbackFriday", 
                "tweet_volume": 23282, 
                "name": "#FlashbackFriday", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MarioBautistaEnHoy", 
                "query": "%23MarioBautistaEnHoy", 
                "tweet_volume": 24315, 
                "name": "#MarioBautistaEnHoy", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D9%83%D9%85_%D8%B9%D9%86%D8%AF%D9%83_%D8%B1%D9%82%D9%85_%D8%A8%D9%86%D8%AA_%D9%85%D9%86_%D8%AA%D9%88%D9%8A%D8%AA%D8%B1", 
                "query": "%23%D9%83%D9%85_%D8%B9%D9%86%D8%AF%D9%83_%D8%B1%D9%82%D9%85_%D8%A8%D9%86%D8%AA_%D9%85%D9%86_%D8%AA%D9%88%D9%8A%D8%AA%D8%B1", 
                "tweet_volume": null, 
                "name": "#\u0643\u0645_\u0639\u0646\u062f\u0643_\u0631\u0642\u0645_\u0628\u0646\u062a_\u0645\u0646_\u062a\u0648\u064a\u062a\u0631", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23A41MesesDeTuSiembraComandante", 
                "query": "%23A41MesesDeTuSiembraComandante", 
                "tweet_volume": 39635, 
                "name": "#A41MesesDeTuSiembraComandante", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MakeMeVideo", 
                "query": "%23MakeMeVideo", 
                "tweet_volume": null, 
                "name": "#MakeMeVideo", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FCKH96", 
                "query": "%23FCKH96", 
                "tweet_volume": null, 
                "name": "#FCKH96", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23TiroConArco", 
                "query": "%23TiroConArco", 
                "tweet_volume": null, 
                "name": "#TiroConArco", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23WhenIStressOutI", 
                "query": "%23WhenIStressOutI", 
                "tweet_volume": null, 
                "name": "#WhenIStressOutI", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MinhaInfanciaFoiBoaPq", 
                "query": "%23MinhaInfanciaFoiBoaPq", 
                "tweet_volume": 22352, 
                "name": "#MinhaInfanciaFoiBoaPq", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23Felsefeye31Kadro", 
                "query": "%23Felsefeye31Kadro", 
                "tweet_volume": null, 
                "name": "#Felsefeye31Kadro", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NABJNAHJ16", 
                "query": "%23NABJNAHJ16", 
                "tweet_volume": 18229, 
                "name": "#NABJNAHJ16", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23LaFieraGranFinal", 
                "query": "%23LaFieraGranFinal", 
                "tweet_volume": null, 
                "name": "#LaFieraGranFinal", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23VisibleWomen", 
                "query": "%23VisibleWomen", 
                "tweet_volume": null, 
                "name": "#VisibleWomen", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23BlogHer16", 
                "query": "%23BlogHer16", 
                "tweet_volume": null, 
                "name": "#BlogHer16", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23110MilletvekiliAran%C4%B1yor", 
                "query": "%23110MilletvekiliAran%C4%B1yor", 
                "tweet_volume": 12648, 
                "name": "#110MilletvekiliAran\u0131yor", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D8%B3%D8%A7%D8%B9%D9%87_%D8%A7%D8%B3%D8%AA%D8%AC%D8%A7%D8%A8%D9%87", 
                "query": "%23%D8%B3%D8%A7%D8%B9%D9%87_%D8%A7%D8%B3%D8%AA%D8%AC%D8%A7%D8%A8%D9%87", 
                "tweet_volume": 86440, 
                "name": "#\u0633\u0627\u0639\u0647_\u0627\u0633\u062a\u062c\u0627\u0628\u0647", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23RendUnFilmX", 
                "query": "%23RendUnFilmX", 
                "tweet_volume": null, 
                "name": "#RendUnFilmX", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23QueremosRock", 
                "query": "%23QueremosRock", 
                "tweet_volume": null, 
                "name": "#QueremosRock", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23LinhaDACSophiaAbrah%C3%A3o", 
                "query": "%23LinhaDACSophiaAbrah%C3%A3o", 
                "tweet_volume": null, 
                "name": "#LinhaDACSophiaAbrah\u00e3o", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23SiTodoSaleBien", 
                "query": "%23SiTodoSaleBien", 
                "tweet_volume": null, 
                "name": "#SiTodoSaleBien", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23QuerUmConselho", 
                "query": "%23QuerUmConselho", 
                "tweet_volume": null, 
                "name": "#QuerUmConselho", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23DiaDeLaCerveza", 
                "query": "%23DiaDeLaCerveza", 
                "tweet_volume": 32781, 
                "name": "#DiaDeLaCerveza", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23SanaBirS%C4%B1rVerece%C4%9Fim", 
                "query": "%23SanaBirS%C4%B1rVerece%C4%9Fim", 
                "tweet_volume": null, 
                "name": "#SanaBirS\u0131rVerece\u011fim", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23InternationalBeerDay", 
                "query": "%23InternationalBeerDay", 
                "tweet_volume": 19066, 
                "name": "#InternationalBeerDay", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23SuicideSquadRejects", 
                "query": "%23SuicideSquadRejects", 
                "tweet_volume": null, 
                "name": "#SuicideSquadRejects", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23%D9%8A%D8%A7%D8%B3%D8%B1_%D8%A7%D9%84%D9%82%D8%AD%D8%B7%D8%A7%D9%86%D9%8A_11_%D8%B3%D9%86%D9%87_%D9%81%D9%8A_%D8%A7%D9%84%D9%87%D9%84%D8%A7%D9%84", 
                "query": "%23%D9%8A%D8%A7%D8%B3%D8%B1_%D8%A7%D9%84%D9%82%D8%AD%D8%B7%D8%A7%D9%86%D9%8A_11_%D8%B3%D9%86%D9%87_%D9%81%D9%8A_%D8%A7%D9%84%D9%87%D9%84%D8%A7%D9%84", 
                "tweet_volume": null, 
                "name": "#\u064a\u0627\u0633\u0631_\u0627\u0644\u0642\u062d\u0637\u0627\u0646\u064a_11_\u0633\u0646\u0647_\u0641\u064a_\u0627\u0644\u0647\u0644\u0627\u0644", 
                "promoted_content": null
            }
        ], 
        "as_of": "2016-08-05T19:18:30Z", 
        "locations": [
            {
                "woeid": 1, 
                "name": "Worldwide"
            }
        ]
    }
]

[
    {
        "created_at": "2016-08-05T19:13:35Z", 
        "trends": [
            {
                "url": "http://twitter.com/search?q=%23PaulONeal", 
                "query": "%23PaulONeal", 
                "tweet_volume": 29285, 
                "name": "#PaulONeal", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23OpeningCeremony", 
                "query": "%23OpeningCeremony", 
                "tweet_volume": 49306, 
                "name": "#OpeningCeremony", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FridayFeeling", 
                "query": "%23FridayFeeling", 
                "tweet_volume": 85572, 
                "name": "#FridayFeeling", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NABJNAHJ16", 
                "query": "%23NABJNAHJ16", 
                "tweet_volume": 18229, 
                "name": "#NABJNAHJ16", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FlashbackFriday", 
                "query": "%23FlashbackFriday", 
                "tweet_volume": 23282, 
                "name": "#FlashbackFriday", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Mark+Teixeira%22", 
                "query": "%22Mark+Teixeira%22", 
                "tweet_volume": 18881, 
                "name": "Mark Teixeira", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22I+Ran+the+C.I.A.+Now%22", 
                "query": "%22I+Ran+the+C.I.A.+Now%22", 
                "tweet_volume": 18357, 
                "name": "I Ran the C.I.A. Now", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Purple+Lamborghini%22", 
                "query": "%22Purple+Lamborghini%22", 
                "tweet_volume": 14316, 
                "name": "Purple Lamborghini", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Nate+Parker%22", 
                "query": "%22Nate+Parker%22", 
                "tweet_volume": null, 
                "name": "Nate Parker", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22ON+SALE+NOW%22", 
                "query": "%22ON+SALE+NOW%22", 
                "tweet_volume": 10186, 
                "name": "ON SALE NOW", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%22Charlie+Strong%22", 
                "query": "%22Charlie+Strong%22", 
                "tweet_volume": null, 
                "name": "Charlie Strong", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23WhenIStressOutI", 
                "query": "%23WhenIStressOutI", 
                "tweet_volume": null, 
                "name": "#WhenIStressOutI", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23BlogHer16", 
                "query": "%23BlogHer16", 
                "tweet_volume": null, 
                "name": "#BlogHer16", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23VisibleWomen", 
                "query": "%23VisibleWomen", 
                "tweet_volume": null, 
                "name": "#VisibleWomen", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23SuicideSquadRejects", 
                "query": "%23SuicideSquadRejects", 
                "tweet_volume": null, 
                "name": "#SuicideSquadRejects", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23InternationalBeerDay", 
                "query": "%23InternationalBeerDay", 
                "tweet_volume": 19066, 
                "name": "#InternationalBeerDay", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FlippinGinjaIsOverParty", 
                "query": "%23FlippinGinjaIsOverParty", 
                "tweet_volume": null, 
                "name": "#FlippinGinjaIsOverParty", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MentalHealthCareSoPoor", 
                "query": "%23MentalHealthCareSoPoor", 
                "tweet_volume": 13565, 
                "name": "#MentalHealthCareSoPoor", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23LastTimeFrankDroppedAnAlbum", 
                "query": "%23LastTimeFrankDroppedAnAlbum", 
                "tweet_volume": 11511, 
                "name": "#LastTimeFrankDroppedAnAlbum", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FMLA23", 
                "query": "%23FMLA23", 
                "tweet_volume": null, 
                "name": "#FMLA23", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23newtanavideo", 
                "query": "%23newtanavideo", 
                "tweet_volume": null, 
                "name": "#newtanavideo", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23ReadWhereYouAre", 
                "query": "%23ReadWhereYouAre", 
                "tweet_volume": null, 
                "name": "#ReadWhereYouAre", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23ShopEdmond", 
                "query": "%23ShopEdmond", 
                "tweet_volume": null, 
                "name": "#ShopEdmond", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23DolansMakeMeConfident", 
                "query": "%23DolansMakeMeConfident", 
                "tweet_volume": null, 
                "name": "#DolansMakeMeConfident", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NameABandsNextTour", 
                "query": "%23NameABandsNextTour", 
                "tweet_volume": null, 
                "name": "#NameABandsNextTour", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MakeMeVideo", 
                "query": "%23MakeMeVideo", 
                "tweet_volume": null, 
                "name": "#MakeMeVideo", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23seriousacademic", 
                "query": "%23seriousacademic", 
                "tweet_volume": null, 
                "name": "#seriousacademic", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23DAPGO", 
                "query": "%23DAPGO", 
                "tweet_volume": 45302, 
                "name": "#DAPGO", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23VRLA", 
                "query": "%23VRLA", 
                "tweet_volume": null, 
                "name": "#VRLA", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23hannibalmontanabalexperience", 
                "query": "%23hannibalmontanabalexperience", 
                "tweet_volume": null, 
                "name": "#hannibalmontanabalexperience", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23VRAChat", 
                "query": "%23VRAChat", 
                "tweet_volume": null, 
                "name": "#VRAChat", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23jobsreport", 
                "query": "%23jobsreport", 
                "tweet_volume": 12140, 
                "name": "#jobsreport", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23HyperMasculAZNs", 
                "query": "%23HyperMasculAZNs", 
                "tweet_volume": null, 
                "name": "#HyperMasculAZNs", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MLGOrlando", 
                "query": "%23MLGOrlando", 
                "tweet_volume": null, 
                "name": "#MLGOrlando", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NewMusicFriday", 
                "query": "%23NewMusicFriday", 
                "tweet_volume": null, 
                "name": "#NewMusicFriday", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23MansionTonight", 
                "query": "%23MansionTonight", 
                "tweet_volume": null, 
                "name": "#MansionTonight", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23RememberOakCreek", 
                "query": "%23RememberOakCreek", 
                "tweet_volume": null, 
                "name": "#RememberOakCreek", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23DontTellTheKidsBut", 
                "query": "%23DontTellTheKidsBut", 
                "tweet_volume": null, 
                "name": "#DontTellTheKidsBut", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23FoodFri", 
                "query": "%23FoodFri", 
                "tweet_volume": null, 
                "name": "#FoodFri", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23SashaBanksGotMarried", 
                "query": "%23SashaBanksGotMarried", 
                "tweet_volume": null, 
                "name": "#SashaBanksGotMarried", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%231YearSinceOTRAMetLife", 
                "query": "%231YearSinceOTRAMetLife", 
                "tweet_volume": null, 
                "name": "#1YearSinceOTRAMetLife", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23OlympicMoment", 
                "query": "%23OlympicMoment", 
                "tweet_volume": null, 
                "name": "#OlympicMoment", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23KimojiMerch", 
                "query": "%23KimojiMerch", 
                "tweet_volume": null, 
                "name": "#KimojiMerch", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23CTRRS", 
                "query": "%23CTRRS", 
                "tweet_volume": null, 
                "name": "#CTRRS", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23pubpeopleormovies", 
                "query": "%23pubpeopleormovies", 
                "tweet_volume": null, 
                "name": "#pubpeopleormovies", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23NormaniIsImportant", 
                "query": "%23NormaniIsImportant", 
                "tweet_volume": 15562, 
                "name": "#NormaniIsImportant", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23myprintly", 
                "query": "%23myprintly", 
                "tweet_volume": null, 
                "name": "#myprintly", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23CheezIt355", 
                "query": "%23CheezIt355", 
                "tweet_volume": null, 
                "name": "#CheezIt355", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23HappyBirthdayElvisDuran", 
                "query": "%23HappyBirthdayElvisDuran", 
                "tweet_volume": null, 
                "name": "#HappyBirthdayElvisDuran", 
                "promoted_content": null
            }, 
            {
                "url": "http://twitter.com/search?q=%23patientchat", 
                "query": "%23patientchat", 
                "tweet_volume": null, 
                "name": "#patientchat", 
                "promoted_content": null
            }
        ], 
        "as_of": "2016-08-05T19:18:31Z", 
        "locations": [
            {
                "woeid": 23424977, 
                "name": "United States"
            }
        ]
    }
]

Ademas podemos fundir las listas o diccionarios como si de un base de datos relacional se tratara, esto por ejemplo con el fin de revelar los elementos comunes entre las Trends o Tendencias, debemos saber que puede o no haber solapamiento entre los datos, esto depende de que datos estamos interceptando, podemos usar el código siguiente:

 

worl_trends_set = set ([trend["name"] 
 for trend in worl_trends[0]['trends']])

us_trends_set = set ([trend["name"] 
 for trend in us_trends[0]['trends']])

common_trends = worl_trends_set.intersection(us_trends_set)



print common_trends

Con este código vamos a obtener lo siguiente:

 

["#MakeMeVideo", "#NABJNAHJ16", "#SuicideSquadRejects", "#VisibleWomen", "#MentalHealthCareSoPoor", "#FlashbackFriday", "#NormaniIsImportant", "#OpeningCeremony", "#FridayFeeling", "#BlogHer16", "#InternationalBeerDay", "#PaulONeal", "#WhenIStressOutI"]

Es una salida un poco fea :S, ahora vamos a implementar un for para ir visualizando cada elemento de la lista y ademas vamos a colocarles un id a la manera python(ya luego lo haremos con sqlite3) y lo vamos a agregar desde el diccionario(aunque la estructura parece un tupla o lista) a la base de datos, este es el código y hasta aquí lo voy a dejar por hoy mañana comentamos un poco el código y la salida:

 


import twitter, json, sqlite3
# Lo primero que debemos hacer es ir a http://dev.twitter.com/apps/new y crear una app
#puedes ver https://dev.twitter.com/docs/auth/oauth para mas informacion
#las credenciales que necesitamos debemos copiarlas de XXXXXXXXXXXXXXXXXXXXXX y usarlas tal como se muestrasn en las imagenes de esta entrada ZZZZZZZZZZZZZZZZZZZZZZZzz

CONSUMER_KEY = 'Recuerda A'

CONSUMER_SECRET = 'Datos B'

OAUTH_TOKEN = 'Datos C'

OAUTH_TOKEN_SECRET = 'DatosD'



auth = twitter.oauth.OAuth(  OAUTH_TOKEN , OAUTH_TOKEN_SECRET, CONSUMER_KEY , CONSUMER_SECRET )

twitter_api = twitter.Twitter(auth=auth)

conn = sqlite3.connect('CuentasIntersecciones.sqlite') #Agregado de los conocimientos de la especializacion

cur = conn.cursor() # Agregado


cur.execute('''
DROP TABLE IF EXISTS CuentasComunicadas''')

cur.execute('''
CREATE TABLE CuentasComunicadas (cuenta TEXT, cuenta_id INTEGER)''')

word = cur.fetchone()

cuenta_id = 0

WORL_WOE_ID = 1

US_WOE_ID = 23424977

worl_trends = twitter_api.trends.place(_id=WORL_WOE_ID)

us_trends = twitter_api.trends.place(_id=US_WOE_ID)


worl_trends_set = set ([trend["name"] 
			for trend in worl_trends[0]['trends']])

us_trends_set = set ([trend["name"] 
			for trend in us_trends[0]['trends']])

common_trends = worl_trends_set.intersection(us_trends_set)

for word in common_trends:
  print word
  cuenta= str(word)
  cur = conn.cursor()
  cur.execute('''INSERT INTO CuentasComunicadas (cuenta,cuenta_id) VALUES ( ?, ? ) ''', (cuenta, cuenta_id))
  cuenta_id +=1

SQLSTR = "SELECT  cuenta,cuenta_id FROM CuentasComunicadas"

print
print
for word in cur.execute(SQLSTR):
  print str(word[0]), word[1]


conn.commit()
cur.close()

La salida del programa es la siguiente:

 

> python ej1_conBD_twiter1.py 
#SuicideSquadRejects
#VisibleWomen
#MakeMe
#FlashbackFriday
#LastTimeFrankDroppedAnAlbum
#NormaniIsImportant
#OpeningCeremony
#MentalHealthCareSoPoor
#BlogHer16
#PaulONeal
#AustinMahoneOnLiveStream
#WhenIStressOutI


#SuicideSquadRejects 0
#VisibleWomen 1
#MakeMe 2
#FlashbackFriday 3
#LastTimeFrankDroppedAnAlbum 4
#NormaniIsImportant 5
#OpeningCeremony 6
#MentalHealthCareSoPoor 7
#BlogHer16 8
#PaulONeal 9
#AustinMahoneOnLiveStream 10

 

Hasta mañana, recuerda comentar es agradecer y es importante para mi ser de ayuda con mi tiempo libre 😀

Sigue desde el principio esta serie y el por que me he animado a escribir sobre este tema:

Jugando con la API de Twitter [Parte 1]

Experiencia de La Especialización Python para toda la vida

Anuncios

2 pensamientos en “Jugando con la API de Twitter [Parte 2]

  1. Pingback: Jugando con la API de Twitter [Parte 3] | La Shell del Bit Pegajoso

  2. Pingback: Jugando con la API de Twitter [Parte 3.1] | La Shell del Bit Pegajoso

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s