VlibrasCheckVersion.py 2.23 KB
# -*- coding: cp1252 -*-
import json, zipfile, subprocess,urllib
from os import popen
import sys

PATH_RESOURCE = 'c:/vlibras-libs/update/'
TXT_LOCAL_VERSION = PATH_RESOURCE+'version/version.txt'
JSON_LOCAL_NAME = PATH_RESOURCE+'version/version.json'
JSON_API_NAME = PATH_RESOURCE+'download/versionApi.json'
URL_REQUEST_API = 'http://150.165.205.94:8000/versionApi.json'


    
    
def check():
	
    try:
       
        with open(JSON_LOCAL_NAME) as data_file:
                json_data = json.load(data_file)

        my_dict_version_full = json_data['dictionaryVersion'].encode('utf-8')
        my_player_version = json_data['playerVersion'].encode('utf-8').replace(".", "")
        my_dict_version = my_dict_version_full.replace(".", "").replace("_", "")  

        if(not downladFileApi(URL_REQUEST_API)):
            return 'erro'
        
        with open(JSON_API_NAME) as data:
                json_data = json.load(data)
        
        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_player_version) > int(my_player_version): 
           return url_download_player
                
        elif int(api_dict_version) > int(my_dict_version):
                
            return url_download_dict
        
        else: 
            return 'atualizado'

    except Exception, e:
        print e
        return 'erro'	
         

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



if __name__ == '__main__':
    txt_local_version = open(TXT_LOCAL_VERSION, 'w')
    result = check()
    if result.endswith('.exe'):
        txt_local_version.write('1')
    elif result.endswith('.zip'):
        txt_local_version.write('2')
    elif (result == 'atualizado'):
        txt_local_version.write('0')
    else:
        txt_local_version.write('-1')

    txt_local_version.close()
    print result