[Cs:s] Server Command to download new version of plugin

Please post any questions about developing your plugin here. Please use the search function before posting!
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

[Cs:s] Server Command to download new version of plugin

Postby cssbestrpg » Tue Mar 09, 2021 7:38 am

Hi. i have issue make server command to download newest version files from github. It gives this errors:

Code: Select all

zombie_download

[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\plugins\zr\modules\version\__init__.py", line 27, in zombie_downloadd
    with urlopen(download_url, GAME_PATH, timeout=timeout) as url:
  File "..\addons\source-python\Python3\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "..\addons\source-python\Python3\urllib\request.py", line 524, in open
    req = meth(req)
  File "..\addons\source-python\Python3\urllib\request.py", line 1248, in do_request_
    raise TypeError(msg)

TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.


The code:

Syntax: Select all

from paths import GAME_PATH
from urllib.request import urlopen
from core import echo_console
from commands.server import ServerCommand

Ver = '1.0.2'

download_url = ('https://github.com/srpg/Zombie-Riot/archive/main.zip')
new_version = ('http://cssbestrpg.online/version.txt')

def version_checker(timeout=3):
with urlopen(new_version, timeout=timeout) as url:
return url.read().decode('utf-8')

def check_version():
if version_checker() > Ver:
echo_console('[Zombie Riot] There is %s version available to download!' % (version_checker()))
echo_console('[Zombie Riot] You can download new version from: https://github.com/srpg/Zombie-Riot')
echo_console('[Zombie Riot] You are currently running %s version' % (Ver))
else:
echo_console('[Zombie Riot] There is no new version available!')

def download(timeout=3):
with urlopen(download_url, GAME_PATH, timeout=timeout) as url:
data = url.read()
with GAME_PATH.open('wb') as f:
f.write(data)

@ServerCommand('zombie_download')
def zombie_downloadd(command):
download()
echo_console('[Zombie Riot] You have downloaded newest version to cstrike folder')
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Cs:s] Server Command to download new version of plugin

Postby L'In20Cible » Tue Mar 09, 2021 8:10 am

There is quite a few issues in that code. First, the exception you referenced is caused by the following line:

cssbestrpg wrote:

Syntax: Select all

with urlopen(download_url, GAME_PATH, timeout=timeout) as url:


The second parameter, is the additional data that should be included with your request, not a local path.

Then, there is the following:

cssbestrpg wrote:

Syntax: Select all

with GAME_PATH.open('wb') as f:


You cannot do that. GAME_PATH is a folder, not a file. What you want to do, is extract the zip in that directory, which you can do using the ZipFile package. Have a look at update.py which can gives you some pointers.

There is also the following:
cssbestrpg wrote:

Syntax: Select all

if version_checker() > Ver:
echo_console('[Zombie Riot] There is %s version available to download!' % (version_checker()))


That is terrible practice. You don't want to make 2 requests just for logging. Do it once, and assign it locally.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Server Command to download new version of plugin

Postby cssbestrpg » Tue Mar 09, 2021 9:19 am

I got the download part now work. But i have other issue, when downloads from github the files, and i want to make extract them, but when it extract it always first extract Zombie-Riot-main folder. So it won't properly extract files. I want to make it extract inside of that folder.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Cs:s] Server Command to download new version of plugin

Postby L'In20Cible » Tue Mar 09, 2021 10:48 am

cssbestrpg wrote:I got the download part now work. But i have other issue, when downloads from github the files, and i want to make extract them, but when it extract it always first extract Zombie-Riot-main folder. So it won't properly extract files. I want to make it extract inside of that folder.

That's how the archive downloaded from GitHub is structured. Either move the files afterwards, or extract them one by one manually.
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Server Command to download new version of plugin

Postby cssbestrpg » Tue Mar 09, 2021 11:06 am

How i can move the files inside of the folder? In python code
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [Cs:s] Server Command to download new version of plugin

Postby L'In20Cible » Tue Mar 09, 2021 11:56 am

cssbestrpg wrote:How i can move the files inside of the folder? In python code

There's multiple ways you can move file, though I would probably personally prefer something like that overall:

Syntax: Select all

from io import BytesIO
from paths import GAME_PATH
from urllib.request import urlopen
from zipfile import ZipFile

# Request the archive
with urlopen('https://github.com/srpg/Zombie-Riot/archive/main.zip') as response:

# Open the archive
with ZipFile(BytesIO(response.read())) as zipfile:

# Loop through all the members of the archive
for info in zipfile.infolist():

# Get the name of the current file
filename = info.filename

# Remove the first subdirectory
path = GAME_PATH / filename.split('/', 1)[1]

# Create directories in case they don't exist yet
if info.is_dir():
path.makedirs_p()

# Otherwise, write the files
else:
with open(path, 'wb') as f:
f.write(zipfile.read(filename))
cssbestrpg
Senior Member
Posts: 287
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [Cs:s] Server Command to download new version of plugin

Postby cssbestrpg » Tue Mar 09, 2021 12:26 pm

Thanks for help now it works perfectly

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 40 guests