Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active August 29, 2015 14:03
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 sfan5/c998d5b316bfb9b13703 to your computer and use it in GitHub Desktop.
Save sfan5/c998d5b316bfb9b13703 to your computer and use it in GitHub Desktop.
MediaCrush URL Info Module for phenny
import re
import web
import json
mc_re = r'.*https?:\/\/(?:www\.|cdn\.)?mediacru\.sh\/([a-zA-Z0-9_-]{12})(?:\.webm|\.mp4|\.ogv|\.jpg|\.png|\.gif)?.*'
def durationconvert(inp):
secs = int(inp)
hrs = secs // 3600
secs -= 3600*hrs
mins = secs // 60
secs -= 60*mins
return ((str(hrs)+":" if hrs else "") +
(str(mins)+":" if mins else "") + str(secs))
def resolutionconvert(w, h):
restbl = {
"320x240": "240p (4:3)",
"426x240": "240p",
"480x360": "360p (4:3)",
"640x360": "360p",
"640x480": "480p (4:3)",
"854x480": "480p",
"960x540": "qHD (960x540)",
"1280x720": "720p",
"1920x1080": "1080p",
"3840x2160": "2160p (4K UHD)",
}
s = "%sx%s" % (w, h)
if s in restbl:
return restbl[s]
else:
return s
def available(key, dic):
return True if dic != None and key in dic and dic[key] != None else False
def mediacrush(phenny, match):
data = json.loads(str(web.get("https://mediacru.sh/api/" + match.group(1))[0], 'utf-8'))
if not "metadata" in data:
return
if data["blob_type"] == "audio":
artist = data["metadata"]["artist"]+" - " if available("artist", data["metadata"]) else data["metadata"]["composer"]+" - " if available("composer", data["metadata"]) else ""
duration = " " + durationconvert(data["metadata"]["duration"]) if available("duration", data["metadata"]) else ""
s = "MediaCrush: Audio, " + artist + (data["metadata"]["title"] if available("title", data["metadata"]) else "No Title") + duration
elif data["blob_type"] == "video":
if "dimensions" in data["metadata"]:
resolution = ", " + resolutionconvert(data["metadata"]["dimensions"]["width"], data["metadata"]["dimensions"]["height"])
else:
resolution = ""
duration = " " + durationconvert(data["metadata"]["duration"]) if available("duration", data["metadata"]) else ""
if available("has_audio", data["metadata"]):
has_audio = data["metadata"]["has_audio"]
else:
has_audio = False
if available("has_subtitles", data["metadata"]):
has_subs = data["metadata"]["has_subtitles"]
else:
has_subs = False
tmp = ""
if not has_audio:
tmp += " without audio"
if has_subs:
tmp += "," if not has_audio else " with" + " subtitles"
s = "MediaCrush: Video%s%s" % (tmp, resolution) + duration
elif data["blob_type"] == "image":
if available("dimensions", data["metadata"]):
resolution = "%sx%s image" % (data["metadata"]["dimensions"]["width"], data["metadata"]["dimensions"]["height"])
else:
resolution = "Image with unknown dimensions"
s = "MediaCrush: " + resolution
elif data["blob_type"] == "album":
ctypes = []
for f in data["files"]:
if not f["blob_type"] in ctypes:
ctypes.append(f["blob_type"])
nnswf = 0
for f in data["files"]:
if available("nsfw", f["flags"]) and f["flags"]["nsfw"]:
nnswf += 1
if len(ctypes) == 1:
ctypes = [ctypes[0] + "s"]
s = "MediaCrush: Album with %d files (%s)" % (len(data["files"]), ', '.join(ctypes))
if nnswf == 0:
pass
elif len(data["files"]) == nnsfw:
s += ", NSFW"
else: # len(data["files"]) > nnsfw > 0
s += ", partly NSFW"
if data["blob_type"] in ["image", "video", "audio"] and available("nsfw", data["flags"]) and data["flags"]["nsfw"]:
s += ", NSFW"
phenny.say(s)
mediacrush.rule = mc_re
mediacrush.priority = 'medium'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment