[PYTHON] I tried using Twitter api and Line api

Introduction

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.

Environment

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()

At the end

I think it will be very easy if you can use the API.

If you have any questions, feel free to tell us! !!

Anything related to this article will be answered! !!

Please tell me if there is something wrong

Recommended Posts

I tried using Twitter api and Line api
I tried using the checkio API
I tried follow management with Twitter API and Python (easy)
I made a Chatbot using LINE Messaging API and Python
I tried using YOUTUBE Data API V3
I tried drawing a line using turtle
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using UnityCloudBuild API from Python
I tried using the BigQuery Storage API
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I tried using docomo speech recognition API and Google Speech API in Java
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried using PyEZ and JSNAPy. Part 1: Overview
I tried using AWS Rekognition's Detect Labels API
I tried using Remote API on GAE / J
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried using the Google Cloud Vision API
I tried LINE Message API (line-bot-sdk-python) on GAE
I tried using google test and CMake in C
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
Try using the Twitter API
I tried to automate internal operations with Docker, Python and Twitter API + bonus
I tried using BigQuery ML
I tried using Amazon Glacier
Try using the Twitter API
I tried APN (remote notification) using Parse.com REST API
I tried using git inspector
I tried using the API of the salmon data project
[Python] I tried using OpenPose
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator