1
3

More than 3 years have passed since last update.

【Python】特定のハッシュタグが付いたツイートを自動的にTrelloのカードに追加

Posted at

はじめに

背景

初めまして、桃熊猫(@momopanda_jp )です。
私はTwitterでやりたいことを結構書くのですが、TLで流れて忘れてしまうので、
ツイートしたら自動的にTrelloのカードに追加できたら便利そうだなと思い、
コーディングすることにしました。

今回はTwitterのデータの操作に便利なライブラリ「tweepy」を使ってみようと思います。
詳しくはこちら→tweepy

対象

・Twitter API未取得の方
・Trello API未取得の方
・Google Cloud Platform(GCP)未登録の方
・やりたいことなどをリスト化したい方
・ツイートはするけど、Trelloに一々追加するのが面倒な方

環境

Windows10 Home
Python3.7
VSCode
Chrome

目次

1.Twitter API・Trello APIの取得

 1-1. Twitter API取得
 1-2. Trello API取得

2.ツイートの取得
3.Trelloへのカード追加

 3-1. Trelloのボード・リストのIDを取得
 3-2.カードの名前をツイート内容にして追加
 3-3. 同じ内容のツイートは追加しないようにする

4.GCPの登録・自動化

1.Twitter API・Trello APIの取得

1-1. Twitter API取得

Twitter API 登録 (アカウント申請方法) から承認されるまでの手順まとめ ※2019年8月時点の情報
参考に取得しました。

1-2.Trello API取得

PythonでTrelloのタスクを取得するの「【準備】TrelloのAPIトークンとシークレットを取得する」を参考に、
キー・トークンを取得しました。

2.ツイートの取得

2-1.tweepyのインストール

コマンド
pip install tweepy

2-2. ツイートの取得・整形

Python
import tweepy

#Twitter API
#各キーを取得
consumer_key = 'xxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxx-xxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxx'

#認証情報を設定    
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

#APIインスタンスの作成
api = tweepy.API(auth)

#自分のハッシュタグが付いた最新のツイートの内容を一つずつ出力
#qは出力したいワード(今回はハッシュタグ)を指定
#screen_nameは出力したいアカウントのID
#result_type='recent'で最新のツイートを指定
#itemsの引数で一回で出力できるツイート数を指定できる
for tweet in tweepy.Cursor(api.search, q = '#○○○○', screen_name = 'xxxxxxxxxx', result_type = 'recent').items(1):
    wish = tweet.text.replace('\\n', '').replace('RT @xxxxxxxxxx: ', '').replace('#○○○○', '').replace('\n','')

参考:Pythonで特定のキーワードが付与されたツイート収集する方法
   tweepyレファレンス

3.Trelloへのカード追加

3-1. Trelloのボード・リストのIDを取得

Trelloの情報がJSON形式なので、見やすいようにjqをインストールして、PATHを通します。
PATHの通し方はWindows で環境変数 PATH をいじる方法のまとめを参考にさせてもらいました。
curlしたいURLはTrello(Get Boards that Member belongs to)の例を参考にしてください。

追加したいボードのIDを以下のように取得します。

コマンド
curl "https://trello.com/1/members/{userID}/boards?key={Key}&token={token}&fields=name" | jq

出力結果はこのように表示されます。

コマンド(結果)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   117  100   117    0     0    117      0  0:00:01 --:--:--  0:00:01   207
[
  {
    "name": "To do list",
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx"
  },
  {
    "name": "やりたいこと",
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx"
  }
]

追加したいリストのIDも以下で取得できます。

コマンド
curl "https://trello.com/1/boards/{BoardID}/lists?key={key}&token={token}&fields=name" | jq
コマンド(結果)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   319  100   319    0     0    319      0  0:00:01 --:--:--  0:00:01   600
[
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "未着手"
  },
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "勉強すること"
  },
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "作業中"
  },
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "完了"
  },
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "合格"
  },
  {
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "中止"
  }
]

3-2. カードの名前をツイート内容にして追加

Python
import tweepy
from trello import TrelloApi

# Trello API
# Key Token
#trello = TrelloApi('キー', 'トークン')
key = 'xxxxxxxxxxxxxx'
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
trello = TrelloApi(key, token)

# List ID ボードIDから調べたリストID
listid = 'xxxxxxxxxxxxxxxxxxxxxx'

#自分のハッシュタグが付いたツイートを一つずつ出力
for tweet in tweepy.Cursor(api.search, q = '#○○○○', screen_name = 'xxxxxxxxxx', result_type = 'recent').items(1):
        wish = tweet.text.replace('\\n', '').replace('RT @xxxxxxxxxx: ', '').replace('#○○○○', '').replace('\n','')

        # Card Name
        cardname = wish #特定のハッシュタグ付きのツイート内容

        # Card Description
        #desc = '新しいタスクの内容です。1行目n新しいタスクの内容です。2行目n新しいタスクの内容です。3行目n'
        desc = ''
        card = trello.cards.new(cardname, listid, desc)

3-3.同じ内容のツイートは追加しないようにする

Python
import tweepy
import json
from trello import TrelloApi
import requests


# Trello API
# Key Token
#trello = TrelloApi('キー', 'トークン')
key = 'xxxxxxxxxxxxxxxxxxxx'
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
trello = TrelloApi(key, token)

# List ID ボードIDから調べたリストID
listid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 


#既にあるカードの確認
url = "https://trello.com/1/lists/" + listid + "/cards"
query = {'key': key, 'token': token, 'fields': 'name'}
r = requests.get(url, params = query)
data = r.json()

name_list = []

for x in range(len(data)):
    name = data[x]['name'].replace('\n','')
    name_list.append(name)


#自分のハッシュタグが付いたツイートを一つずつ出力
for tweet in tweepy.Cursor(api.search, q = '#○○○○', screen_name = 'xxxxxxxxxxx', result_type = 'recent').items(1):
     wish = tweet.text.replace('\\n', '').replace('RT @xxxxxxxxxxx: ', '').replace('#○○○○', '').replace('\n','')

     #カードが重複しないように設定
     if wish not in name_list:
         # Card Name
         cardname = wish #カードの名前を特定のハッシュタグ付きのツイート内容にする

         # Card Description
         #desc = '新しいタスクの内容です。1行目n新しいタスクの内容です。2行目n新しいタスクの内容です。3行目n'
         desc = ''

         card = trello.cards.new(cardname, listid, desc)

        else:
            break

参考:【30分で動かすシリーズ】TrelloにAPIを使ってカードの起票や取得をしてみる
   Trello(Create a new card)

4.GCPの登録・自動化

まずGoogle Cloud Platformを登録しましょう。

サーバーレス + Pythonで定期的にスクレイピングを行う方法を参考に、登録から作ったジョブの実行まで進めていきます。

順番は以下の通りです。
・GCP登録
・GCPでプロジェクト作成
・Cloud Functionsの作成
   ・関数の作成
   ・ソースコードの作成
   ・関数のテスト
・Cloud Schedulerの作成
   ・ジョブの実行

関数の作成時に、私はランタイムをPython3.7にして、
ソースコードの入力時にmain.pyrequirement.txtは以下のように書きました。
mainの引数のeventcontextは関数に含まれていなくても、書いておかないと実行できないので注意しましょう。

main.py
import tweepy
import json
from trello import TrelloApi
import requests

def main(event, context):
    #Twitter API
    #各キーを取得
    consumer_key = 'xxxxxxxxxxxx'
    consumer_secret = 'xxxxxxxxxxxxxxx'
    access_token = 'xxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx'
    access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

    #認証情報を設定
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    #APIインスタンスの作成
    api = tweepy.API(auth)

    # Trello API
    # Key Token
    #trello = TrelloApi('キー', 'トークン')
    key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    trello = TrelloApi(key, token)

    # List ID   上記で調べたボードIDから調べたリストID
    listid = 'xxxxxxxxxxxxxxxxxx'


    #既にあるカードの確認
    url = "https://trello.com/1/lists/" + listid + "/cards"
    query = {'key': key, 'token': token, 'fields': 'name'}
    r = requests.get(url, params = query)
    data = r.json()
    name_list = []

    for x in range(len(data)):
        name = data[x]['name'].replace('\n','')
        name_list.append(name)


    #自分のハッシュタグが付いたツイートを一つずつ出力
    for tweet in tweepy.Cursor(api.search, q = '#○○○○', screen_name = 'xxxxxxxxxxx', result_type = 'recent').items(1):
        wish = tweet.text.replace('\\n', '').replace('RT @xxxxxxxxxxx: ', '').replace('#○○○○', '').replace('\n','')

        #カードが重複しないように設定
        if wish not in name_list:
            # Card Name
            cardname = wish #特定のハッシュタグ付きのツイート内容

            # Card Description
            #desc = '新しいタスクの内容です。1行目n新しいタスクの内容です。2行目n新しいタスクの内容です。3行目n'
            desc = ''
            card = trello.cards.new(cardname, listid, desc)
        else:
            break

requirement.txt
# Function dependencies, for example:
# package>=version
tweepy>=3.8.0
trello>=0.9.4
requests>=2.23.0

今回、Cloud Schedulerでは、1分ごとに関数が実行されるように設定しました。
ツイートを沢山する方は、出力するツイート数を増やしたりして調整してください。

参考:Cloud Function
   Cloud Scheduler

以上になります。閲覧ありがとうございました!

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3