bitbybyte / fantiadl

Download posts and media from Fantia
MIT License
303 stars 51 forks source link

[BUG] Unable to download due to invalid session #77

Closed ramenchef closed 2 years ago

ramenchef commented 2 years ago

Getting "Error: Invalid session. Please verify your session cookie" every time I attempt to run fantiadl. Things were working the last time I ran this back in december.

command run: .\fantiadl_v1.8.exe -i -s -d 2022-01 -c cookies.txt https://fantia.jp/fanclubs/ tested: .\fantiadl_v1.8.exe -i -s -d 2022-01 -c <_SESSION_ID VALUE> https://fantia.jp/fanclubs/ I have also logged out of fantia and logged back in to generate a new cookie/_session_id to test with.

LighterSideOfDark commented 2 years ago

I'm having the same issue, I even tried generating a new session ID with a fresh browser that's never logged into Fantia before.

Neverwintern commented 2 years ago

A quick fix is adding User-Agent headers to all session.get calls.

E.g.:

headers = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4826.0 Safari/537.36 Edg/99.0.1141.0'
        # check_user = self.session.get(ME_API)
        check_user = self.session.get(ME_API, headers=headers)

And I also edit the condition check of the login function.

        if not check_user.ok:
            sys.exit("Error: Invalid session. Please verify your session cookie")
MrT1122 commented 2 years ago

Ended up with a similar fix, just that a Stackoverflow post suggested to update the session header only once. So in models.py after self.session = requests.session() I added the lines

useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
self.session.headers.update({'User-Agent': useragent })  

which made it work again.

Of course it then would be more elegant to not hard code the user-agent but instead pass it as an argument into the FantiaDownloader class like def __init__(self, session_arg, useragent,..", then add another command line argument in fantiadl.py with whatever parameter name you prefer, like cmdl_parser.add_argument("-u", "--useragent", dest="useragent", metavar="USERAGENT", help="Useragent") and then pass that into the downloader with downloader = models.FantiaDownloader(session_arg=session_arg, useragent=cmdl_opts.useragent,..

This seems to work fine for me. Not sure if it's good Python though or could potentially break the program. Don't really know the language and just wanted a quick fix that allows me to pass the user-agent as a command line parameter like the session_id.

bitbybyte commented 2 years ago

The change actually seems to only just require that User-Agent be set. I'm setting it to fantiadl/{version} to be a good citizen but hopefully we don't need to use user agent spoofing and other trickery yet.