0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アニメの字幕をもとに自動生成した文章を一定間隔ごとにtweetする

Posted at

概要

アニメの字幕をもとにマルコフ連鎖で文章自動生成 - Qiitaの続きです。
自動生成した文章を一定間隔ごとにtweetするbotアカウントを作りました。
アニメ見て日本語覚えたbot - Twitter
アニメ見て.JPG

環境

ubuntu 18.04 LTS / python 3.6.7 / php 7.2.17

TwitterAPIの利用申請

Twitterの機能を利用するためにはAPIの申請が必要なようです。こちらの記事(Twitter API 登録 (アカウント申請方法) から承認されるまでの手順まとめ ※2018年9月時点の情報 - Qiita)を参考に申請を行いConsumerキーとアクセストークンを入手しました。

TwitterOAuthを使ってtweetを行う

APIを使用するにはTwitterOAuthが必要なようなのでhttps://github.com/abraham/twitteroauthからダウンロード、解凍して適当なディレクトリに置きます。
とりあえずこんな感じのディレクトリ構成にしました。

~/workspace/
  ┣ twitteroauth/
  ┣ assdumper/
  ┣ Videos/
  ┃    録画した.tsファイルをすべてここに入れておく
  ┣ wakatigaki.txt
  ┣ ass2text.py
  ┗ generate_text.py

公式ドキュメント(https://twitteroauth.com/)を参考に下記のようなコードにしてみました。

tweet.php
<?php

//OAuthライブラリの読み込み
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//認証情報
$consumerKey = '取得したconsumerKey';
$consumerSecret = '取得したconsumerSecret';
$accessToken = '取得したaccessToken';
$accessTokenSecret = '取得したaccessTokenSecret';

//接続
$connection = new TwitterOAuth( $consumerKey, $consumerSecret, $accessToken, $accessTokenSecret );

//文章生成
exec( 'python3 generate_text.py', $out );

//tweet
$statues = $connection->post( "statuses/update", [ "status" => $out[ 0 ] ] );

?>

このファイルをworkspace直下において実行すると自動生成された文章がtweetされます。

crontabでスケジューリング

あとはcrontabでスケジューリングしたら完成です。

.sh
crontab */20 * * * * cd ~/workspace && php tweet.php

これで20分ごとに自動生成した文章がtweetされます。

あとがき

記事に書くことがなくて困るくらい簡単に出来ました笑
twitterAPIの利用申請時の英作文が一番苦労したんじゃないかってぐらいです笑
TwitterOauthさまさまですね。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?