import requests
import re

def GetTargetNameFromReq(asset_name):
    return asset_name.split('.')[0]

def GetConfigNameFromReq(asset_name):
    conf = asset_name.split('.')[1][0]
    if conf == 'R':
        return 'Release'
    elif conf == 'M':
        return 'Mixed'
    elif conf == 'D':
        return 'Debug'
    else:
        raise Exception('Wrong asset file name. Maybe format changed.')

def GetPlatformNameFromReq(asset_name):
    return re.compile('[rmdRMD]').sub('', asset_name.split('.')[1])

def IsLatestRelease(uri, release_name):
    req = requests.get(uri + '/latest')
    if req.json()['name'] == release_name:
        return True
    return False

def GetReleasesDict(uri):
    """Get releases from github and make dictionary. Returns the dictionary.

    Keyword arguments:  
    uri -- link to GitHub API /:repo/releases

    """
    req = requests.get(uri)
    result = dict()
    for r in req.json():
        release_data = dict()
        config = None
        current_target_name = None
        for asset in r['assets']:
            if current_target_name != GetTargetNameFromReq(asset['name']):
                config = dict()
                current_target_name = GetTargetNameFromReq(asset['name'])
            build_name = GetConfigNameFromReq(asset['name']) + '_' + GetPlatformNameFromReq(asset['name'])
            config[build_name] = asset['browser_download_url']
            release_data[current_target_name] = config
        release_data['prerelease'] = r['prerelease']
        release_data['latest'] = IsLatestRelease(uri, r['name'])
        result[r['name']] = release_data
    # print(result)
    return result
import requests
import re

def GetTargetNameFromReq(asset_name):
    return asset_name.split('.')[0]

def GetConfigNameFromReq(asset_name):
    conf = asset_name.split('.')[1][0]
    if conf == 'R':
        return 'Release'
    elif conf == 'M':
        return 'Mixed'
    elif conf == 'D':
        return 'Debug'
    else:
        raise Exception('Wrong asset file name. Maybe format changed.')

def GetPlatformNameFromReq(asset_name):
    return re.compile('[rmdRMD]').sub('', asset_name.split('.')[1])

def IsLatestRelease(uri, release_name):
    req = requests.get(uri + '/latest')
    if req.json()['name'] == release_name:
        return True
    return False

def GetReleasesDict(uri):
    """Get releases from github and make dictionary. Returns the dictionary.

    Keyword arguments:  
    uri -- link to GitHub API /:repo/releases

    """
    req = requests.get(uri)
    result = dict()
    for r in req.json():
        release_data = dict()
        config = None
        current_target_name = None
        for asset in r['assets']:
            if current_target_name != GetTargetNameFromReq(asset['name']):
                config = dict()
                current_target_name = GetTargetNameFromReq(asset['name'])
            build_name = GetConfigNameFromReq(asset['name']) + '_' + GetPlatformNameFromReq(asset['name'])
            config[build_name] = asset['browser_download_url']
            release_data[current_target_name] = config
        release_data['prerelease'] = r['prerelease']
        release_data['latest'] = IsLatestRelease(uri, r['name'])
        result[r['name']] = release_data
    # print(result)
    return result
Get releases from github and make dictionary. Returns the dictionary.      Keyword arguments:       uri -- link to GitHub API /:repo/releases # print(result)