[CS:S] Map Download

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

[CS:S] Map Download

Postby cssbestrpg » Sat Sep 09, 2023 9:05 pm

Hi, i have issues with my code, i tried make a code that downloads a map and extract it to maps folder when type the map url adress, the issue i can't get the extract part to work.

Here is the code i tried:

Syntax: Select all

import os, urllib, shutil
from paths import GAME_PATH
from urllib import request
from commands.say import SayCommand

@SayCommand('download')
def download_command(command, index, team_only):
args = command.arg_string
if args:
download_map(args)
return False

def download_map(url):
try:
urllib.request.urlretrieve(url, "maps_file.rar")
shutil.move(os.getcwd() + '/maps_file.rar', GAME_PATH.joinpath('maps_file.rar'))

with open(GAME_PATH + '/maps_file.rar', 'rb') as open_file:
for i in open_file.readlines():
if i.lower().endswith('.bsp') or i.lower().endswith('.nav'):
shutil.move(GAME_PATH.joinpath(i), GAME_PATH + '/maps')
except urllib.error.URLError:
print('An error appeared')


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File "..\addons\source-python\packages\source-python\commands\auth.py", line 44, in __call__
    return self.callback(*args)
  File "..\addons\source-python\plugins\test\test.py", line 10, in download_command
    download_map(args)
  File "..\addons\source-python\plugins\test\test.py", line 20, in download_map
    if i.lower().endswith('.bsp') or i.lower().endswith('.nav'):

TypeError: endswith first arg must be bytes or a tuple of bytes, not str
User avatar
Articha
Member
Posts: 32
Joined: Tue Sep 21, 2021 12:13 pm
Location: Russia

Re: [CS:S] Map Download

Postby Articha » Sun Sep 10, 2023 6:39 am

You opening Rar archive with bytes mode and reading it while first to do is unarchive this.

You probably want to unarchive to temporary folder first, then search for maps file (via extension check) and then move them. For unarchive you can use libraries like rarfile or patool.
Darwyn
Junior Member
Posts: 1
Joined: Mon Sep 11, 2023 4:08 am

Re: [CS:S] Map Download

Postby Darwyn » Mon Sep 11, 2023 6:03 am

Hello,

I also needed some advice on how to deal with Rar archives, in particular byte mode and unarchiving. Your suggestions are really helpful. Thanks for sharing this valuable information!
My website here
cssbestrpg
Senior Member
Posts: 309
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [CS:S] Map Download

Postby cssbestrpg » Mon Sep 11, 2023 6:36 pm

I tried with rarfile, but i am having now an other issue.

Here is the code:

Syntax: Select all

import os
import urllib
import shutil
from rarfile import RarFile
from zipfile import ZipFile, is_zipfile
from paths import GAME_PATH
from urllib.request import urlopen
from commands.say import SayCommand

DOWNLOADED_FILE = GAME_PATH / 'maps_file.zip'
DOWNLOADED_FILE_RAR = GAME_PATH / 'maps_file.rar'
MAPS_FOLDER = GAME_PATH / 'maps'

@SayCommand('download')
def download_command(command, index, team_only):
args = command.arg_string
if args:
download_map(args)
return False

def download_map(url):
with urlopen(url, timeout=3) as response:
data = response.read()
with DOWNLOADED_FILE.open('wb') as f:
f.write(data)
if is_zipfile(DOWNLOADED_FILE):
with ZipFile(DOWNLOADED_FILE) as zipfile:
for info in zipfile.infolist():
filename = info.filename
try:
path = GAME_PATH / filename.split('/', 1)[1]
except IndexError:
path = GAME_PATH / filename
if filename.endswith('.bsp') or filename.endswith('.nav'):
with open(path, 'wb') as f:
f.write(zipfile.read(filename))
move_files_to_maps()
os.remove(DOWNLOADED_FILE)
else:
os.rename(DOWNLOADED_FILE, DOWNLOADED_FILE_RAR)
with RarFile(DOWNLOADED_FILE_RAR) as archive:
for file_info in archive.infolist():
filename = file_info.filename
try:
path = GAME_PATH / filename.split('/', 1)[1]
except IndexError:
path = GAME_PATH / filename
if filename.endswith('.bsp') or filename.endswith('.nav'):
with open(path, 'wb') as f:
f.write(archive.read(filename))
move_files_to_maps()
os.remove(DOWNLOADED_FILE_RAR)

def move_files_to_maps():
for maps in os.listdir(GAME_PATH):
if maps.endswith('.nav') or maps.endswith('.bsp'):
shutil.move(GAME_PATH.joinpath(maps), MAPS_FOLDER.joinpath(maps))


It gives following error when it tries rar files:

Code: Select all

Traceback (most recent call last):
  File "..\addons\source-python\packages\source-python\commands\auth.py", line 44, in __call__
    return self.callback(*args)
  File "..\addons\source-python\plugins\test\test.py", line 20, in download_command
    download_map(args)
  File "..\addons\source-python\plugins\test\test.py", line 51, in download_map
    f.write(rarfile.read(filename))
  File "..\addons\source-python\Python3\rarfile.py", line 809, in read
    with self.open(name, "r", pwd) as f:
  File "..\addons\source-python\Python3\rarfile.py", line 794, in open
    return self._file_parser.open(inf, pwd)
  File "..\addons\source-python\Python3\rarfile.py", line 1251, in open
    return self._open_hack(inf, pwd)
  File "..\addons\source-python\Python3\rarfile.py", line 1638, in _open_hack
    return self._open_hack_core(inf, pwd, prefix, b"")
  File "..\addons\source-python\Python3\rarfile.py", line 1289, in _open_hack_core
    return self._open_unrar(tmpname, inf, pwd, tmpname)
  File "..\addons\source-python\Python3\rarfile.py", line 1300, in _open_unrar
    setup = tool_setup()
  File "..\addons\source-python\Python3\rarfile.py", line 3380, in tool_setup
    raise RarCannotExec("Cannot find working tool")

rarfile.RarCannotExec: Cannot find working tool
User avatar
Articha
Member
Posts: 32
Joined: Tue Sep 21, 2021 12:13 pm
Location: Russia

Re: [CS:S] Map Download

Postby Articha » Tue Sep 12, 2023 6:21 am

Solution. I wish somebody except me help you, because I don't have time to access PC, especially Source.Python
cssbestrpg
Senior Member
Posts: 309
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: [CS:S] Map Download

Postby cssbestrpg » Tue Sep 12, 2023 12:40 pm

Well i got it working after installing other python package to deal rar files

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 21 guests