2024-08-04 02:12:34 +02:00
|
|
|
import random
|
|
|
|
import os
|
2024-02-09 15:08:18 +01:00
|
|
|
from requests import get
|
|
|
|
from json import loads
|
|
|
|
from shutil import copyfileobj
|
|
|
|
from mastodon import Mastodon
|
2024-08-04 02:12:34 +02:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
load_dotenv()
|
2024-02-09 15:08:18 +01:00
|
|
|
|
|
|
|
mastodon = Mastodon(
|
2024-08-04 02:12:34 +02:00
|
|
|
access_token = os.getenv("token"),
|
|
|
|
api_base_url = os.getenv("url")
|
2024-02-09 15:08:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-04 02:12:34 +02:00
|
|
|
# Use this only if you want a specific set.
|
|
|
|
# You need to set both the code and find out how many total cards are in the set
|
|
|
|
# setname = "sld"
|
|
|
|
# cardid = random.randint(0,1718)
|
|
|
|
|
2024-02-09 15:08:18 +01:00
|
|
|
# Load the card data from Scryfall
|
2024-08-04 02:12:34 +02:00
|
|
|
|
|
|
|
#This is for a specific set
|
|
|
|
card = loads(get(f"https://api.scryfall.com/cards/{setname}/{cardid}").text)
|
|
|
|
|
|
|
|
# This is the actual random card:
|
|
|
|
card = loads(get(f"https://api.scryfall.com/cards/random?format=image.json").text)
|
2024-02-09 15:08:18 +01:00
|
|
|
|
|
|
|
# Get the image URL
|
|
|
|
img_url = card['image_uris']['large']
|
|
|
|
|
|
|
|
# Get card title
|
|
|
|
mtg_title = card['name']
|
|
|
|
|
|
|
|
# Artist name
|
|
|
|
mtg_artist = card['artist']
|
|
|
|
|
|
|
|
# Get flavor text
|
|
|
|
flavor = card['oracle_text']
|
|
|
|
|
|
|
|
# Get card set
|
|
|
|
mtg_set = card['set_name']
|
|
|
|
|
|
|
|
# Save the image
|
|
|
|
with open('image.jpg', 'wb') as out_file:
|
|
|
|
copyfileobj(get(img_url, stream = True).raw, out_file)
|
|
|
|
|
2024-02-09 16:07:14 +01:00
|
|
|
# Removing weird or unusable characters for hashtags
|
2024-08-04 02:12:34 +02:00
|
|
|
special_characters=["$","'","`","%","&","(",")",":","?","!","@","*"," "]
|
2024-02-09 16:07:14 +01:00
|
|
|
for i in special_characters:
|
|
|
|
hTitle = mtg_title.replace(i,"")
|
|
|
|
hSet = mtg_set.replace(i,"")
|
|
|
|
hArtist = mtg_artist.replace(i,"")
|
2024-02-09 15:08:18 +01:00
|
|
|
|
2024-08-04 02:12:34 +02:00
|
|
|
hArtist = hArtist.replace('.', '')
|
|
|
|
hArtist = hArtist.replace(',', '')
|
|
|
|
hArtist = hArtist.replace("'", '')
|
|
|
|
hTitle = hTitle.replace(',',"")
|
|
|
|
hTitle = hTitle.replace('.',"")
|
|
|
|
hTitle = hTitle.replace(':',"")
|
|
|
|
hTitle = hTitle.replace("'","")
|
|
|
|
hSet = hSet.replace('.','')
|
|
|
|
hSet = hSet.replace(',','')
|
|
|
|
|
2024-02-09 16:07:14 +01:00
|
|
|
# Set the Mastodon post information
|
|
|
|
media = mastodon.media_post("image.jpg", description="Card Name: " + mtg_title + "\n" + "Set: " + mtg_set + "\n" + "Description: " + flavor + "\n" + "Artist: " + mtg_artist)
|