from re import L
import requests
import images as img
import boards as board
import time
import sys
from bs4 import BeautifulSoup
from colorama import Fore

# python3 main.py board "insert the url" "location to store at"

desired_folder = sys.argv[3]
print(Fore.LIGHTGREEN_EX + f"Folder = {desired_folder}"+ Fore.WHITE)


downloaded = 0
def count(one):  # this feature took me 30 minutes...
    global downloaded
    downloaded += int(one) 
    return downloaded - 1

failed_download = 0
def failed(one):
    global failed_download
    failed_download += int(one)
    return failed_download - 1

hashmap = {} # making it global so it can keep track, same with time_out and reset_hashmap
reset_hashmap = 0

def main():
    if sys.argv[1] == "board": #archived threads
        global reset_hashmap
        global hashmap
        print(Fore.WHITE + "Waiting for threads to load...")
        if reset_hashmap != 0:
            print("Waiting for new threads to be added")
            for i in range(10000, 0, -1):
                sys.stdout.write("\r")
                sys.stdout.write("{:2d} seconds remaining.".format(i))
                sys.stdout.flush()
                time.sleep(1)
        reset_hashmap += 1
        if reset_hashmap == 100: #higher, the better, but at cost of memory... idk how much memory, still need test runs
            hashmap = {} # this hard resets the hashmap
            reset_hashmap = 0
            main()
        
        arch_thread = f"{sys.argv[2]}" # gets the archived thread urls
        result = requests.get(arch_thread)
        html = BeautifulSoup(result.text, "html.parser")
        archived_thread = board.get_archived_thread(html)

        while True:
            for i in range(len(archived_thread)):
                if archived_thread[i] in hashmap:
                    main()

                else:
                    # checks if the thread is in the hashmap, if not, it will add into it and download the media 
                    hashmap[archived_thread[i]] = i 
                    board_name = sys.argv[2].split("/")[-2]

                    full_url = f"https://boards.4channel.org/{board_name}/thread/{archived_thread[i]}"  # get the images in that thread
                    result = requests.get(full_url)
                    html = BeautifulSoup(result.text, "html.parser")

                    fileurl = img.get_file_url(html)
                    filename = img.get_file_name(html)
                        
                    while True:
                            for j in range(len(fileurl)):
                                if len(filename) < 3: #change this to lower if you want to download smaller threads, or comment this out
                                    print(Fore.YELLOW + f"Less than 3 images in {full_url}, skipping" + Fore.WHITE)
                                    break
                                try:
                                    one = 1
                                    time.sleep(.6)
                                    if filename[j][-4:] == "webm":
                                        continue
                                    print(f'Downloading "{filename[j]}" from {full_url}')
                                    img.download_file(fileurl[j], filename[j], archived_thread[i], desired_folder, board_name)
                                    count(one) # adds 1 to downloaded stack

                                except (ValueError, OSError):
                                    print(Fore.RED + f'Can\'t get file "{filename[j]}" from {full_url}, could be webm'+ Fore.WHITE) # it does not support webm 
                                    failed(one)
                                    continue
                            break

    if sys.argv[1] == "live": # current active board
        while True:
            print(Fore.WHITE + "Waiting for threads to load...")
            active_url = f"{sys.argv[2]}" # gets the archived thread urls
            html = requests.get(active_url).text
            active_thread = board.get_active_thread(html)

            for i in range(len(active_thread)):
                board_name = sys.argv[2].split("/")[-2]
                
                full_url = f"https://boards.4channel.org/{board_name}/thread/{active_thread[i]}"  # get the images in that thread
                result = requests.get(full_url)
                html = BeautifulSoup(result.text, "html.parser")

                fileurl = img.get_file_url(html)
                filename = img.get_file_name(html)
                print(full_url)
                    
                while True:
                        for j in range(len(fileurl)):
                            try:
                                one = 1
                                time.sleep(1)
                                if filename[j][-4:] == "webm":
                                    continue
                                print(f'Downloading "{filename[j]}" from {full_url}')
                                img.download_file(fileurl[j], filename[j], active_thread[i], desired_folder, board_name)
                                count(one) # adds 1 to downloaded stack

                            except (ValueError, OSError):
                                print(Fore.RED + f'Can\'t get file "{filename[j]}" from {full_url}, could be webm'+ Fore.WHITE) # it does not support webm 
                                failed(one)
                                continue
                        break
                    
if __name__ =='__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("\nDownloaded " + str(count(1)) + " images\nFailed " + str(failed(1)) + " media") 
        print("Made by pach on https://github.com/PachV/fourchannel-thiny")




from re import L
import requests
import images as img
import boards as board
import time
import sys
from bs4 import BeautifulSoup
from colorama import Fore

# python3 main.py board "insert the url" "location to store at"

desired_folder = sys.argv[3]
print(Fore.LIGHTGREEN_EX + f"Folder = {desired_folder}"+ Fore.WHITE)


downloaded = 0
def count(one):  # this feature took me 30 minutes...
    global downloaded
    downloaded += int(one) 
    return downloaded - 1

failed_download = 0
def failed(one):
    global failed_download
    failed_download += int(one)
    return failed_download - 1

hashmap = {} # making it global so it can keep track, same with time_out and reset_hashmap
reset_hashmap = 0

def main():
    if sys.argv[1] == "board": #archived threads
        global reset_hashmap
        global hashmap
        print(Fore.WHITE + "Waiting for threads to load...")
        if reset_hashmap != 0:
            print("Waiting for new threads to be added")
            for i in range(10000, 0, -1):
                sys.stdout.write("\r")
                sys.stdout.write("{:2d} seconds remaining.".format(i))
                sys.stdout.flush()
                time.sleep(1)
        reset_hashmap += 1
        if reset_hashmap == 100: #higher, the better, but at cost of memory... idk how much memory, still need test runs
            hashmap = {} # this hard resets the hashmap
            reset_hashmap = 0
            main()
        
        arch_thread = f"{sys.argv[2]}" # gets the archived thread urls
        result = requests.get(arch_thread)
        html = BeautifulSoup(result.text, "html.parser")
        archived_thread = board.get_archived_thread(html)

        while True:
            for i in range(len(archived_thread)):
                if archived_thread[i] in hashmap:
                    main()

                else:
                    # checks if the thread is in the hashmap, if not, it will add into it and download the media 
                    hashmap[archived_thread[i]] = i 
                    board_name = sys.argv[2].split("/")[-2]

                    full_url = f"https://boards.4channel.org/{board_name}/thread/{archived_thread[i]}"  # get the images in that thread
                    result = requests.get(full_url)
                    html = BeautifulSoup(result.text, "html.parser")

                    fileurl = img.get_file_url(html)
                    filename = img.get_file_name(html)
                        
                    while True:
                            for j in range(len(fileurl)):
                                if len(filename) < 3: #change this to lower if you want to download smaller threads, or comment this out
                                    print(Fore.YELLOW + f"Less than 3 images in {full_url}, skipping" + Fore.WHITE)
                                    break
                                try:
                                    one = 1
                                    time.sleep(.6)
                                    if filename[j][-4:] == "webm":
                                        continue
                                    print(f'Downloading "{filename[j]}" from {full_url}')
                                    img.download_file(fileurl[j], filename[j], archived_thread[i], desired_folder, board_name)
                                    count(one) # adds 1 to downloaded stack

                                except (ValueError, OSError):
                                    print(Fore.RED + f'Can\'t get file "{filename[j]}" from {full_url}, could be webm'+ Fore.WHITE) # it does not support webm 
                                    failed(one)
                                    continue
                            break

    if sys.argv[1] == "live": # current active board
        while True:
            print(Fore.WHITE + "Waiting for threads to load...")
            active_url = f"{sys.argv[2]}" # gets the archived thread urls
            html = requests.get(active_url).text
            active_thread = board.get_active_thread(html)

            for i in range(len(active_thread)):
                board_name = sys.argv[2].split("/")[-2]
                
                full_url = f"https://boards.4channel.org/{board_name}/thread/{active_thread[i]}"  # get the images in that thread
                result = requests.get(full_url)
                html = BeautifulSoup(result.text, "html.parser")

                fileurl = img.get_file_url(html)
                filename = img.get_file_name(html)
                print(full_url)
                    
                while True:
                        for j in range(len(fileurl)):
                            try:
                                one = 1
                                time.sleep(1)
                                if filename[j][-4:] == "webm":
                                    continue
                                print(f'Downloading "{filename[j]}" from {full_url}')
                                img.download_file(fileurl[j], filename[j], active_thread[i], desired_folder, board_name)
                                count(one) # adds 1 to downloaded stack

                            except (ValueError, OSError):
                                print(Fore.RED + f'Can\'t get file "{filename[j]}" from {full_url}, could be webm'+ Fore.WHITE) # it does not support webm 
                                failed(one)
                                continue
                        break
                    
if __name__ =='__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("\nDownloaded " + str(count(1)) + " images\nFailed " + str(failed(1)) + " media") 
        print("Made by pach on https://github.com/PachV/fourchannel-thiny")




# python3 main.py board "insert the url" "location to store at" # this feature took me 30 minutes... # making it global so it can keep track, same with time_out and reset_hashmap #archived threads #higher, the better, but at cost of memory... idk how much memory, still need test runs # this hard resets the hashmap # gets the archived thread urls # checks if the thread is in the hashmap, if not, it will add into it and download the media # get the images in that thread #change this to lower if you want to download smaller threads, or comment this out # adds 1 to downloaded stack # it does not support webm # current active board # gets the archived thread urls # get the images in that thread # adds 1 to downloaded stack # it does not support webm