mickael9 / fac

Command-line mod manager for Factorio (install, update...)
MIT License
51 stars 17 forks source link

Should check status code from requests.get() first in download_mod() #7

Closed Arnavion closed 8 years ago

Arnavion commented 8 years ago

I ran fac install hacked-splitters and it failed with:

Traceback (most recent call last):
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\gtk-build\python-3.5\Win32\Scripts\fac.exe\__main__.py", line 9, in <module>
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\site-packages\fac\main.py", line 84, in main
    args.run(args)
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\site-packages\fac\commands\install.py", line 158, in run
    self.manager.install_mod(release, unpack=args.unpack)
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\site-packages\fac\mods.py", line 476, in install_mod
    self.download_mod(release, tmp_file)
  File "c:\programdata\chocolatey\lib\python3-x86_32\tools\lib\site-packages\fac\mods.py", line 515, in download_mod
    len(data), release.file_size
Exception: Downloaded file has incorrect size (0), expected 40119.

I added a print('%s' % req) in download_mod and get <Response [403]>. So basically the download request returned a 0-byte 403 response. You should probably also verify that the response status code is 200 before data = req.content, etc

(I'm still investigating why I'm getting a 403 there at all... The 403 is because this is a new account and I haven't connected it to my Steam account yet. Logging in to mods.factorio.com in a web browser gives me This account doesn't have a sufficient membership. If you bought the game on Steam, you need to connect your Steam account to your factorio.com account.)

mickael9 commented 8 years ago

You can use -v for requests debugging :)

I'm not having this issue, I suspect the 403 error is because your token or username is invalid (from player-data.json) Are you able to download mods from the game?

Anyway, I'll add some error handling so that invalid error codes are raised as HTTPError

Arnavion commented 8 years ago

See edit to the OP :)

Anyway, I'll add some error handling so that invalid error codes are raised as HTTPError

Yeah, that's what I was asking for. Otherwise there's the possibility a non-200 response has the same content length as the actual mod file, and fac install won't notice.

mickael9 commented 8 years ago

There's no way it wouldn't fail later in the process anyway, since the zip file is opened and info.json is read before the zip gets saved.

Anyway, I added some proper error messages for both the "already logged in with no game ownership" and also added the require_ownership=true parameter which the game uses on login. This is what it looks like now:

$ fac install yarm
Installing: YARM 0.7.107...
Downloading: https://mods.factorio.com/api/downloads/data/mods/187/YARM_0.7.107.zip...
Authentification error when downloading mod. Please login again.
You need a Factorio account to download mods.
Please provide your username and password to authenticate yourself.
Your username and token (NOT your password) will be stored so that you only have to enter it once
This uses the exact same method used by Factorio itself

Username [mickael9-test]:  
Password (not shown):
Ownership error: Your factorio account doesn't own the game.
Please buy the game or link your Steam account if you have bought the game from Steam.