Util.py 5.01 KB
import os.path
import json, zipfile, subprocess,urllib
from os import popen
import sys

PATH_RESOURCE = 'c:/vlibras-libs/update/'
DICTIONARY_EXTRACT_PATH = 'C:/vlibras-libs/Player/VLibrasPlayer_Data/Bundles'
DICTIONARY_ZIP_PARH = PATH_RESOURCE+'download/vlibras_dicionario.zip'
TXT_DICT_VERSION = PATH_RESOURCE+'version/dictVersion.txt'
TXT_PLAYER_VERSION = PATH_RESOURCE+'version/playerVersion.txt'
JSON_API_VERSION = PATH_RESOURCE+'download/versionApi.json'
URL_REQUEST_API = 'http://vlibras.lavid.ufpb.br/api/dicionario/'
TEXT_CHECK_INSTALL = 'C:/vlibras-libs/installSucess.txt'
INSTALL_PLAYER_PATH = PATH_RESOURCE+'download/Vlibras_Up.exe'
##JSON_API_NAME = PATH_RESOURCE+'download/versionApi.json'
##TEXT_CHECK_INSTALL = 'C:/vlibras-libs/installSucess.txt'
PATH_PLAYER = PATH_RESOURCE+'Player/VlibrasPlayer.exe'



def updateFileDictVersion(version):
    try:    
        txt_local_version = open(TXT_DICT_VERSION, 'w')
        txt_local_version.write(version)
        txt_local_version.close()
        return True
    except:
        return False

def checkDictVersion():
    if os.path.isfile(TXT_DICT_VERSION):
        arquivoDictVersion = open(TXT_DICT_VERSION,'r')
        versionDict = arquivoDictVersion.readline()
        arquivoDictVersion.close()
        if not os.path.exists(DICTIONARY_EXTRACT_PATH):
            os.mkdir(DICTIONARY_EXTRACT_PATH)
        return [versionDict,len(os.listdir(DICTIONARY_EXTRACT_PATH))]
    else:
        return []
    
def checkDictSucess(quant):
    return int(quant) < len(os.listdir(DICTIONARY_EXTRACT_PATH))

def updateFilePlayerVersion(version):
    try:    
        txt_local_version = open(TXT_PLAYER_VERSION, 'w')
        txt_local_version.write(version)
        txt_local_version.close()
        return True
    except:
        return False
    

def checkPlayerVersion():
    if os.path.isfile(TXT_PLAYER_VERSION):
        arquivoDictPlayer = open(TXT_PLAYER_VERSION,'r')
        version = arquivoDictPlayer.readline()
        arquivoDictPlayer.close()
        return version
    else:
        return '000'


def updateSuccess():
    txt_local_version = open(TEXT_CHECK_INSTALL, 'w')
    txt_local_version.write('sucess')
    txt_local_version.close()
    shutil.move('C:/vlibras-libs/update/bin/vlibrasPlayerUp.exe', "C:/vlibras-libs/update/")
    

def downladFileApi(_url):
    try:
        
        urllib.urlretrieve (_url, JSON_API_VERSION)
        
        return True
    except Exception, e:
        print e
        return False

def getUrlDict():

    try:
        my_dict_version = '000000'
        dictVersionList = checkDictVersion();
        if(len(dictVersionList) == 2):
            my_dict_version = dictVersionList[0].replace(".", "").replace("_", "")
            if(not downladFileApi(URL_REQUEST_API+dictVersionList[0]+'?type=json')):
                return ['erro']
        else:
            if(not downladFileApi(URL_REQUEST_API+'0.0.0_0.0.0?type=json')):
                return ['erro']
            
        with open(JSON_API_VERSION) as data:
                json_data = json.load(data)
                
        api_dict_version_full = json_data['dictionaryVersion'].encode('utf-8')
        api_dict_version = json_data['dictionaryVersion'].encode('utf-8').replace(".", "").replace("_", "")
        api_player_version = json_data['playerVersion'].encode('utf-8').replace(".", "")
        url_download_player = json_data['playerUrl'].encode('utf-8')
        url_download_dict = json_data['dictionaryUrl'].encode('utf-8')
        
        
        if int(api_dict_version) > int(my_dict_version): 
           return [url_download_dict,api_dict_version_full]
        else:
            return []
    except:
        return ['erro']

def getUrlDictPlayer():
    my_dict_version = '000000'
    dictVersionList = checkDictVersion()
    my_player_version = checkPlayerVersion().replace(".", "").replace("_", "")
    if(len(dictVersionList) == 2):
        my_dict_version = dictVersionList[0].replace(".", "").replace("_", "")
        if(not downladFileApi(URL_REQUEST_API+dictVersionList[0]+'?type=json')):
            return ['erro']
    else:
        if(not downladFileApi(URL_REQUEST_API+'0.0.0_0.0.0?type=json')):
            return ['erro']
        
    with open(JSON_API_VERSION) as data:
            json_data = json.load(data)
            
    api_dict_version_full = json_data['dictionaryVersion'].encode('utf-8')
    api_dict_version = json_data['dictionaryVersion'].encode('utf-8').replace(".", "").replace("_", "")
    api_player_version = json_data['playerVersion'].encode('utf-8').replace(".", "")
    api_player_version_full = json_data['playerVersion'].encode('utf-8')
    url_download_player = json_data['playerUrl'].encode('utf-8')
    url_download_dict = json_data['dictionaryUrl'].encode('utf-8')
    
    if int(api_player_version) > int(my_player_version):
        return [url_download_player,api_player_version_full]
    elif int(api_dict_version) > int(my_dict_version): 
       return [url_download_dict,api_dict_version_full]
    else:
        return []




    
if __name__ == '__main__':
    print checkDictVersion()