<reponame>TES286/TMCL<gh_stars>1-10
import os.path
import re
from os import mkdir as _mkdir

import requests


def mkdir(path: str):
    path = path.split('/')
    _path = ''
    for i in range(len(path)):
        _path = (_path + path[i] + '/').replace('//', '/')
        if (os.path.isdir(_path)) and (not os.path.exists(_path)):
            # print(_path)
            _mkdir(_path)


def download(url, file_name):
    headers = {'user-agent': 'Mozilla/5.0 (compatible; TMCLDownload/0.1; +http://tmcl.tes286.top/download.html)'}

    file_name = os.path.expandvars(file_name)

    r = requests.get(url, stream=True, headers=headers)
    if not os.path.exists(file_name):
        path = re.findall('(.*/)*(.+)', file_name.replace('\\', '/'))[0][0]
        mkdir(path)

    chunk_size = 1024
    with open(file_name, "wb+") as file:
        for data in r.iter_content(chunk_size=chunk_size):
            file.write(data)
    r.close()


def dl_version(mirror='bmclapi'):
    url = 'https://bmclapi2.bangbang93.com/mc/game/version_manifest.json'
    headers = {'user-agent': 'Mozilla/5.0 (compatible; TMCLDownload/0.1; +http://tmcl.tes286.top/download.html)'}

    if mirror == 'mojang':
        url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json'

    elif mirror == 'bmclapi':
        url = 'https://bmclapi2.bangbang93.com/mc/game/version_manifest.json'

    elif mirror == 'mcbbs':
        url = 'https://download.mcbbs.net/mc/game/version_mainfest.json'

    date = requests.get(url, headers=headers)
    if date.ok:
        return {'ok': True, 'date': date.json()['versions']}

    else:
        return {'ok': False, 'date': {}}


def dl_game_json(version_name, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/{0}/{0}.json'.format(version_name))


def dl_game_jar(version_name, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/{0}/{0}.jar'.format(version_name))


def dl_game_assets_json(version, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/assets/index/{}.json'.format(version))


def dl_game_assets_file(file_hash, mirror='bmclapi'):
    main_url = 'https://bmclapi2.bangbang93.com/assets/'
    if mirror == 'mojang':
        main_url = 'https://resources.download.minecraft.net/'

    elif mirror == 'bmclapi':
        main_url = 'https://bmclapi2.bangbang93.com/assets/'

    elif mirror == 'mcbbs':
        main_url = 'https://download.mcbbs.net/assets/'

    path = '{}/{}'.format(file_hash[:2], file_hash)

    download(url=main_url + path, file_name='.minecraft/assets/objects/' + path)


def dl_game_libraries(url, mirror="bmclapi"):
    if mirror == 'bmclapi':
        url = url.replace('https://libraries.minecraft.net/', 'https://bmclapi2.bangbang93.com/maven')

    elif mirror == 'mcbbs':
        url = url.replace('https://libraries.minecraft.net/', 'https://download.mcbbs.com/maven')

    download(url=url, file_name='.minecraft/libraries/{}'.format(url.replace('https://libraries.minecraft.net/', '')
                                                                 .replace('https://bmclapi2.bangbang93.com/maven/', '')
                                                                 .replace('https://download.mcbbs.com/maven/', '')))

import os.path
import re
from os import mkdir as _mkdir

import requests


def mkdir(path: str):
    path = path.split('/')
    _path = ''
    for i in range(len(path)):
        _path = (_path + path[i] + '/').replace('//', '/')
        if (os.path.isdir(_path)) and (not os.path.exists(_path)):
            # print(_path)
            _mkdir(_path)


def download(url, file_name):
    headers = {'user-agent': 'Mozilla/5.0 (compatible; TMCLDownload/0.1; +http://tmcl.tes286.top/download.html)'}

    file_name = os.path.expandvars(file_name)

    r = requests.get(url, stream=True, headers=headers)
    if not os.path.exists(file_name):
        path = re.findall('(.*/)*(.+)', file_name.replace('\\', '/'))[0][0]
        mkdir(path)

    chunk_size = 1024
    with open(file_name, "wb+") as file:
        for data in r.iter_content(chunk_size=chunk_size):
            file.write(data)
    r.close()


def dl_version(mirror='bmclapi'):
    url = 'https://bmclapi2.bangbang93.com/mc/game/version_manifest.json'
    headers = {'user-agent': 'Mozilla/5.0 (compatible; TMCLDownload/0.1; +http://tmcl.tes286.top/download.html)'}

    if mirror == 'mojang':
        url = 'https://launchermeta.mojang.com/mc/game/version_manifest.json'

    elif mirror == 'bmclapi':
        url = 'https://bmclapi2.bangbang93.com/mc/game/version_manifest.json'

    elif mirror == 'mcbbs':
        url = 'https://download.mcbbs.net/mc/game/version_mainfest.json'

    date = requests.get(url, headers=headers)
    if date.ok:
        return {'ok': True, 'date': date.json()['versions']}

    else:
        return {'ok': False, 'date': {}}


def dl_game_json(version_name, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/{0}/{0}.json'.format(version_name))


def dl_game_jar(version_name, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/{0}/{0}.jar'.format(version_name))


def dl_game_assets_json(version, url, mirror='bmclapi'):
    if mirror == 'mojang':
        pass

    elif mirror == 'bmclapi':
        url = url.replace('https://launchermeta.mojang.com/', 'https://bmclapi2.bangbang93.com/')
        url = url.replace('https://launcher.mojang.com/', 'https://bmclapi2.bangbang93.com/')

    elif mirror == 'mcbbs':
        url = url.replace('https://launchermeta.mojang.com/', 'https://download.mcbbs.net/')
        url = url.replace('https://launcher.mojang.com/', 'https://download.mcbbs.net/')

    download(url=url, file_name='.minecraft/assets/index/{}.json'.format(version))


def dl_game_assets_file(file_hash, mirror='bmclapi'):
    main_url = 'https://bmclapi2.bangbang93.com/assets/'
    if mirror == 'mojang':
        main_url = 'https://resources.download.minecraft.net/'

    elif mirror == 'bmclapi':
        main_url = 'https://bmclapi2.bangbang93.com/assets/'

    elif mirror == 'mcbbs':
        main_url = 'https://download.mcbbs.net/assets/'

    path = '{}/{}'.format(file_hash[:2], file_hash)

    download(url=main_url + path, file_name='.minecraft/assets/objects/' + path)


def dl_game_libraries(url, mirror="bmclapi"):
    if mirror == 'bmclapi':
        url = url.replace('https://libraries.minecraft.net/', 'https://bmclapi2.bangbang93.com/maven')

    elif mirror == 'mcbbs':
        url = url.replace('https://libraries.minecraft.net/', 'https://download.mcbbs.com/maven')

    download(url=url, file_name='.minecraft/libraries/{}'.format(url.replace('https://libraries.minecraft.net/', '')
                                                                 .replace('https://bmclapi2.bangbang93.com/maven/', '')
                                                                 .replace('https://download.mcbbs.com/maven/', '')))
# print(_path)