Python http.cookiejar.MozillaCookieJar() Examples

The following are 9 code examples of http.cookiejar.MozillaCookieJar(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module http.cookiejar , or try the search function .
Example #1
Source File: cookies.py    From bdbag with Apache License 2.0 6 votes vote down vote up
def load_and_merge_cookie_jars(cookie_jar_paths):
    cookie_jar = RequestsCookieJar()
    if not cookie_jar_paths:
        return cookie_jar

    logging.debug("Attempting to load and merge the following cookie files: %s" % cookie_jar_paths)
    for f in cookie_jar_paths:
        if os.path.isfile(f):
            try:
                cookies = MozillaCookieJar(f)
                cookies.load(ignore_expires=True, ignore_discard=True)
                cookie_jar.update(cookies)
            except Exception as e:
                logging.warning("Unable to load cookie file [%s]: %s" % (f, get_typed_exception(e)))

    # Do not preserve expire values from cookies with expires=0 from the file, or requests will not use the cookie
    for cookie in iter(cookie_jar):
        if not cookie.expires:
            cookie.expires = None

    return cookie_jar 
Example #2
Source File: asf_template.py    From esa_sentinel with MIT License 6 votes vote down vote up
def get_cookie(self):
        if os.path.isfile(self.cookie_jar_path):
            self.cookie_jar = MozillaCookieJar()
            self.cookie_jar.load(self.cookie_jar_path)
            
            # make sure cookie is still valid
            if self.check_cookie():
                print(" > Re-using previous cookie jar.")
                return True
            else:
                print(" > Could not validate old cookie Jar")
        
        # We don't have a valid cookie, prompt user or creds
        print("No existing URS cookie found, please enter Earthdata username & password:")
        print("(Credentials will not be stored, saved or logged anywhere)")
        
        # Keep trying 'till user gets the right U:P
        while self.check_cookie() is False:
            self.get_new_cookie()
        
        return True
    
    # Validate cookie before we begin 
Example #3
Source File: Crawler.py    From dl_coursera with MIT License 6 votes vote down vote up
def _login(sess, cookies_file=None, cookies_base64=None):
        if cookies_file is None:
            if cookies_base64 is None:
                cookies_base64 = os.environ.get('DL_COURSERA_COOKIES_BASE64')
                assert cookies_base64

            cookies = base64.standard_b64decode(cookies_base64)

            with TmpFile() as tmpfile:
                with open(tmpfile, 'wb') as ofs:
                    ofs.write(cookies)

                cj = MozillaCookieJar()
                cj.load(tmpfile)
        else:
            cj = MozillaCookieJar()
            cj.load(cookies_file)

        sess.cookies.update(cj) 
Example #4
Source File: scholar.py    From dblp with MIT License 6 votes vote down vote up
def __init__(self):
        self.articles = []
        self.query = None
        self.cjar = MozillaCookieJar()

        # If we have a cookie file, load it:
        if ScholarConf.COOKIE_JAR_FILE and \
           os.path.exists(ScholarConf.COOKIE_JAR_FILE):
            try:
                self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
                               ignore_discard=True)
                ScholarUtils.log('info', 'loaded cookies file')
            except Exception as msg:
                ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
                self.cjar = MozillaCookieJar() # Just to be safe

        self.opener = build_opener(HTTPCookieProcessor(self.cjar))
        self.settings = None # Last settings object, if any 
Example #5
Source File: api.py    From 115wangpan with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, persistent=False,
                 cookies_filename=None, cookies_type='LWPCookieJar'):
        """
        :param bool auto_logout: whether to logout automatically when
            :class:`.API` object is destroyed

                                 .. deprecated:: 0.6.0
                                     Call :meth:`.API.logout` explicitly

        :param bool persistent: whether to use persistent session that stores
            cookies on disk
        :param str cookies_filename: path to the cookies file, use default
            path (`~/.115cookies`) if None
        :param str cookies_type: a string representing
            :class:`cookielib.FileCookieJar` subclass,
            `LWPCookieJar` (default) or `MozillaCookieJar`
        """
        self.persistent = persistent
        self.cookies_filename = cookies_filename
        self.cookies_type = cookies_type
        self.passport = None
        self.http = RequestHandler()
        self.logger = logging.getLogger(conf.LOGGING_API_LOGGER)
        # Cache attributes to decrease API hits
        self._user_id = None
        self._username = None
        self._signatures = {}
        self._upload_url = None
        self._lixian_timestamp = None
        self._root_directory = None
        self._downloads_directory = None
        self._receiver_directory = None
        self._torrents_directory = None
        self._task_count = None
        self._task_quota = None
        if self.persistent:
            self.load_cookies() 
Example #6
Source File: _api.py    From youtube-transcript-api with MIT License 5 votes vote down vote up
def _load_cookies(cls, cookies, video_id):
        cookie_jar = {}
        try:
            cookie_jar = cookiejar.MozillaCookieJar()
            cookie_jar.load(cookies)
        except CookieLoadError:
            raise CookiePathInvalid(video_id)
        if not cookie_jar:
            raise CookiesInvalid(video_id)
        return cookie_jar 
Example #7
Source File: mealpy.py    From mealpy with MIT License 4 votes vote down vote up
def initialize_mealpal():
    cookies_path = xdg.XDG_CACHE_HOME / 'mealpy' / COOKIES_FILENAME
    mealpal = MealPal()
    mealpal.session.cookies = MozillaCookieJar()

    if cookies_path.exists():
        try:
            mealpal.session.cookies.load(cookies_path, ignore_expires=True, ignore_discard=True)
        except UnicodeDecodeError:
            pass
        else:
            # hacky way of validating cookies
            sleep_duration = 1
            for _ in range(5):
                try:
                    MealPal.get_schedules('San Francisco')
                except requests.HTTPError:
                    # Possible fluke, retry validation
                    print(f'Login using cookies failed, retrying after {sleep_duration} second(s).')
                    time.sleep(sleep_duration)
                    sleep_duration *= 2
                else:
                    print('Login using cookies successful!')
                    return mealpal

        print('Existing cookies are invalid, please re-enter your login credentials.')

    while True:
        email, password = get_mealpal_credentials()

        try:
            mealpal.login(email, password)
        except requests.HTTPError:
            print('Invalid login credentials, please try again!')
        else:
            break

    # save latest cookies
    print(f'Login successful! Saving cookies as {cookies_path}.')
    mealpal.session.cookies.save(cookies_path, ignore_discard=True, ignore_expires=True)

    return mealpal 
Example #8
Source File: __init__.py    From clist with Apache License 2.0 4 votes vote down vote up
def __init__(self,
                 proxy=bool(strtobool(environ.get('REQUESTER_PROXY', '0'))),
                 cookie_filename=None,
                 caching=None,
                 user_agent=None,
                 headers=None,
                 file_name_with_proxies=path.join(path.dirname(__file__), 'proxies.txt')):
        self.opened = None
        if cookie_filename:
            self.cookie_filename = cookie_filename
        if caching is not None:
            self.caching = caching
        if headers:
            self.headers = headers
        else:
            self.headers = [
                ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
                ('Accept-Encoding', 'gzip, deflate'),
                ('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3'),
                ('Connection', 'keep-alive'),
                (
                    'User-Agent',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2'
                    if user_agent is None else user_agent
                )
            ]
        if self.cookie_filename:
            makedirs(path.dirname(self.cookie_filename), exist_ok=True)
            self.cookiejar = MozillaCookieJar(self.cookie_filename)
            if path.exists(self.cookie_filename):
                self.cookiejar.load()
        else:
            self.cookiejar = MozillaCookieJar()

        self.http_cookie_processor = urllib.request.HTTPCookieProcessor(self.cookiejar)
        self.opener = urllib.request.build_opener(self.http_cookie_processor)
        self.proxer = None
        if proxy:
            if proxy is True:
                if not file_name_with_proxies or not path.exists(file_name_with_proxies):
                    raise FileWithProxiesNotFound("ERROR: not found '%s' file" % file_name_with_proxies)
                self.proxer = proxer(file_name_with_proxies)
                proxy = self.proxer.get()
                self.print("[proxy]", proxy)
                time_response = self.proxer.time_response()
                if time_response:
                    self.print("[average time]", time_response)

            self.opener.add_handler(urllib.request.ProxyHandler({
                'http': proxy,
                'https': proxy,
            }))

        self._init_opener_headers = self.headers 
Example #9
Source File: asf_template.py    From esa_sentinel with MIT License 4 votes vote down vote up
def get_new_cookie(self):
        # Start by prompting user to input their credentials
        
        # Another Python2/3 workaround
        try:
            new_username = raw_input("Username: ")
        except NameError:
            new_username = input("Username: ")
        new_password = getpass.getpass(prompt="Password (will not be displayed): ")
        
        # Build URS4 Cookie request
        auth_cookie_url = self.asf_urs4['url'] + '?client_id=' + self.asf_urs4['client'] + '&redirect_uri=' + \
                          self.asf_urs4['redir'] + '&response_type=code&state='
        
        try:
            # python2
            user_pass = base64.b64encode(bytes(new_username + ":" + new_password))
        except TypeError:
            # python3
            user_pass = base64.b64encode(bytes(new_username + ":" + new_password, "utf-8"))
            user_pass = user_pass.decode("utf-8")
        
        # Authenticate against URS, grab all the cookies
        self.cookie_jar = MozillaCookieJar()
        opener = build_opener(HTTPCookieProcessor(self.cookie_jar), HTTPHandler(), HTTPSHandler(**self.context))
        request = Request(auth_cookie_url, headers={"Authorization": "Basic {0}".format(user_pass)})
        
        # Watch out cookie rejection!
        try:
            response = opener.open(request)
        except HTTPError as e:
            if e.code == 401:
                print(" > Username and Password combo was not successful. Please try again.")
                return False
            else:
                # If an error happens here, the user most likely has not confirmed EULA.
                print("\nIMPORTANT: There was an error obtaining a download cookie!")
                print("Your user appears to lack permission to download data from the ASF Datapool.")
                print(
                    "\n\nNew users: you must first log into Vertex and accept the EULA. In addition, your Study Area must be set at Earthdata https://urs.earthdata.nasa.gov")
                exit(-1)
        except URLError as e:
            print("\nIMPORTANT: There was a problem communicating with URS, unable to obtain cookie. ")
            print("Try cookie generation later.")
            exit(-1)
        
        # Did we get a cookie?
        if self.check_cookie_is_logged_in(self.cookie_jar):
            # COOKIE SUCCESS!
            self.cookie_jar.save(self.cookie_jar_path)
            return True
        
        # if we aren't successful generating the cookie, nothing will work. Stop here!
        print("WARNING: Could not generate new cookie! Cannot proceed. Please try Username and Password again.")
        print("Response was {0}.".format(response.getcode()))
        print(
            "\n\nNew users: you must first log into Vertex and accept the EULA. In addition, your Study Area must be set at Earthdata https://urs.earthdata.nasa.gov")
        exit(-1)
    
    # make sure we're logged into URS