I wanted to study api, so I made it as a test. Get the trend of Twitter (1st-5th) and make a bot to notify by Line.
pip install line-bot-sdk //Library using Line api
pip install requests requests_oauthlib//Library for using twitter api
Install each.
import json
from requests_oauthlib import OAuth1Session
from linebot import LineBotApi
from linebot.models import TextSendMessage
CONSUMER_KEY='*****'
CONSUMER_SECRET='*****'
ACESS_TOKEN='*****'
ACESS_TOKEN_SECRET='*****'
twitter=OAuth1Session(CONSUMER_KEY,CONSUMER_SECRET,ACESS_TOKEN,ACESS_TOKEN_SECRET)//Please enter your apikey
url='https://api.twitter.com/1.1/trends/place.json'
params={'id':23424856} //Set the id value for Japan.
res=twitter.get(url,params=params)
json=res.json()
trends=json[0]['trends']
names=[]
urls=[]
for i,trend in enumerate(trends):
if i>=5:
break
name=trends[i]['name']
url=trends[i]['url']
names.append(name)
urls.append(url)
CHANNEL_ACCESS_TOKEN="*****"
line_bot_api=LineBotApi(CHANNEL_ACCESS_TOKEN)
texts=[]
number=0
def main():
USER_ID="*****"
for i in range(len(names)):
texts.append(str(i+1)+" "+names[i]+"\n"+urls[i])
line_bot_api.push_message(USER_ID, TextSendMessage(text=texts[number]+'\n'+texts[number+1]+'\n'+texts[number+2]+'\n'+texts[number+3]+'\n'+texts[number+4]))
main()
I think it will be very easy if you can use the API.
Anything related to this article will be answered! !!
Recommended Posts