# Path: modules/goglib_recreate_banner.py
# def goglib_recreate_banner(game_name, banner_path):
# 
#     nebula_dir = os.getenv('NEBULA_DIR')
#     banner = Image.new('1', (518, 240), 0)
#     draw = ImageDraw.Draw(banner)
#     font = ImageFont.truetype(nebula_dir + '/fonts/DejaVuSans.ttf', 24)
#     game_name_width = font.getsize(game_name)[0]
#     game_name_height = font.getsize(game_name)[1]
# 
#     if game_name_width > 518:
# 
#         game_name_list = []
#         for i in range(0, len(game_name), 28):
#             game_name_list.append(game_name[i:i+28])
# 
#         all_lines_height = len(game_name_list) * game_name_height
#         full_title_y = 120 - all_lines_height/2
# 
#         n = 0
#         for line in game_name_list:
#             line_width = font.getsize(line)[0]
#             x = 259 - line_width/2
#             y = full_title_y + (game_name_height * n)
#             draw.text((x, y), line, fill=1, font=font)
#             n += 1
# 
#     else:
# 
#         x = 259 - game_name_width/2
#         y = 120 - game_name_height/2
#         draw.text((x, y), game_name, fill=1, font=font)
# 
#     banner.save(banner_path, 'JPEG')


import os
import PIL
from PIL import Image
from gogapi import GogApi, Token
from modules import goglib_recreate_banner
    from urllib2 import Request as urllib_request
    from urllib2 import urlopen as urllib_urlopen
    from urllib2 import URLError as urllib_urlerror
    from urllib2 import HTTPError as urllib_httperror
    from urllib.request import Request as urllib_request
    from urllib.request import urlopen as urllib_urlopen
    from urllib.request import URLError as urllib_urlerror
    from urllib.request import HTTPError as urllib_httperror
        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:

# Path: modules/goglib_recreate_banner.py
# def goglib_recreate_banner(game_name, banner_path):
# 
#     nebula_dir = os.getenv('NEBULA_DIR')
#     banner = Image.new('1', (518, 240), 0)
#     draw = ImageDraw.Draw(banner)
#     font = ImageFont.truetype(nebula_dir + '/fonts/DejaVuSans.ttf', 24)
#     game_name_width = font.getsize(game_name)[0]
#     game_name_height = font.getsize(game_name)[1]
# 
#     if game_name_width > 518:
# 
#         game_name_list = []
#         for i in range(0, len(game_name), 28):
#             game_name_list.append(game_name[i:i+28])
# 
#         all_lines_height = len(game_name_list) * game_name_height
#         full_title_y = 120 - all_lines_height/2
# 
#         n = 0
#         for line in game_name_list:
#             line_width = font.getsize(line)[0]
#             x = 259 - line_width/2
#             y = full_title_y + (game_name_height * n)
#             draw.text((x, y), line, fill=1, font=font)
#             n += 1
# 
#     else:
# 
#         x = 259 - game_name_width/2
#         y = 120 - game_name_height/2
#         draw.text((x, y), game_name, fill=1, font=font)
# 
#     banner.save(banner_path, 'JPEG')


# Path: modules/goglib_get_banner.py
import os
import PIL
from PIL import Image
from gogapi import GogApi, Token
from modules import goglib_recreate_banner
    from urllib2 import Request as urllib_request
    from urllib2 import urlopen as urllib_urlopen
    from urllib2 import URLError as urllib_urlerror
    from urllib2 import HTTPError as urllib_httperror
    from urllib.request import Request as urllib_request
    from urllib.request import urlopen as urllib_urlopen
    from urllib.request import URLError as urllib_urlerror
    from urllib.request import HTTPError as urllib_httperror

        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)