Created
July 16, 2018 11:19
-
-
Save vim13/5b77e766c58ed1417cb86474cdda7a50 to your computer and use it in GitHub Desktop.
tootを検閲してtweet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import re | |
import os | |
import ast | |
import requests | |
import tweepy | |
from mastodon import Mastodon | |
def login(): | |
mastodon = Mastodon(client_id = '/home/ubuntu/python/app_key.txt', | |
access_token = '/home/ubuntu/python/user_key.txt', api_base_url = 'https://mstdn.jp') | |
return mastodon | |
def checkId(mastodon): | |
user_dict = mastodon.account_verify_credentials() | |
timeline = mastodon.account_statuses(user_dict['id'], limit=1) | |
ids = str(timeline[0].id) | |
return ids | |
def fileOpen(path): | |
opened_file = open(path, 'r') | |
text = opened_file.read() | |
opened_file.close() | |
return text | |
def fileWrite(path, text): | |
opened_file = open(path, 'w') | |
opened_file.write(text) | |
opened_file.close() | |
def twitterAuth(path): | |
dic = fileOpen(path) | |
keys = ast.literal_eval(dic) | |
auth = tweepy.OAuthHandler(keys['consumer_key'], keys['consumer_secret']) | |
auth.set_access_token(keys['access_token'], keys['access_token_secret']) | |
api = tweepy.API(auth) | |
return api | |
def makeTweet(toot_list): | |
api = twitterAuth('/home/ubuntu/python/twitter_keys.txt') | |
toot_list.reverse() | |
for tweet in toot_list: | |
media_ids = [] | |
for filename in tweet['media']: | |
temp = '/home/ubuntu/python/temp.jpg' | |
name = filename['url'] | |
request = requests.get( name, stream=True) | |
if request.status_code == 200: | |
with open(temp, 'wb') as image: | |
for chunk in request: | |
image.write(chunk) | |
res = api.media_upload(temp) | |
os.remove(temp) | |
media_ids.append(str(res.media_id)) | |
if tweet['toot']: | |
api.update_status(status = tweet['toot'][0][:140].encode('utf-8'), media_ids = media_ids) | |
else: | |
api.update_status(media_ids = media_ids) | |
def ngWord(x): | |
lists = fileOpen('/home/ubuntu/python/NGWords.txt').decode('utf-8') | |
comp = re.compile(lists, re.I) | |
i = 0 | |
while i >= 0: | |
m = comp.search(x, i) | |
if m: | |
num = m.end() - m.start() | |
rep = u"*" * num | |
x = x[:m.start()] + rep + x[m.end():] | |
i = m.start() + 1 | |
else: | |
break | |
return x | |
def makeList(mastodon, logs): | |
toot_list = [] | |
user_dict = mastodon.account_verify_credentials() | |
toot = mastodon.account_statuses(user_dict['id'], since_id = logs) | |
for line in toot: | |
toot_dict = { u'toot':[], u'media':[]} | |
if line[u'media_attachments']: | |
toot_dict['media'] = line[u'media_attachments'] | |
if line[u'content']: | |
x =line[u'content'] | |
p = re.compile(r"<[^>]*?>") | |
x = p.sub("", x) | |
x = ngWord(x) | |
toot_dict['toot'].append(x) | |
toot_list.append(toot_dict) | |
return toot_list | |
def main(): | |
mastodon = login() | |
ids = checkId(mastodon) | |
logs = fileOpen('/home/ubuntu/python/log_toot.txt') | |
if not ids == logs: | |
lists = makeList(mastodon, logs) | |
makeTweet(lists) | |
fileWrite('/home/ubuntu/python/log_toot.txt', str(ids)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment