Example #1
0
def login_imgur(request):
    if len(request) == 1:
        return "Go to the following website: \n"\
               "https://api.imgur.com/oauth2/authorize?client_id=%s&response_type=pin\n"\
               "use command /login_imgur <pin>" % imgur_api.client_id
    elif len(request) == 2:
        result = imgur_api.get_token_from_pin(request[1])
        if result:
            return "Logged in as: " + imgur_api.get_bot_username()
        else:
            return "Login failed. Imgur API might be down, or wrong pin code provided. Please try again"

    else:
        return "Wrong number of parameters. Usage /login_imgur"
Example #2
0
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]"
Example #3
0
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"
Example #4
0
def login_status_imgur(request):
    if imgur_api.get_token():
        return "Logged in as: " + imgur_api.get_bot_username() + "\n" + \
               "Full gallery can be viewed at: " + imgur_api.get_bot_imgur_profile()
    else:
        return "Not logged in"
Example #5
0
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"