Commit 58f2500b authored by Barry Peddycord III's avatar Barry Peddycord III 🍞

Add new file

parent 5aadd089
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pip3 install wget
import time, os, sys, json
import wget
from mastodon import Mastodon, StreamListener
config = json.loads(open('/etc/askurza.json').read())
your_instance = config['your_instance']
login = config['login']
pwd = config['pwd']
tmpdir = 'tmp'
owner = config['owner']
tmpdir = 'tmp'
if not os.path.exists(tmpdir):
# wget
cards = json.loads(open("scryfall-oracle-cards.json").read())
index = {}
for card in cards:
index[card['name'].lower()] = card
if not os.path.exists('scryfall_clientcred.txt'):
to_file = 'scryfall_clientcred.txt',
api_base_url = your_instance
mastodon = Mastodon(
client_id = 'scryfall_clientcred.txt',
api_base_url = your_instance
def handle(toot):
cards = fetch_cards(toot)
if not cards:
return None, None
replytext = "\n"
images = []
for card in cards[:4]:
replytext += "%s: %s\n"%(card['name'][:30], card['scryfall_uri'])
card_file = tmpdir + '/' + card['id'] + '.jpg'
if not os.path.exists(card_file):
try:['image_uris']['normal'], card_file)
card_file = ""
card_text = "%s: %s"%(card['name'][:30], card.get('oracle_text', "Could not fetch oracle text."))
if card_file:
images.append([card_file, card_text])
return replytext, images
def handle_commands(status):
id = status['account']['id']
username = status['account']['acct']
banned = abuse_check(id)
if banned:
if "+register" in status['content']:
mastodon.status_post(status="@"+username+" OK! I'm going to send you a follow request. From now on, if you post a card name [[like this]] even without mentioning me, I will reply with the scryfall results. If you want me to stop, just tell me to +unregister.", visibility="direct")
elif "+unregister" in status['content']:
a= mastodon.status_post(status="@"+username+" OK! I will stop following you and stop replying to your posts. You can still explicitly request card lookups by mentioning me, and can +register later if you change your mind.", visibility="direct")
b= mastodon.account_unfollow(id)
def fetch_cards(string):
cardlist = string.lower().split('[[')
result = []
for card in cardlist:
card = card.split(']]')[0]
if card in index:
return result[:10]
history = {}
timers = {}
grace = 120
limit = 100
def abuse_check(id):
x = mastodon.account_relationships(id)[0]
if x['blocking']: return True
if id not in timers: timers[id] = time.time()
if time.time() - timers[id] > grace:
if id not in history: history[id] = 0
history[id] += 1
if history[id] > limit:
return True
pass #mastodon.account_follow(id)
return False
def check_cards(notification):
if notification['reblog']:
reply_text, images = handle(notification['content'])
if reply_text:
banned = abuse_check(notification['account']['id'])
if banned:
pictures_ids = []
for i, t in images:
pictures_ids.append( mastodon.media_post(i, description=t, focus=(-1,1)) )
mastodon.status_reply(notification, reply_text, media_ids=pictures_ids, untag=True, sensitive=True)
class Listener(StreamListener):
def on_notification(self, notification):
if notification['type'] == "mention":
def on_update(self, status):
x = mastodon.account_relationships(status['account']['id'])[0]
if x['following']:
mastodon.log_in(login, pwd)
mastodon.status_post(status="%s Rebooting - loaded %d cards"%(owner, len(index.keys())), visibility="direct")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment