Home > Net >  How to post request per line in text file with threading
How to post request per line in text file with threading

Time:09-27

I created an username checker against the anilist api. It basically checks usernames for availability to get 'rare' usernames. I'm trying to speed up the process by using threading but instead of checking different usernames it checks one username fifty times. How can I make it so that it checks different usernames per request in the threadpool.

My code:

def req_split(r):
    global checked
    global available

    username = [line.strip() for line in open(f"external/{usernameFile}")]
    for name in username:
        data = {
            "query": "mutation($name:String){CreateUser(userName:$name){id name about avatar{large}bannerImage unreadNotificationCount donatorTier donatorBadge moderatorRoles options{titleLanguage airingNotifications displayAdultContent profileColor notificationOptions{type enabled}}mediaListOptions{scoreFormat rowOrder animeList{customLists sectionOrder splitCompletedSectionByFormat advancedScoring advancedScoringEnabled}mangaList{customLists sectionOrder splitCompletedSectionByFormat advancedScoring advancedScoringEnabled}}}}",
            "variables": {"name":name}}

        headers = {
            "Host": "anilist.co",
            "schema": "internal",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
            "Content-Type": "application/json"}

        url = 'https://anilist.co/graphql'

        req = requests.post(url str(r), headers=headers, json=data) 

        if "userName" in req.text:
            checked  = 1
            print(f"{Fore.LIGHTBLACK_EX}[ ]{Fore.RESET} Taken:       {name}")

        else: 
            checked  = 1
            available  = 1
            print(f"{Fore.LIGHTBLACK_EX}[ ]{Fore.RESET} Available:   {name}")
            with open('external/available.txt', "a") as av:
                av.write(f'{username}\n')

data = range(0,5000)

with Pool(50) as p:
    pm = p.imap_unordered(req_split,data)
    pm = [i for i in pm if i]

CodePudding user response:

Break this out a bit more. Prepare the list of usernames, and then have the the req_split method handle one user at a time. Also you can do the post processing outside of the req_split method and avoid using globals.

def req_split(username):
    data = {
        "query": "mutation($name:String){CreateUser(userName:$name){id name about avatar{large}bannerImage unreadNotificationCount donatorTier donatorBadge moderatorRoles options{titleLanguage airingNotifications displayAdultContent profileColor notificationOptions{type enabled}}mediaListOptions{scoreFormat rowOrder animeList{customLists sectionOrder splitCompletedSectionByFormat advancedScoring advancedScoringEnabled}mangaList{customLists sectionOrder splitCompletedSectionByFormat advancedScoring advancedScoringEnabled}}}}",
        "variables": {"name": username}
    }
    headers = {
        "Host": "anilist.co",
        "schema": "internal",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
        "Content-Type": "application/json"
    }
    url = 'https://anilist.co/graphql'
    req = requests.post(url, headers=headers, json=data)

    return username, "userName" in req.text


if __name__ == "__main__":
    with open(f"external/{usernameFile}", "r") as username_file:
        usernames = [line.strip() for line in username_file]

    checked_count = 0
    available_count = 0
    with Pool(50) as pool:
        for username, available in pool.imap_unordered(req_split, usernames):
            checked_count  = 1
            if available:
                available_count  = 1
                print(f"{Fore.LIGHTBLACK_EX}[ ]{Fore.RESET} Available:   {username}")
                with open('external/available.txt', "a") as av:
                    av.write(f'{username}\n')
            else:
                print(f"{Fore.LIGHTBLACK_EX}[ ]{Fore.RESET} Taken:       {username}")
  • Related