[SOLVED] Issue downloading package via Python script

Questions about WAPT Packaging / Requests and help regarding Wapt packages.
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Locked
troublestarter
Messages: 15
Registration: September 5, 2018 - 10:30
Location: Villeneuve d'Ascq

October 4, 2018 - 2:31 PM

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()
User avatar
agauvrit
WAPT Expert
Messages: 238
Registration: Nov 17, 2016 - 10:25
Location: Nantes
Contact :

October 4, 2018 - 3:27 PM

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
troublestarter
Messages: 15
Registration: September 5, 2018 - 10:30
Location: Villeneuve d'Ascq

October 4, 2018 - 3:45 PM

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?
User avatar
agauvrit
WAPT Expert
Messages: 238
Registration: Nov 17, 2016 - 10:25
Location: Nantes
Contact :

October 4, 2018 - 5:51 PM

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
troublestarter
Messages: 15
Registration: September 5, 2018 - 10:30
Location: Villeneuve d'Ascq

October 4, 2018 - 7:02 PM

Thank you. I'll have to try it!
Floflobel
Messages: 135
Registration: Oct 15, 2015 - 5:32 p.m.

October 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
User avatar
sfonteneau
WAPT Expert
Messages: 2318
Registered: July 10, 2014 - 11:52 PM
Contact :

October 4, 2018 - 7:32 PM

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!
User avatar
agauvrit
WAPT Expert
Messages: 238
Registration: Nov 17, 2016 - 10:25
Location: Nantes
Contact :

October 5, 2018 - 9:56 AM

Awesome !
troublestarter
Messages: 15
Registration: September 5, 2018 - 10:30
Location: Villeneuve d'Ascq

October 5, 2018 - 5:50 PM

I confirm ...
Locked