Created
May 14, 2018 13:07
-
-
Save sakakendo/2aa6b99d291887017cfd160f16ec64f3 to your computer and use it in GitHub Desktop.
collect hashtagged pictures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip3 install requests_oauthlib | |
from requests_oauthlib import OAuth1Session | |
from IPython.display import Image,display_jpeg | |
import json,requests | |
CK='' | |
CS='' | |
AT='' | |
AS='' | |
twitter=OAuth1Session(CK,CS,AT,AS) | |
def getTimeline(): | |
param={'count':20} | |
url='https://api.twitter.com/1.1/statuses/home_timeline.json' | |
req=twitter.get(url,params=param) | |
if req.status_code == 200: | |
timeline=json.loads(req.text) | |
for tweet in timeline: | |
print(tweet['text']+'\n') | |
else: | |
print("Error :"+str(req.status_code)) | |
def searchTweets(q='#二度と撮れない写真を貼れ'): | |
url="https://api.twitter.com/1.1/search/tweets.json" | |
req=twitter.get(url,params={'q':q,'count':30}) | |
body=json.loads(req.text) | |
for status in body.get('statuses'): | |
if status == None:continue | |
print(status.get("text")) | |
medias=status.get('entities').get('media') | |
if medias == None: continue | |
for media in medias: | |
url=media.get('media_url') | |
ext=url.split('.')[-1] | |
fname=url.split('/')[-1] | |
print('url : ',url,ext,fname) | |
if ext=='jpg': | |
res=requests.get(url,allow_redirects=False) | |
if res.status_code!=200: continue | |
if 'image' not in res.headers["content-type"]: continue | |
with open(fname,'wb') as f: | |
f.write(res.content) | |
display_jpeg(Image(fname)) | |
else: | |
print(ext,'is not supported') | |
searchTweets() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment