示例#1
0
 def login(self, username, password):
     """Try to form a new connection to Instagram using the given username
     and password."""
     # try to load cookies from cookie jar
     client_kwargs = dict()
     if os.path.isfile(COOKIE_JAR):
         cookies = CookieJar()
         with open(COOKIE_JAR, 'rb') as cookiejar:
             cookie_string = cookiejar.read()
         # make sure username in cookies is as expected; otherwise,
         # don't use cookies
         try:
             cookies._cookies = pickle.loads(cookie_string)
             cookie_user = [c.value for c in cookies if c.name == 'ds_user']
             if cookie_user == username:
                 client_kwargs['cookie'] = cookie_string
         except:
             logging.warn("Could not parse cookie file.")
     self.client = Client(username, password, **client_kwargs)
     self.username = username
示例#2
0
    def loads(self, d: dict):
        """
        Read a session from a given dict. A warning will be given if username or password field is missing.

        :param d: a dict contains a session.
        """
        renew_required = True

        if "cookies" in d.keys() and d["cookies"]:
            if isinstance(d["cookies"], httpx.models.Cookies):
                cj = d["cookies"]  # type: ignore
            elif isinstance(d["cookies"], dict):
                cj = CookieJar()  # type: ignore
                # noinspection PyTypeHints
                cj._cookies = d["cookies"]  # type: ignore
            else:
                raise TypeError
            try:
                self.cookies = cj
                renew_required = False
            except SessionException:
                pass
        else:
            self._cookies = {}

        if "username" not in d.keys() or "password" not in d.keys(
        ) or not d["username"] or not d["password"]:
            warnings.warn("Missing username or password field", LoadWarning)
            self._username = ""
            self._password = ""
            renew_required = False
        else:
            self._username = d["username"]
            self._password = d["password"]

        if renew_required:
            self.login(self._username, self._password)