Skip to content

Instantly share code, notes, and snippets.

@YuzuRyo61
Last active May 4, 2018 14:24
Show Gist options
  • Save YuzuRyo61/4e7f961632a2ec927bf5a65bf3917e64 to your computer and use it in GitHub Desktop.
Save YuzuRyo61/4e7f961632a2ec927bf5a65bf3917e64 to your computer and use it in GitHub Desktop.
またつまらぬものを作ってしまった。ただの男ロマンスクリプトです^^;
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import webbrowser
import requests
import re
import json
from xml.sax.saxutils import unescape
from urllib.parse import urlencode
from mastodon import *
PREFIX = "MastLogger"
class MListen(StreamListener):
def on_update(self, status):
account = status["account"]
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0x0000)
if str(status["spoiler_text"]) == "":
print("[{}]".format(str(PREFIX)) + "TOOT > {0} @{1}".format(str(account["display_name"].translate(non_bmp_map)), str(account["acct"].translate(non_bmp_map))))
else:
print("[{}]".format(str(PREFIX)) + "TOOT CW > {0} @{1}".format(str(account["display_name"].translate(non_bmp_map)), str(account["acct"].translate(non_bmp_map))))
def on_delete(self, status_id):
print("[{}]".format(str(PREFIX)) + "DELETE >>> {}".format(str(status_id)))
if __name__ == "__main__":
if not os.path.isfile("client.txt") and not os.path.isfile("instance.txt"):
instance = input("Instance Address: ")
Mastodon.create_app(
'MastLogger',
api_base_url = str(instance),
scopes=['read'],
website="https://yuzulia.com/",
to_file = 'client.txt'
)
with open('instance.txt', 'w') as iw:
iw.write(instance)
if not os.path.isfile("token.txt"):
with open('client.txt', 'r') as cw:
ck = cw.readline()
cs = cw.readline()
with open('instance.txt', 'r') as i:
instance = i.readline()
ck = re.sub('\n', '', ck)
cs = re.sub('\n', '', cs)
ap = urlencode(dict(
client_id=ck,
response_type="code",
redirect_uri="urn:ietf:wg:oauth:2.0:oob",
scope='read'
))
aurl = 'https://'+instance+'/oauth/authorize?'+ap
webbrowser.open(aurl)
print("Auth url is: \n{}".format(aurl))
chk = input("Challenge Key: ")
res = requests.post('https://'+instance+'/oauth/token', dict(
grant_type="authorization_code",
redirect_uri="urn:ietf:wg:oauth:2.0:oob",
client_id=ck,
client_secret=cs,
code=chk
)).json()
with open('token.txt', 'w') as tw:
tw.write(res["access_token"])
print("Write OK")
with open("instance.txt", 'r') as i:
instance = i.readline()
client = Mastodon(
client_id="client.txt",
access_token="token.txt",
api_base_url = str(instance)
)
print("[{}]".format(str(PREFIX)) + "START LOG")
listen = MListen()
client.stream_public(listen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment