n8henrie / pycookiecheat

Borrow cookies from your browser's authenticated session for use in Python scripts.
https://n8henrie.com/2013/11/use-chromes-cookies-for-easier-downloading-with-python-requests/
MIT License
744 stars 111 forks source link

Browser enums #62

Closed n8henrie closed 10 months ago

n8henrie commented 10 months ago

Enums seem like a more correct way to provide discrete browser variants, and since they're easily available in Python for many versions now, I wanted to try passing an enum between functions internally instead of a string. This should provide much better type checking during development. As an additional advantage, any time it is necessary to accept a string from a user, this should be able to consolidate the parsing logic to a single location.

As StrEnum wasn't available until relatively late in the game, I used the "inherit from str, Enum" trick to allow easy comparisons:

>>> browser = BrowserType.CHROME
>>> browser == "chrome"
True
>>> browser == "chromE"
False

... though I don't think I'm leveraging this anywhere so a straight Enum with auto() variants would likely have been fine.

Added a deprecation warning for users passing in strings; it should be minimal friction to adapt code from browser="chrome" to browser=BrowserType("chrome") or browser=BrowserType.CHROME.

This is partly in preparation for adding a CLI tool, which I hope to embark on soon (https://github.com/n8henrie/pycookiecheat/issues/60), and I intend to finally add some proper logging as well (https://github.com/n8henrie/pycookiecheat/issues/46)

Would appreciate any thoughts if you have time @grandchild!

n8henrie commented 10 months ago

The rest looks good, but I only did a visual check. I didn't test the actual code (yet).

Thanks for looking

A few seemingly unrelated commits with nixos things in the beginning, which I ignored.

Yeah, old habits die hard.