mirror of
https://github.com/cfultz/MTGRandomScryfallBot.git
synced 2024-11-21 15:50:03 +01:00
Updated the bot to also be able to random cards from specific sets
This commit is contained in:
parent
640cc15b69
commit
44519e8520
@ -1,17 +1,31 @@
|
||||
# Import required methods
|
||||
import random
|
||||
import os
|
||||
from requests import get
|
||||
from json import loads
|
||||
from shutil import copyfileobj
|
||||
from mastodon import Mastodon
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
mastodon = Mastodon(
|
||||
access_token = 'token.secret',
|
||||
api_base_url = 'https://cfultz.com'
|
||||
access_token = os.getenv("token"),
|
||||
api_base_url = os.getenv("url")
|
||||
)
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
# Load the card data from Scryfall
|
||||
card = loads(get(f"https://api.scryfall.com/cards/random?format=image.json").text)
|
||||
|
||||
#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)
|
||||
|
||||
# Get the image URL
|
||||
img_url = card['image_uris']['large']
|
||||
@ -33,14 +47,21 @@ with open('image.jpg', 'wb') as out_file:
|
||||
copyfileobj(get(img_url, stream = True).raw, out_file)
|
||||
|
||||
# Removing weird or unusable characters for hashtags
|
||||
special_characters=["$", "'","`","%","&","(",")",",",":","?","!","@",",",".","*","-"]
|
||||
special_characters=["$","'","`","%","&","(",")",":","?","!","@","*"," "]
|
||||
for i in special_characters:
|
||||
hTitle = mtg_title.replace(i,"")
|
||||
hSet = mtg_set.replace(i,"")
|
||||
hArtist = mtg_artist.replace(i,"")
|
||||
|
||||
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(',','')
|
||||
|
||||
# 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)
|
||||
|
||||
# Post the Toot
|
||||
mastodon.status_post("#magicthegathering" + " " + "#mtg" + " " + "#" + hTitle.replace(" ", "") + " " + "#" + hArtist.replace(" ", ""),media_ids=media)
|
||||
|
Loading…
Reference in New Issue
Block a user