# Path: modules/goglib_get_banner.py
# def goglib_get_banner(banner_path, unavailable_path, game_id, *args):
# 
#     banner_height = 240
#     game_name = os.path.basename(banner_path).split('.jpg')[0]
#     print("Getting picture for: '" + game_name + "'")
# 
#     try:
#         token_path = os.getenv('HOME') + '/.config/lgogdownloader/galaxy_tokens.json'
#         token = Token.from_file(token_path)
#         if token.expired():
#             token.refresh()
#             token.save(token_path)
#         api = GogApi(token)
# 
#         prod = api.product(game_id)
#         prod.update_galaxy(expand=True)
#         banner_url = 'https:' + ''.join(prod.image_logo.split('_glx_logo'))
# 
#         banner_req = urllib_request(banner_url)
#         banner_data = urllib_urlopen(banner_req).read()
#         banner_file = open(banner_path, 'wb')
#         banner_file.write(banner_data)
#         banner_file.close()
# 
#         pic_src = Image.open(banner_path)
#         scale_lvl = banner_height/float(pic_src.size[1])
#         scaled_width = int(float(pic_src.size[0])*scale_lvl)
#         pic = pic_src.resize((scaled_width, banner_height), PIL.Image.ANTIALIAS)
#         pic.save(banner_path)
#         pic = pic.convert('L')
#         pic.save(unavailable_path)
# 
#     except urllib_urlerror as e:
#         print(e.reason)
#     except urllib_httperror as e:
#         print(e.code)
#         print(e.read())
#     except:
#         goglib_recreate_banner.goglib_recreate_banner(game_name, banner_path)


import os, sys
from modules import goglib_get_banner
        for line in in_file:
            if 'gamename: ' in line:
                game_name = line.split('gamename: ',1)[1].replace('\n', '')
                list_names.append(game_name)
            if 'product id: ' in line:
                game_id = line.split('product id: ',1)[1].replace('\n', '')
                name2id[game_name] = game_id
            if 'title: ' in line:
                list_titles.append(line.split('title: ',1)[1].replace('\n', ''))
            if 'icon: ' in line:
                list_icons.append(line.split('icon: ',1)[1].replace('\n', ''))
    in_file.close()

    number_of_games = len(list_names)

    if not os.path.exists(data_dir + '/images'):
        os.makedirs(data_dir + '/images')
    if not os.path.exists(data_dir + '/images/goglib'):
        os.makedirs(data_dir + '/images/goglib')
    if not os.path.exists(data_dir + '/images/goglib/unavailable'):
        os.makedirs(data_dir + '/images/goglib/unavailable')

    for i in range(0, number_of_games):

        banner_path_0 = data_dir + '/images/goglib/' + list_names[i] + '.jpg'
        banner_path_1 = nebula_dir + '/images/goglib/' + list_names[i] + '.jpg'
        unavailable_path = data_dir + '/images/goglib/unavailable/' + list_names[i] + '.jpg'
        game_id = name2id.get(list_names[i], None)

        if (not os.path.exists(banner_path_0)) and (not os.path.exists(banner_path_1)):

# Path: modules/goglib_get_banner.py
# def goglib_get_banner(banner_path, unavailable_path, game_id, *args):
# 
#     banner_height = 240
#     game_name = os.path.basename(banner_path).split('.jpg')[0]
#     print("Getting picture for: '" + game_name + "'")
# 
#     try:
#         token_path = os.getenv('HOME') + '/.config/lgogdownloader/galaxy_tokens.json'
#         token = Token.from_file(token_path)
#         if token.expired():
#             token.refresh()
#             token.save(token_path)
#         api = GogApi(token)
# 
#         prod = api.product(game_id)
#         prod.update_galaxy(expand=True)
#         banner_url = 'https:' + ''.join(prod.image_logo.split('_glx_logo'))
# 
#         banner_req = urllib_request(banner_url)
#         banner_data = urllib_urlopen(banner_req).read()
#         banner_file = open(banner_path, 'wb')
#         banner_file.write(banner_data)
#         banner_file.close()
# 
#         pic_src = Image.open(banner_path)
#         scale_lvl = banner_height/float(pic_src.size[1])
#         scaled_width = int(float(pic_src.size[0])*scale_lvl)
#         pic = pic_src.resize((scaled_width, banner_height), PIL.Image.ANTIALIAS)
#         pic.save(banner_path)
#         pic = pic.convert('L')
#         pic.save(unavailable_path)
# 
#     except urllib_urlerror as e:
#         print(e.reason)
#     except urllib_httperror as e:
#         print(e.code)
#         print(e.read())
#     except:
#         goglib_recreate_banner.goglib_recreate_banner(game_name, banner_path)


# Path: modules/goglib_get_data.py
import os, sys
from modules import goglib_get_banner

        for line in in_file:
            if 'gamename: ' in line:
                game_name = line.split('gamename: ',1)[1].replace('\n', '')
                list_names.append(game_name)
            if 'product id: ' in line:
                game_id = line.split('product id: ',1)[1].replace('\n', '')
                name2id[game_name] = game_id
            if 'title: ' in line:
                list_titles.append(line.split('title: ',1)[1].replace('\n', ''))
            if 'icon: ' in line:
                list_icons.append(line.split('icon: ',1)[1].replace('\n', ''))
    in_file.close()

    number_of_games = len(list_names)

    if not os.path.exists(data_dir + '/images'):
        os.makedirs(data_dir + '/images')
    if not os.path.exists(data_dir + '/images/goglib'):
        os.makedirs(data_dir + '/images/goglib')
    if not os.path.exists(data_dir + '/images/goglib/unavailable'):
        os.makedirs(data_dir + '/images/goglib/unavailable')

    for i in range(0, number_of_games):

        banner_path_0 = data_dir + '/images/goglib/' + list_names[i] + '.jpg'
        banner_path_1 = nebula_dir + '/images/goglib/' + list_names[i] + '.jpg'
        unavailable_path = data_dir + '/images/goglib/unavailable/' + list_names[i] + '.jpg'
        game_id = name2id.get(list_names[i], None)

        if (not os.path.exists(banner_path_0)) and (not os.path.exists(banner_path_1)):

            goglib_get_banner.goglib_get_banner(banner_path_0, unavailable_path, game_id)