[PYTHON] Have Voiceroid read the Twitter TL aloud

Introduction

Since August 16, 2018, Twitter's API has changed and UserStream is no longer available. Therefore, the source code described below does not work properly. </ font> The current response is undecided, but instead of Userstream You may want to hit the List API, which can be used 900 times / 15 minutes (as of September 27).

It is possible to read aloud even with the stick-reading plug-in, which is easier. But I didn't know how to prevent RT from being read aloud, so I wrote it appropriately. ~~ Actually, it would be best if it could be run on linux instead of windows ... ~~

Execution environment

  • Windows10
  • Python3.5.2
  • tweepy ver3.5.0 --Minoyasu ★ TALK ver.1.13.2 --VOICEROID + Tohoku Kiritan

Preparation

--Python installation http://qiita.com/t2y/items/2a3eb58103e85d8064b6

--Install tweepy http://qiita.com/laco0416/items/f2a8928ef54626a75ae1

--Twitter API Key is required to use tweepy http://phiary.me/twitter-api-key-get-how-to/

--Minan ★ TALK installation http://publish-tool.blogspot.jp/search/label/%E6%B0%91%E5%AE%89%E2%98%85TALK

--Buy your favorite VOICEROID + http://www.ah-soft.com/voiceroid/

Execution script

TimelineVR.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import codecs
import json
from os import path as ospath
import re
import subprocess
import tweepy

# Check Config.json
fpath = '{0}\\Config.json'.format(ospath.dirname(ospath.abspath(__file__)))
if ospath.exists(fpath):
    fp = codecs.open(fpath, 'r', 'utf-8')
    conf = json.load(fp)
    fp.close()
else:
    # Initial setting
    PATH_VRX = input('Drag and Drop vrx.exe : ')
    PATH_VOICEROID = input('Drag and Drop VOICEROID.exe : ')
    CK = input("Input Consumer Key (API Key) : ")
    CS = input("Input Consumer Secret (API Secret) : ")
    AT = input("Input Access Token : ")
    AS = input("Input Access Token Secret : ")
    conf = {'path': {'vrx': PATH_VRX, 'voiceroid': PATH_VOICEROID},
            'api': {'ck': CK, 'cs': CS, 'at': AT, 'as': AS}}
    # Save to Config.json
    fp = codecs.open(fpath, 'w', 'utf-8')
    json.dump(conf, fp, indent=4)
    fp.close()

# Call voiceroid.exe and vrx.exe
pvr = subprocess.Popen(conf['path']['voiceroid'])
pvrx = subprocess.Popen(conf['path']['vrx'])

# Set Tweepy api
auth = tweepy.OAuthHandler(conf['api']['ck'], conf['api']['cs'])
auth.set_access_token(conf['api']['at'], conf['api']['as'])
api = tweepy.API(auth)


# Override Listener
class Listener(tweepy.StreamListener):

    def on_status(self, status):
        # NG word
        try:
            for word in conf['NG']['word']:
                if status.text.find(word) != -1:
                    return True
        except KeyError:
            pass
        # NG client
        try:
            if status.source in conf['NG']['client']:
                return True
        except KeyError:
            pass
        # NG user_id
        try:
            if status.user.id in conf['NG']['user_id']:
                return True
        except KeyError:
            pass
        # NG user_screen_name
        try:
            if status.user.screen_name in conf['NG']['user_screen_name']:
                return True
        except KeyError:
            pass
        # replace Tweet text
        try:
            for retxt in conf['re']:
                status.text = re.sub(retxt, conf['re'][retxt], status.text)
        except KeyError:
            pass
        #It seems to be okay because it has been escaped(?)
        cmd = "{0} {1}Tweet.{2}".format(
            conf['path']['vrx'], status.user.name, status.text)
        subprocess.call(cmd)
        return True

listener = Listener()
stream = tweepy.Stream(auth, listener)
try:
    stream.userstream()
except:
    pvr.kill()
    pvrx.kill()

Config.json


{
    "path": {
        "vrx": "C:\\xxx\\xxx\\vrx.exe",
        "voiceroid": "D:\\xxx\\xxx\\X\\VOICEROID.exe"
    },
    "api": {
        "ck": "xxx",
        "cs": "xxx",
        "at": "xxx",
        "as": "xxx"
    },
    "NG": {
        "word": ["RT @"],
        "client": ["Tsui abandoned jar"],
        "user_id": ["1919810", "114514"],
        "user_screen_name": ["tadokoro", "kouji"]
    },
    "re": {
        "(https?|ftp)(:\/\/[-_\\.!~*\\'()a-zA-Z0-9;\/?:\\@&=\\+\\$,%#]+)": "URL omitted"
    }
}

Put the existing Config.json in the same folder as TimelineVR.py. If not at startup, the initial settings for creating Config.json will be entered.

-Added NG word, NG client, NG user ID, and NG user screen name. Write as you like in the created Config.json. By adding "RT @" to the NG word, you can prevent RT from being read aloud.

-Addition of regular expression replacement By specifying the regular expression you want to replace and the character string to replace with, You can replace the text of the tweet. In the sample, the URL is set to be read as "URL omitted".

problem

If you execute the above script, it will be read aloud, but there are some problems.

~~-Sometimes it is not read aloud (it will be improved if you re-execute) Addendum (2017-02-06): Fixed Addendum (2017-11-14): If subprocess.Popen () is used, it will move to the next process before the process is called, so it seems desirable to use subprocess.call (). Addendum (2018-06-14): The source code is totally modified.

--Many windows open (at least 3) Regarding this, windows 10 has a virtual desktop, so hopefully it will not bother me ~~ .. should be ~~

At the end ... I think it should be forcibly terminated (appropriate)

Finally

For the time being, this time is for people who just need to move. In fact, you can work while talking from TL, so the time taken by Twitter is considerably reduced. ~~ Then why not stop Twitter ...? ~~

Recommended Posts

Have Voiceroid read the Twitter TL aloud
Have iphone read aloud Twitter TL using Pythonista3
Have python read the command output
Have pandas read the zip file on the web
Read the OpenCV documentation