Skip to content

Instantly share code, notes, and snippets.

@tomatosoupcan
Last active May 15, 2018 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomatosoupcan/33b04d0ac326c487b7d8268b840d9247 to your computer and use it in GitHub Desktop.
Save tomatosoupcan/33b04d0ac326c487b7d8268b840d9247 to your computer and use it in GitHub Desktop.
Weechat Replace Scryfall MTG Shortcode
# -*- coding: utf-8 -*-
import weechat
import re
import urllib2
weechat.register("mtg_replace", "tomatosoup", "0.1", "GPL3", "replaces mtg shortcodes and resolves scryfall images and replaces those too", "", "UTF-8")
weechat.prnt(" ", "Hello world")
mtgmatch = re.compile("^(.*)(:mana(.*):)(.*)")
halfmatch = re.compile(".*https://scryfall.com/card.*utm_source=slack.*")
boldmatch = re.compile("(?:^|\s)\*.*\*(?:$|\s)")
itamatch = re.compile("(?:^|\s)_.*_(?:$|\s)")
urlmatch = re.compile("(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)")
swaplist = [
["mana-0","%s0%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-1","%s1%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-2","%s2%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-2b","%s2/B%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-2g","%s2/%sG%s",(weechat.color("black,gray"),weechat.color("black,green"),weechat.color("reset"))],
["mana-2r","%s2/%sR%s",(weechat.color("black,gray"),weechat.color("black,red"),weechat.color("reset"))],
["mana-2u","%s2/%sU%s",(weechat.color("black,gray"),weechat.color("black,blue"),weechat.color("reset"))],
["mana-2w","%s2/%sW%s",(weechat.color("black,gray"),weechat.color("black,white"),weechat.color("reset"))],
["mana-3","%s3%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-4","%s4%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-5","%s5%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-6","%s6%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-7","%s7%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-8","%s8%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-9","%s9%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-10","%s10%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-11","%s11%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-12","%s12%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-13","%s13%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-14","%s14%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-15","%s15%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-16","%s16%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-17","%s17%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-18","%s18%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-19","%s19%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-20","%s20%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-100-1","%s10%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-100-2","%s0%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-1000000-1","%s1,%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-1000000-2","%s000%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-1000000-3","%s,00%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-1000000-4","%s0%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-b","%sB%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-bg","%sB/%sG%s",(weechat.color("black,gray"),weechat.color("black,green"),weechat.color("reset"))],
["mana-bp","%sΦ%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-br","%sB/%sR%s",(weechat.color("black,gray"),weechat.color("black,red"),weechat.color("reset"))],
["mana-c","%s◇%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-chaos","%s൴%s",(weechat.color("white,darkgray"),weechat.color("reset"))],
["mana-e","%s⬟%s",(weechat.color("white,darkgray"),weechat.color("reset"))],
["mana-g","%sG%s",(weechat.color("black,green"),weechat.color("reset"))],
["mana-gp","%sΦ%s",(weechat.color("black,green"),weechat.color("reset"))],
["mana-gu","%sG%s/%sU%s",(weechat.color("black,green"),weechat.color("black,gray"),weechat.color("black,blue"),weechat.color("reset"))],
["mana-gu","%sG%s/%sW%s",(weechat.color("black,green"),weechat.color("black,gray"),weechat.color("black,white"),weechat.color("reset"))],
["mana-half","%s½%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-hr","%s½%s",(weechat.color("black,red"),weechat.color("reset"))],
["mana-hw","%s½%s",(weechat.color("black,white"),weechat.color("reset"))],
["mana-infinity","%s∞%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-q","%s↶%s",(weechat.color("white,darkgray"),weechat.color("reset"))],
["mana-r","%sR%s",(weechat.color("black,red"),weechat.color("reset"))],
["mana-rg","%sR%s/%sG%s",(weechat.color("black,red"),weechat.color("black,gray"),weechat.color("black,green"),weechat.color("reset"))],
["mana-rp","%sΦ%s",(weechat.color("black,red"),weechat.color("reset"))],
["mana-rw","%sR%s/%sW%s",(weechat.color("black,red"),weechat.color("black,gray"),weechat.color("black,white"),weechat.color("reset"))],
["mana-s","%s❄%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-t","%s↷%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-u","%sU%s",(weechat.color("black,blue"),weechat.color("reset"))],
["mana-ub","%sU%s/B%s",(weechat.color("black,blue"),weechat.color("black,gray"),weechat.color("reset"))],
["mana-up","%sΦ%s",(weechat.color("black,blue"),weechat.color("reset"))],
["mana-ur","%sU%s/%sR%s",(weechat.color("black,blue"),weechat.color("black,gray"),weechat.color("black,red"),weechat.color("reset"))],
["mana-w","%sW%s",(weechat.color("black,white"),weechat.color("reset"))],
["mana-wb","%sW%s/B%s",(weechat.color("black,white"),weechat.color("black,gray"),weechat.color("reset"))],
["mana-wp","%sΦ%s",(weechat.color("black,white"),weechat.color("reset"))],
["mana-wu","%sW%s/%sU%s",(weechat.color("black,white"),weechat.color("black,gray"),weechat.color("black,blue"),weechat.color("reset"))],
["mana-x","%sX%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-y","%sY%s",(weechat.color("black,gray"),weechat.color("reset"))],
["mana-z","%sZ%s",(weechat.color("black,gray"),weechat.color("reset"))]
]
urlreg = re.compile('(https://scryfall.com/card.*utm_source=.*)\)')
imgreg = re.compile('border-.*src="(.*jpg)')
date1 = re.compile('!date.*?{time} ')
def mtgchange_cb(data, modifier, modifier_data, string):
string = string.replace("IRCCloud :]","crypt")
string = string.replace("dxbot2.0 :]","[dxboi]")
string = string.replace("Scryfall :]","[scryfall]")
if mtgmatch.match(string) or halfmatch.match(string):
w = 0
string = string.replace("::",": :").replace("00-1: :","00-1::").replace("00-2: :","00-2::").replace("00-3: :","00-3::").replace("00-4: :","00-4::")
for x in re.findall(urlreg, string):
w = 1
if w == 0:
string = string.replace("_","").replace("*","")
y = 0
for x in swaplist:
secmatch = re.compile("(:"+swaplist[y][0]+":)")
for z in re.findall(secmatch, string):
string = string.replace(":"+swaplist[y][0]+":",swaplist[y][1],1) % swaplist[y][2]
y = y + 1
for x in re.findall(urlreg, string):
urlfir = str(x)
page = urllib2.urlopen(urlfir)
for z in re.findall(imgreg, page.read()):
string = string.replace(urlfir, str(z))
return string
for x in re.findall(date1, string):
string = string.replace(str(x), "")
for x in re.findall(urlmatch, string):
return string
for x in re.findall(boldmatch, string):
string = string.replace("*",weechat.color("bold"),1).replace("*",weechat.color("-bold"),1)
for x in re.findall(itamatch, string):
string = string.replace("_",weechat.color("italic"),1).replace("_",weechat.color("-italic"),1)
return string
weechat.hook_modifier("weechat_print", "mtgchange_cb", "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment