Python - twitter API로 친구목록 가져오기

CODEDRAGON Development/Python

반응형


 

 

twitter API로 친구목록 가져오기

 


 

 

 

File Info

file

설명

hidden.py

Twitter Oauth 정보를 가진 파일

oauth.py

OAuth라이브러리

twurl.py

OAuth라이브러리를 가져오고

hidden.py 있는 consumer_key consumer_secret 가져옵니다.

twtest.py

트위터 테스트

twitter1.py

인코딩한 데이터 일부와 요청 가능 횟수 확인하기

twitter2.py

친구 목록

 

 

 

 

Step by Step

·         twtest.py 실행

·         twitter1.py 실행

·         twitter2.py 실행

 

 

twtest.py 실행


 

* Calling Twitter...

https://api.twitter.com/1.1/statuses/user_timeline.json?oauth_timestamp=1529236201&oauth_signature=nSBXB434......gO0%3D&count=2&oauth_nonce=22902366&screen_name=drchuck&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_consumer_key=dJbK6.....s7HUPJw&oauth_token=834982845172981760-L5tmpy1OFtlSxf....YFfp

b'[{"created_at":"Sat Jun 16 19:18:57 +0000 2018","id":1008066417802006528,"id_str":"1008066417802006528","text":"Doing some interior painting in the @sakaiproject racing team #venturebrothers Stanza https:\\/\\/t.co\\/vCkhCAhbvl","truncated":false,"entities":{"hashtags":[{"text":"venturebrothers","indices":[62,78]}],"symbols":[],"user_mentions":[{"screen_name":"sakaiproject","name":"Sakai Project","id":10768892,"id_str":"10768892","indices":[36,49]}],"urls":[],"media":[{"id":1008066411598680065,"id_str":"1008066411598680065","indices":[86,109],"media_url":"http:\\/\\/pbs.twimg.com\\/media\\/Df1fD3VXkAExpHT.jpg","media_url_https":"https:\\/\\/pbs.twimg.com\\/media\\/Df1fD3VXkAExpHT.jpg","url":"https:\\/\\/t.co\\/vCkhCAhbvl","display_url":"pic.twitter.com\\/vCkhCAhbvl","expanded_url":"https:\\/\\/twitter.com\\/drchuck\\/status\\/1008066417802006528\\/photo\\/1","type":"photo","sizes":{"medium":{"w":1024,"h":768,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":680,"h":510,"resize":"fit"},"large":{"w":1024,"h":768,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":1008066411598680065,"id_str":"1008066411598680065","indices":[86,109],"media_url":"http:\\/\\/pbs.twimg.com\\/media\\/Df1fD3VXkAExpHT.jpg","media_url_https":"https:\\/\\/pbs.twimg.com\\/media\\/Df1fD3VXkAExpHT.jpg","url":"https:\\/\\/t.co\\/vCkhCAhbvl","display_url":"pic.twitter.com\\/vCkhCAhbvl","expanded_url":"https:\\/\\/twitter.com\\/drchuck\\/status\\/1008066417802006528\\/photo\\/1","type":"photo","sizes":{"medium":{"w":1024,"h":768,"resize":"fit"},"thu

...

2009","favourites_count":2474,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":4345,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"352726","profile_background_image_url":"http:\\/\\/abs.twimg.com\\/images\\/themes\\/theme5\\/bg.gif","profile_background_image_url_https":"https:\\/\\/abs.twimg.com\\/images\\/themes\\/theme5\\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\\/\\/pbs.twimg.com\\/profile_images\\/661655842190553088\\/v7UwoqC8_normal.jpg","profile_image_url_https":"https:\\/\\/pbs.twimg.com\\/profile_images\\/661655842190553088\\/v7UwoqC8_normal.jpg","profile_banner_url":"https:\\/\\/pbs.twimg.com\\/profile_banners\\/36615874\\/1404329448","profile_link_color":"D02B55","profile_sidebar_border_color":"829D5E","profile_sidebar_fill_color":"99CC33","profile_text_color":"3E4415","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":2,"favorite_count":9,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},"is_quote_status":false,"retweet_count":2,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"en"}]'

======================================

{'strict-transport-security': 'max-age=631138519', 'date': 'Sun, 17 Jun 2018 11:50:03 GMT', 'x-content-type-options': 'nosniff', 'pragma': 'no-cache', 'x-transaction': '00b00162003e7c8d', 'x-connection-hash': 'da95232e6e7496f4710a76cde56a3ae9', 'cache-control': 'no-cache, no-store, must-revalidate, pre-check=0, post-check=0', 'x-frame-options': 'SAMEORIGIN', 'x-rate-limit-limit': '900', 'set-cookie': 'guest_id=v1%3A152923620338583094; Expires=Tue, 16 Jun 2020 11:50:03 GMT; Path=/; Domain=.twitter.com', 'x-rate-limit-remaining': '899', 'content-length': '11454', 'connection': 'close', 'x-response-time': '158', 'expires': 'Tue, 31 Mar 1981 05:00:00 GMT', 'last-modified': 'Sun, 17 Jun 2018 11:50:03 GMT', 'server': 'tsa_m', 'status': '200 OK', 'content-disposition': 'attachment; filename=json.json', 'x-access-level': 'read-write', 'x-xss-protection': '1; mode=block; report=https://twitter.com/i/xss_report', 'content-type': 'application/json;charset=utf-8', 'x-twitter-response-tags': 'BouncerCompliant', 'x-rate-limit-reset': '1529237103'}

 

 

 

 

twitter1.py 실행

 


 

 

Enter Twitter Account:drchuck

Retrieving https://api.twitter.com/1.1/statuses/user_timeline.json?oauth_token=834982845172981760-L5tmpy1OFtlSxfVzOoQ8QeGxGITYFfp&oauth_version=1.0&oauth_timestamp=1529237559&oauth_signature=ueIZmMvcxdw%2BdBWo1xoJ7Fjk6zc%3D&oauth_consumer_key=dJbK6wetTYiTzrTNGCs7HUPJw&oauth_signature_method=HMAC-SHA1&oauth_nonce=21676370&count=2&screen_name=drchuck

[{"created_at":"Sat Jun 16 19:18:57 +0000 2018","id":1008066417802006528,"id_str":"1008066417802006528","text":"Doing some interior painting in the @sakaiproject racing team #venturebrothers Stanza https:\/\/t.co\/vCkhCAhbvl","truncated":false,"entit

Remaining 898

 

Enter Twitter Account:

 

 

 

 

 

twitter2.py 실행


 

"users" 객체 배열로 사용자 정보가 저장되어 있습니다.

 

Enter Twitter Account:drchuck

Retrieving https://api.twitter.com/1.1/friends/list.json?oauth_consumer_key=dJbK6...s7HUPJw&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1529237773&oauth_version=1.0&screen_name=drchuck&oauth_token=834982845172981760-L5tmpy.....TYFfp&oauth_signature=uclJ0J7eEp5pDiuF%2Fp%2B4RlYx9Uw%3D&oauth_nonce=64822153&count=5

{

  "users": [

    {

      "profile_sidebar_border_color": "FFFFFF",

      "profile_text_color": "333333",

      "description": "engineer/CTO @ https://t.co/wQUyKt29Fs: data-driven and open source learning built on Open edX for @usairforce, @nvidia, @ibm, @globalknowledge | romans 8:31",

      "profile_use_background_image": true,

      "status": {

        "favorite_count": 1,

        "text": "Jeb Bush on CNBC talking about our Freshman Year for Free program\n\nExciting! https://t.co/vgBrbXH17f",

        "in_reply_to_user_id_str": null,

        "retweet_count": 0,

        "is_quote_status": true,

        "coordinates": null,

        "place": null,

        "in_reply_to_screen_name": null,

        "id": 1006532138789982215,

        "quoted_status_id_str": "1006253987220393984",

        "geo": null,

        "id_str": "1006532138789982215",

        "contributors": null,

        "in_reply_to_status_id_str": null,

        "in_reply_to_status_id": null,

        "retweeted": false,

        "truncated": false,

        "in_reply_to_user_id": null,

        "created_at": "Tue Jun 12 13:42:17 +0000 2018",

        "quoted_status_id": 1006253987220393984,

        "entities": {

          "user_mentions": [],

          "hashtags": [],

          "urls": [

            {

              "url": "https://t.co/vgBrbXH17f",

              "expanded_url": "https://twitter.com/ExcelinEd/status/1006253987220393984",

              "indices": [

                77,

                100

              ],

              "display_url": "twitter.com/ExcelinEd/stat\u2026"

            }

          ],

          "symbols": []

        },

        "possibly_sensitive": false,

        "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Lite</a>",

        "favorited": false,

        "lang": "en"

      },

      "utc_offset": null,

      "followers_count": 480,

      "id": 527094727,

      "is_translator": false,

      "profile_image_url": "http://pbs.twimg.com/profile_images/859851794465386496/TtBm5Eh3_normal.jpg",

      "location": "New York, NY",

      "translator_type": "none",

      "notifications": false,

      "created_at": "Sat Mar 17 04:37:32 +0000 2012",

      "profile_link_color": "1B95E0",

      "entities": {

        "url": {

          "urls": [

            {

              "url": "https://t.co/wQUyKt29Fs",

              "expanded_url": "http://ibleducation.com",

              "indices": [

                0,

                23

              ],

              "display_url": "ibleducation.com"

            }

          ]

 },

        "description": {

          "urls": [

            {

              "url": "https://t.co/wQUyKt29Fs",

              "expanded_url": "http://ibleducation.com",

              "indices": [

                15,

                38

              ],

              "display_url": "ibleducation.com"

            }

          ]

        }

      },

      "protected": false,

      "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme15/bg.png",

      "follow_request_sent": false,

      "geo_enabled": true,

      "friends_count": 1808,

      "name": "Miguel Amigot",

      "profile_background_tile": false,

      "default_profile_image": false,

      "profile_banner_url": "https://pbs.twimg.com/profile_banners/527094727/1522087452",

      "blocking": false,

      "statuses_count": 1095,

      "following": false,

      "lang": "en",

      "screen_name": "miguelamigot",

      "verified": false,

      "contributors_enabled": false,

      "url": "https://t.co/wQUyKt29Fs",

      "profile_background_color": "022330",

      "muting": false,

      "id_str": "527094727",

      "has_extended_profile": false,

      "blocked_by": false,

      "profile_sidebar_fill_color": "C0DFEC",

      "listed_count": 36,

      "profile_image_url_https": "https://pbs.twimg.com/profile_images/859851794465386496/TtBm5Eh3_normal.jpg",

      "live_following": false,

      "default_profile": false,

      "profile_background_image_url": "http://abs.twimg.com/images/themes/theme15/bg.png",

      "is_translation_enabled": false,

      "favourites_count": 1009,

      "time_zone": null

    },

    {

      "profile_sidebar_border_color": "C6E2EE",

      "profile_text_color": "663B12",

 

...

 

      "lang": "en",

      "screen_name": "DEVlANT",

      "verified": false,

      "contributors_enabled": false,

      "url": "http://t.co/gLm6VQeDvT",

      "profile_background_color": "C0DEED",

      "muting": false,

      "id_str": "20615774",

      "has_extended_profile": false,

      "blocked_by": false,

      "profile_sidebar_fill_color": "DDEEF6",

      "listed_count": 0,

      "profile_image_url_https": "https://pbs.twimg.com/profile_images/2162094923/462507_2821407253845_1218862673_32303709_1257212702_o_normal.jpg",

      "live_following": false,

      "default_profile": true,

      "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",

      "is_translation_enabled": false,

      "favourites_count": 0,

      "time_zone": null

    }

  ],

  "previous_cursor": 0,

  "next_cursor": 1601448682996201546,

  "previous_cursor_str": "0",

  "next_cursor_str": "1601448682996201546"

}

Remaining 14

miguelamigot

   Jeb Bush on CNBC talking about our Freshman Year f

S_Phillipson

   I’m 3/3 for getting a yes to saying “Do you know @

24hoursoflemons

   Some of our riff-raff got themselves into the 24 H

kosburn0408

   RT @hobsonjill: Join @GregNadeau @kosburn0408 @ada

DEVlANT

   I nominate @deviantART for a Shorty Award in #art

 

Enter Twitter Account:

결과를 보면

윗부분은 JSON 형식으로 가져온 데이터이고

아래는  JSON 파싱하여 sreen_name text요소에 있는 내용을 50자만 출력한 것입니다.

 

miguelamigot

 

S_Phillipson

 

...

sreen_name

Jeb Bush on CNBC talking about our Freshman Year f

 

I’m 3/3 for getting a yes to saying “Do you know @

 

text

 

 


반응형

'Development > Python' 카테고리의 다른 글

install Seaborn  (0) 2018.07.02
NumPy.unique()  (0) 2018.07.02
VISUALIZE CODE AND GET LIVE HELP - Python, Java, C, C++, JavaScript, Ruby  (0) 2018.07.01
pprint 패키지  (0) 2018.06.29
PyCharm 2018.x.x 설치하기  (0) 2018.06.29