def meme_gen(request): if len(request) == 2: toptext = "" bottomtext = "" elif len(request) == 3: toptext = "" bottomtext = urllib.parse.quote_plus(request[2]) elif len(request) < 4: return "Wrong number of parameters. Usage /memegen <meme> '<text1>' '<text2>'" else: toptext = urllib.parse.quote_plus(request[2]) bottomtext = urllib.parse.quote_plus(request[3]) request[1] = links.remove_braces_from_link(request[1]) if request[1] not in Dict: if (request[1]) not in links.Links: pass else: # request is in links dictionary request[1] = links.Links[request[1]] # change request to link request[1] = processRedditAndImgurURL(request[1]) parsed_url = urllib.parse.urlparse(request[1]) if not bool(parsed_url.scheme): return "Meme %s not found. Did you mean: %s" % ( request[1], "'" + "', '".join(find_memes_contain(request[1])) + "'" + "', '".join(find_links_contain(request[1])) + "'") maintype = mimetypes.guess_type(parsed_url.path)[0] if maintype not in ('image/png', 'image/jpeg'): return "URL is not a png or jpeg" retval = build_meme_from_link(request, toptext, bottomtext) else: # request in meme dictionary retval = "http://apimeme.com/meme?meme=%s&top=%s&bottom=%s" % ( Dict[request[1]], toptext, bottomtext) if imgur_api.logged_in(): data = urllib.parse.urlencode({'image': retval}) binary_data = data.encode('ASCII') req = urllib.request.Request("https://api.imgur.com/3/upload", data=binary_data, headers=imgur_api.build_header()) send_http_query(req) with open("meme_history", "a") as file: dt = datetime.datetime.now() #global last_sender file.write("[%d-%02d-%02d-%02d:%02d:%02d] <%s> %s\n" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, "NOTIMPLEMENTED", retval)) return retval
def build_meme_gen(request): if len(request) == 2: toptext = "" bottomtext = "" elif len(request) < 4: return "Wrong number of parameters. Usage /memegen <meme> '<text1>' '<text2>'" else: toptext = urllib.parse.quote_plus(request[2]) bottomtext = urllib.parse.quote_plus(request[3]) if request[1] not in meme.Dict: if(request[1]) not in Links: pass else: # request is in links dictionary request[1] = Links[request[1]] # change request to link url = request[1] parsed_url = urllib.parse.urlparse(url) if not bool(parsed_url.scheme): return "Meme %s not found. Did you mean: %s" % (request[1], "'" + "', '".join(find_memes_contain(request[1])) + "'" + "', '".join(find_links_contain(request[1])) + "'") maintype = mimetypes.guess_type(parsed_url.path)[0] if maintype not in ('image/png', 'image/jpeg'): return "URL is not a png or jpeg" retval = build_meme_from_link(request) else: # request in meme dictionary retval = "http://apimeme.com/meme?meme=%s&top=%s&bottom=%s" % (meme.Dict[request[1]], toptext, bottomtext) if imgur_api.logged_in(): data = urllib.parse.urlencode({'image': retval}) binary_data = data.encode('ASCII') req = urllib.request.Request("https://api.imgur.com/3/upload", data=binary_data, headers=imgur_api.build_header()) send_http_query(req) with open("meme_history", "a") as file: dt = datetime.datetime.now() file.write("[%d-%02d-%02d-%02d:%02d:%02d] <%s> %s\n" % (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, requester, retval)) return retval
def build_imgur_pic(request): if type(request) == list and len(request) == 2: req = urllib.request.Request("https://api.imgur.com/3/gallery/r/" + request[1] + "/top/week", headers=imgur_api.build_header()) log("logged in as %s" % imgur_api.get_bot_username()) response = send_http_query(req) # urllib.request.urlopen(req).read() if response_query: json_data = json.loads(response.decode('utf-8')) if json_data['data']: # data is available link_id = random.randint(0, len(json_data['data']) - 1) retval = (str(json_data['data'][link_id]['title']) + " - " + str(json_data['data'][link_id]['link'] + ("v" if(str(json_data['data'][link_id]['link']).endswith(".gif")) else ""))) return retval else: return "images in subreddit '%s' not found in the past day" % request[1] else: return "internal error. please try again." else: return "Wrong number of parameters. Usage /getpic [subreddit]"
def build_meme_from_link(request, toptext_, bottomtext_): toptext = urllib.parse.unquote_plus(toptext_) bottomtext = urllib.parse.unquote_plus(bottomtext_) if not toptext and not bottomtext: return request[1] response = send_http_query(request[1]) if response: file = io.BytesIO(response) else: return "URL not found" log("image loaded from %s " % request[1]) basewidth = 640 # resize file to base width 500 img = Image.open(file) wpercent = (basewidth / float(img.size[0])) if 0.9 <= wpercent <= 1.1: hsize = int(float(img.size[1]) * float(wpercent)) img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) log("image resized") draw = ImageDraw.Draw(img) shadowcolor = "black" fillcolor = "white" width = img.size[0] height = img.size[1] textwidth = (width * 80) / 100 textheight = (height * 18) / 100 maxFontSize = 300 # search appropriate font size for i in range(maxFontSize): font = ImageFont.truetype(settings.font_location, i) toptextsize = font.getsize(toptext) if textwidth < toptextsize[0]: break if textheight < toptextsize[1]: break # draw top text DrawOutlinedText(draw, ((width - toptextsize[0]) / 2, 5), toptext, font=font, outline=shadowcolor, fill=fillcolor) # search appropriate font size bottextsize = (0, 0) for i in range(maxFontSize): font = ImageFont.truetype(settings.font_location, i) bottextsize = font.getsize(bottomtext) # workaround for older PIL version that is used by pythonanywhere bottextheight = i if textwidth < bottextsize[0]: break if textheight < bottextsize[1]: break # draw bottom text DrawOutlinedText( draw, ((width - bottextsize[0]) / 2, height - bottextheight - 10), bottomtext, font=font, outline=shadowcolor, fill=fillcolor) img = img.convert("RGB") img.save(settings.image_temp_file, quality=50) log("Text added to the image") with open(settings.image_temp_file, "rb") as file: data = urllib.parse.urlencode({'image': b64encode(file.read())}) binary_data = data.encode('ASCII') log("Upload start") req = urllib.request.Request("https://api.imgur.com/3/upload", data=binary_data, headers=imgur_api.build_header()) log("logged in as %s" % imgur_api.get_bot_username()) response = send_http_query(req) if response: log("Upload finish") json_data = json.loads(response.decode('utf-8')) return json_data['data']['link'] else: log("Upload failed") return "Upload to imgur failed"
def build_meme_from_link(request): toptext = request[2] if len(request) == 4 else "" bottomtext = request[3] if len(request) == 4 else "" if not toptext and not bottomtext: return request[1] response = send_http_query(request[1]) if response: file = io.BytesIO(response) else: return "URL not found" log("image loaded from %s " % request[1]) basewidth = 640 # resize file to base width 500 img = Image.open(file) wpercent = (basewidth / float(img.size[0])) if 0.9 <= wpercent <= 1.1: hsize = int(float(img.size[1]) * float(wpercent)) img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) log("image resized") draw = ImageDraw.Draw(img) shadowcolor = "black" fillcolor = "white" width = img.size[0] height = img.size[1] textwidth = width - 100 # search appropriate font size for i in range(100): font = ImageFont.truetype(settings.font_location, i) toptextsize = font.getsize(toptext)[0] if textwidth < toptextsize: break # draw top text DrawOutlinedText(draw, ((width - toptextsize) / 2, 5), toptext, font=font, outline=shadowcolor, fill=fillcolor) # search appropriate font size bottextsize = 0 for i in range(100): font = ImageFont.truetype(settings.font_location, i) bottextsize = font.getsize(bottomtext)[0] # workaround for older PIL version that is used by pythonanywhere bottextheight = i # font.getsize(bottomtext)[1] if textwidth < bottextsize: break # draw bottom text DrawOutlinedText(draw, ((width - bottextsize) / 2, height - bottextheight - 10), bottomtext, font=font, outline=shadowcolor, fill=fillcolor) img.save(settings.image_temp_file, quality=50) log("Text added to the image") with open(settings.image_temp_file, "rb") as file: data = urllib.parse.urlencode({'image': b64encode(file.read())}) binary_data = data.encode('ASCII') log("Upload start") req = urllib.request.Request("https://api.imgur.com/3/upload", data=binary_data, headers=imgur_api.build_header()) log("logged in as %s" % imgur_api.get_bot_username()) response = send_http_query(req) if response: log("Upload finish") json_data = json.loads(response.decode('utf-8')) return json_data['data']['link'] else: log("Upload failed") return "Upload to imgur failed"