Page 1 of 1

[SOLVED] Issue downloading package via Python script

Published: October 4, 2018 - 2:31 PM
by troublestarter
Telegram, here is the download link: https://telegram.org/dl/desktop/win
The script cannot download the executable file.

Here is the relevant code:

Code: Select all

def update_package():
    """ You can do a CTRL F9 in pyscripter to update the package """
    import re,requests,urlparse,glob

    url = requests.head('https://telegram.org/dl/desktop/win',proxies={}).headers['Location']
    filename = urlparse.unquote(url.rsplit('/',1)[1])

    if not isfile(filename):
        print('Downloading %s from %s'%(filename,url))
        wget(url,filename)

    exes = glob.glob('*.exe')
    for fn in exes:
        if fn != filename:
            remove_file(fn)

    # updates control version from filename, increment package version.
    control = PackageEntry().load_control_from_wapt ('.')
    control.version = '%s-0'%(re.findall('tsetup(.*)\.exe',filename)[0])
    control.save_control_to_wapt('.')

if __name__ == '__main__':
    update_package()

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 3:27 PM
by agauvrit
Good morning,

In my opinion, it will be simpler to retrieve the installer from the project's GitHub repository, which has this format:

Code: Select all

https://github.com/telegramdesktop/tdesktop/releases/download/v1.4.0/tsetup.1.4.0.exe 
Retrieving the version number with BeautifulSoup shouldn't be too complicated (and it's a good exercise for learning)

Sincerely,

Alexander

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 3:45 PM
by troublestarter
The previous link seemed better to me because it automatically retrieves the latest version...
This is for the "update" part of the package code.

If I write code with a fixed link, what advice would you give for handling version updates? Should I update the code with the new link?

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 5:51 PM
by agauvrit
Yes, the link can easily be generated by retrieving the latest version number available on this same page.
For reference, the Rocket.Chat package does essentially the same thing: https://wapt.tranquil.it/package_detail ... 2_all.wapt

Code: Select all

def update_package():
    proxy={'http':'http://srvproxy:3128','https':'http://srvproxy:3128'}

    import requests,BeautifulSoup
    import re
    verify=True
    pe = PackageEntry()
    pe.load_control_from_wapt(os.getcwd())
    current_version = pe['version'].split('-',1)[0]

    software_name = "Rocket.Chat"

    version_software_url = "https://github.com/RocketChat/Rocket.Chat.Electron/releases/latest"

    pattern=re.compile(r'version (.*)')
    page = wgets(version_software_url,user_agent='Mozilla/5.0 (Windows NT 6.1; Win64; x64)',proxies=proxy)
    bs = BeautifulSoup.BeautifulSoup(page)
    software_version = str(bs.find('title')).split(' ')[1]

    print "Current %s WAPT package version is : %s" % (software_name,current_version)
    print "Latest %s version available is : %s" % (software_name,software_version)

    if Version(current_version) < Version(software_version):
            print "%s package is not up-to-date, updating" % software_name
            print "Cleanup current exe files"

            for exe in glob.glob('*.exe'):
                remove_file(exe)

            print("Downloading latest version")
            wget('https://github.com/RocketChat/Rocket.Chat.Electron/releases/download/' + software_version + '/rocketchat-setup-' + software_version + '.exe' , 'rocketchat-setup' + software_version + '.exe',proxies=proxy)

            pe.version = software_version + '-0'
            pe.save_control_to_wapt(os.getcwd())
    else:
        print("No update needed, package already up to date")
Alexander

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 7:02 PM
by troublestarter
Thank you. I'll have to try it!

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 7:09 PM
by Floflobel
I'd like to add my two cents; personally, we use the GitHub API or parse the link to the latest release. I find it much easier: :)

https://api.github.com/repos/telegramde ... ses/latest

Re: Problem downloading packages via Python script

Published: October 4, 2018 - 7:32 PM
by sfonteneau
Floflobel wrote: Oct 4, 2018 - 7:09 PM I'd like to add my two cents; personally, we use the GitHub API or parse the link to the latest release. I find it much easier. :)

https://api.github.com/repos/telegramde ... ses/latest
A very good contribution, I wasn't aware of the existence of the API!

Re: Problem downloading packages via Python script

Published: October 5, 2018 - 9:56 AM
by agauvrit
Awesome !

Re: Problem downloading packages via Python script

Published: October 5, 2018 - 5:50 PM
by troublestarter
I confirm ...