'How do I get description or any info from imgur properly using python?

I have a public url "https://imgur.com/a/vKe2kEK" that I wish query images from to send to respective users in telegram.

I am referring to an old piece of code here. I face quite a lot of troubles when trying to get the "description" and "image_link" from the imgurHTML = requests.get(get_blog_layout(url))'s text object.

Below is a working and hardcoded example, I believe my regex is not doing too well here as it returns irrelevant information as well.


import json
import requests
import re

url = "https://imgur.com/a/vKe2kEK"


def get_blog_layout(starturl):
    """
    Uses regualar expression to convert the input url to an /layout/blog
    url where all the hashes are within the html. Returns a string with
    url.
    """
    regex = r"imgur.com\/a\/([\w\d]*)"
    urlhash = re.search(regex, starturl)
    try:
        return "https://imgur.com/a/{0}/layout/blog".format(urlhash.group(1))
    except:
        raise Exception("Album Link couldn't get converted")


regex = r"\{\"hash\":\"([\w\d]*)\"\,\"title\".*?\"ext\"\:\"(\.jpg|.png|.gif|.gifv|.mp4)\".*?\}"
# description_regex = r"\"description\"\:\"(.*?)\""
description_regex = r"\"description\"\:\"(.*?)\""
imgurHTML = requests.get(get_blog_layout(url))
print(imgurHTML.text)

usernames = re.findall(description_regex, imgurHTML.text)
print(usernames)
usernames = usernames[4:]
imgurhashes = re.findall(regex, imgurHTML.text)
print(imgurhashes)

Expected output from usernames is: usernames = ['Arrowsoftime_nct1', 'gaojunn_nct1', 'PortJacksonShark_nct1']

Expected output from imgurhashes is: imgurhashes = [('jRj8mCK', '.jpg'), ('xAo81Vf', '.jpg'), ('lRB66WQ', '.jpg')]



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source