Ejemplo n.º 1
0
    def login(self):
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:
            login_params = {
                'username': self.custom_settings['username'],
                'password': self.custom_settings['password'],
                'login': '******',
                'keeplogged': '1'
            }

            try:
                self.session.get(self.urls['login'])
                response = self.session.post(self.urls['login'],
                                             data=login_params).text
            except Exception:
                sickrage.app.log.warning("Unable to connect to provider")
                return False

            if re.search('logout.php', response):
                return True
            elif re.search('Your username or password was incorrect.',
                           response):
                sickrage.app.log.warning(
                    "Invalid username or password. Check your settings")
Ejemplo n.º 2
0
    def add_cookies_from_ui(self):
        """
        Adds the cookies configured from UI to the providers requests session
        :return: A dict with the the keys result as bool and message as string
        """

        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies:
            if self.cookies:
                cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
                if not cookie_validator.match(self.cookies):
                    ui.notifications.message('Failed to validate cookie for provider {provider}'.format(provider=self.name),
                                             'Cookie is not correctly formatted: {0}'.format(self.cookies))
                    return {'result': False,
                            'message': 'Cookie is not correctly formatted: {0}'.format(self.cookies)}

                # cookie_validator got at least one cookie key/value pair, let's return success
                add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
                return {'result': True,
                        'message': ''}

            else:  # Else is not needed, but placed it here for readability
                ui.notifications.message('Failed to validate cookie for provider {provider}'.format(provider=self.name),
                                         'No Cookies added from ui for provider: {0}'.format(self.name))
                return {'result': False,
                        'message': 'No Cookies added from ui for provider: {0}'.format(self.name)}

        return {'result': False,
                'message': 'Adding cookies is not supported for provider: {0}'.format(self.name)}
Ejemplo n.º 3
0
def _identify_captcha():
    """
    验证码识别
    :return: True/False
    """
    for _ in range(setting.repeat_times):
        _session.headers.update({'Referer': 'https://weixin.sogou.com'})
        resp = _session.get(
            f'http://weixin.sogou.com/antispider/util/seccode.php?tc={int(time())}'
        )
        tf = TemporaryFile()
        tf.write(resp.content)
        code = setting.sougo_captcha_callback(Image.open(tf))
        if not code: continue
        resp = _session.post('https://weixin.sogou.com/antispider/thank.php',
                             data={
                                 'c': code,
                                 'r': 'https://weixin.sogou.com/',
                                 'v': 5,
                             })
        resp.encoding = 'utf-8'
        msg = resp.json()
        if msg['code'] == 0:
            add_dict_to_cookiejar(_session.cookies, {'SNUID': msg['id']})
            return True
        print('验证码错误!')
    return False
Ejemplo n.º 4
0
    def login(self):
        """Login method used for logging in before doing a search and torrent downloads."""
        cookie_dict = dict_from_cookiejar(self.session.cookies)
        if cookie_dict.get('session'):
            return True

        if self.cookies:
            add_dict_to_cookiejar(
                self.session.cookies,
                dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

        cookie_dict = dict_from_cookiejar(self.session.cookies)
        if cookie_dict.get('session'):
            return True

        login_params = {
            'submit': 'Login',
            'username': self.username,
            'password': self.password,
            'keeplogged': 1,
        }

        response = self.get_url(self.urls['login'],
                                post_data=login_params,
                                returns='text')
        if not response:
            logger.log("Unable to connect to provider", logger.WARNING)
            return False

        if re.search('<title>Login :: BJ-Share</title>', response):
            logger.log("Invalid username or password. Check your settings",
                       logger.WARNING)
            return False

        return True
Ejemplo n.º 5
0
def identify_captcha():
    while True:
        sogou_session.headers.update(
            {'Referer': 'https://weixin.sogou.com/antispider/'})
        resp = sogou_session.get(
            f'http://weixin.sogou.com/antispider/util/seccode.php?tc={int(time())}'
        )
        code = sogou_captcha_callback(resp.content)
        if not code or not isinstance(code, str):
            logging.warning("Sogou 验证码错误!")
            continue
        code = code.strip().lower()
        if code == 'exit':
            raise KeyboardInterrupt
        resp = sogou_session.post(
            'https://weixin.sogou.com/antispider/thank.php',
            data={
                'c': code,
                'r': 'https://weixin.sogou.com/',
                'v': 5,
            })
        resp.encoding = 'utf-8'
        msg = resp.json()
        if msg['code'] == 0:
            add_dict_to_cookiejar(sogou_session.cookies, {'SNUID': msg['id']})
            break
        logging.warning("Sogou 验证码错误!")
Ejemplo n.º 6
0
    def _getRSSData(self):
        logger.log(u"Cache update URL: %s" % self.provider.url, logger.DEBUG)

        if self.provider.cookies:
            add_dict_to_cookiejar(self.provider.session.cookies, dict(x.rsplit('=', 1) for x in self.provider.cookies.split(';')))

        return self.getRSSFeed(self.provider.url)
Ejemplo n.º 7
0
Archivo: client.py Proyecto: 56-1/idler
    def courseUtils(self, courseId, clazzId):
        """拉取用于刷课的cookies"""
        self.courseId = courseId
        self.clazzId = clazzId

        url = "https://passport2.chaoxing.com/api/cookie"
        self.headers["Host"] = "passport2.chaoxing.com"

        response = requestUtils(url=url,
                                headers=self.headers,
                                cookies=self.cookiesForCourse)
        t = dict_from_cookiejar(response.cookies)  # for get other cookies
        add_dict_to_cookiejar(self.cookiesForClass, t)

        url = "https://mooc1-api.chaoxing.com/gas/clazzperson"
        params = {
            "courseid": courseId,
            "clazzid": clazzId,
            "userid": self.uid,
            "view": "json",
            "fields": "clazzid,popupagreement,personid,clazzname"
        }

        self.headers["Host"] = "mooc1-api.chaoxing.com"
        response = requestUtils(url=url,
                                params=params,
                                headers=self.headers,
                                cookies=self.cookiesForClass)
        try:
            content = response.json()
        except JSONDecodeError as e:
            error(str(e))
            exit(1)

        self.personId = content["data"][0]["personid"]
Ejemplo n.º 8
0
    def add_cookies_from_ui(self):
        """
        Add the cookies configured from UI to the providers requests session.

        :return: A dict with the the keys result as bool and message as string
        """
        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies:
            if self.cookies:
                cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
                if not cookie_validator.match(self.cookies):
                    ui.notifications.message(
                        'Failed to validate cookie for provider {provider}'.format(provider=self.name),
                        'Cookie is not correctly formatted: {0}'.format(self.cookies))
                    return {'result': False,
                            'message': 'Cookie is not correctly formatted: {0}'.format(self.cookies)}

                # cookie_validator got at least one cookie key/value pair, let's return success
                add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
                return {'result': True,
                        'message': ''}

            else:  # Cookies not set. Don't need to check cookies
                return {'result': True,
                        'message': 'No Cookies added from ui for provider: {0}'.format(self.name)}

        return {'result': False,
                'message': 'Adding cookies is not supported for provider: {0}'.format(self.name)}
Ejemplo n.º 9
0
    def _get_citations_from_detail_url(self, detail_url, eid):
        add_dict_to_cookiejar(self.session.cookies, {'javaScript': 'true'})
        with self.throttler():
            r = self.session.get(detail_url)

        et = html5lib.parse(r.text, treebuilder="lxml")

        def get_cited_by_link(et):
            namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
            cite_links = et.xpath(
                ".//html:a[starts-with(@href, 'http://www.scopus.com/search/submit/citedby.url') and @title='View all citing documents']",
                namespaces=namespaces)

            for link in cite_links:
                if 'title' in link.attrib:
                    logging.debug('Trying citation link: %s',
                                  link.attrib['title'])
                    return link.attrib['href']

            return None

        link = get_cited_by_link(et)
        if link == None:
            logging.warning('No SCOPUS citation link found')
            return []

        headers = {'Referer': r.url}

        with self.throttler():
            r2 = self.session.get(link, headers=headers)

        return self._download_from_results_form(
            r2, context=['_get_citations_from_detail_url', detail_url, eid])
Ejemplo n.º 10
0
 def __init__(self, ticket=None, at_token=None):
     self.session = requests.session()
     self.__at = None
     self.__boss_ticket = None
     if ticket is not None:
         add_dict_to_cookiejar(self.session.cookies, {TICKET_NAME: ticket})
     if at_token is not None:
         self.__at = at_token
Ejemplo n.º 11
0
    def _get_rss_data(self):
        sickrage.app.log.debug("Cache update URL: %s" % self.provider.urls['base_url'])

        if self.provider.cookies:
            add_dict_to_cookiejar(self.provider.session.cookies,
                                  dict(x.rsplit('=', 1) for x in self.provider.cookies.split(';')))

        return self.get_rss_feed(self.provider.urls['base_url'])
Ejemplo n.º 12
0
    def _get_rss_data(self):
        if self.provider.cookies:
            add_dict_to_cookiejar(
                self.provider.session.cookies,
                dict(
                    x.rsplit('=', 1)
                    for x in self.provider.cookies.split(';')))

        return self.get_rss_feed(self.provider.url)
Ejemplo n.º 13
0
    def _set_cookie(self, url):
        response1 = self.session.get(url)
        jsl_clearance_s = re.findall(r'cookie=(.*?);location', response1.text)[0]
        jsl_clearance_s = str(execjs.eval(jsl_clearance_s)).split('=')[1].split(';')[0]
        add_dict_to_cookiejar(self.session.cookies, {'__jsl_clearance_s': jsl_clearance_s})

        response2 = self.session.get(url)
        data = json.loads(re.findall(r';go\((.*?)\)', response2.text)[0])
        jsl_clearance_s = self._get__jsl_clearance_s(data)
        add_dict_to_cookiejar(self.session.cookies, {'__jsl_clearance_s': jsl_clearance_s})
Ejemplo n.º 14
0
    def __init__(self,
                 cookie_file='cookies.txt',
                 debug_files=False,
                 dest_directory='.'):
        self.debug_files = debug_files
        self.cookies = self.get_cookies(cookie_file)

        self.session = BaseURLSession(prefix_url="https://echo360.org.uk")
        add_dict_to_cookiejar(self.session.cookies, self.cookies)
        self.dest_directory = dest_directory
Ejemplo n.º 15
0
    def add_cookies_from_ui(self):
        """
        Add the cookies configured from UI to the providers requests session.

        :return: A dict with the the keys result as bool and message as string
        """
        # Added exception for rss torrent providers, as for them adding cookies initial should be optional.
        from medusa.providers.torrent.rss.rsstorrent import TorrentRssProvider
        if isinstance(self, TorrentRssProvider) and not self.cookies:
            return {'result': True,
                    'message': 'This is a TorrentRss provider without any cookies provided. '
                               'Cookies for this provider are considered optional.'}

        # This is the generic attribute used to manually add cookies for provider authentication
        if not self.enable_cookies:
            return {'result': False,
                    'message': 'Adding cookies is not supported for provider: {0}'.format(self.name)}

        if not self.cookies:
            return {'result': False,
                    'message': 'No Cookies added from ui for provider: {0}'.format(self.name)}

        cookie_validator = re.compile(r'^([\w%]+=[\w%]+)(;[\w%]+=[\w%]+)*$')
        if not cookie_validator.match(self.cookies):
            ui.notifications.message(
                'Failed to validate cookie for provider {provider}'.format(provider=self.name),
                'Cookie is not correctly formatted: {0}'.format(self.cookies))
            return {'result': False,
                    'message': 'Cookie is not correctly formatted: {0}'.format(self.cookies)}

        if self.required_cookies:
            if self.name == 'Beyond-HD':
                if not any('remember_web_' in x.rsplit('=', 1)[0] for x in self.cookies.split(';')):
                    return {
                        'result': False,
                        'message': "You haven't configured the required cookies. Please login at {provider_url}, "
                        'and make sure you have copied the following cookies: {required_cookies!r}'.format(
                            provider_url=self.name, required_cookies=self.required_cookies
                        )
                    }
            else:
                if not all(req_cookie in [x.rsplit('=', 1)[0] for x in self.cookies.split(';')]
                           for req_cookie in self.required_cookies):
                    return {
                        'result': False,
                        'message': "You haven't configured the required cookies. Please login at {provider_url}, "
                        'and make sure you have copied the following cookies: {required_cookies!r}'.format(
                            provider_url=self.name, required_cookies=self.required_cookies
                        )
                    }

        # cookie_validator got at least one cookie key/value pair, let's return success
        add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
        return {'result': True,
                'message': ''}
Ejemplo n.º 16
0
    def _getRSSData(self):
        logger.log(u"Cache update URL: %s" % self.provider.url, logger.DEBUG)

        if self.provider.cookies:
            add_dict_to_cookiejar(
                self.provider.session.cookies,
                dict(
                    x.rsplit('=', 1)
                    for x in self.provider.cookies.split(';')))

        return self.getRSSFeed(self.provider.url)
Ejemplo n.º 17
0
def run():
    # 第一次请求
    response = session.get(url, headers=header, verify=False)
    # 获取参数及加密方式
    parameter, js_file = get_parameter(response)
    # 获取cookie
    clearance = get_cookie(parameter, js_file)
    # 修改cookie
    add_dict_to_cookiejar(session.cookies, {'__jsl_clearance_s': clearance})
    # 第三次请求
    html = session.get(url, headers=header, verify=False)
    print(html.content.decode())
Ejemplo n.º 18
0
    def login(self):
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self._cookies)
        else:
            login_params = {
                'username': self.username,
                'password': self.password,
                'login': '******'
            }

            response = self.get_url(self.urls['login'],
                                    post_data=login_params,
                                    returns='text')
            if not response:
                logger.log(u"Unable to connect to provider", logger.WARNING)
                return False

            if re.search('/logout.php', response):

                try:
                    if dict_from_cookiejar(self.session.cookies
                                           )['uid'] and dict_from_cookiejar(
                                               self.session.cookies)['pass']:
                        self._uid = dict_from_cookiejar(
                            self.session.cookies)['uid']
                        self._hash = dict_from_cookiejar(
                            self.session.cookies)['pass']

                        self._cookies = {'uid': self._uid, 'pass': self._hash}
                        return True
                except Exception:
                    logger.log(u"Unable to login to provider (cookie)",
                               logger.WARNING)
                    return False

            else:
                if re.search(
                        'Username does not exist in the userbase or the account is not confirmed yet.',
                        response):
                    logger.log(
                        u"Invalid username or password. Check your settings",
                        logger.WARNING)

                if re.search('DDoS protection by CloudFlare', response):
                    logger.log(
                        u"Unable to login to provider due to CloudFlare DDoS javascript check",
                        logger.WARNING)

                    return False
Ejemplo n.º 19
0
    def login(self):
        """Login method used for logging in before doing search and torrent downloads."""
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        login_params = {
            'username': self.username,
            'password': self.password,
        }

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:
            response = self.get_url(self.urls['login'],
                                    post_data=login_params,
                                    returns='response')
            if not response or not response.text:
                logger.log('Unable to connect to provider', logger.WARNING)
                return False

            if re.search('/logout.php', response.text):
                try:
                    if dict_from_cookiejar(self.session.cookies)['uid'] and \
                            dict_from_cookiejar(self.session.cookies)['pass']:
                        self._uid = dict_from_cookiejar(
                            self.session.cookies)['uid']
                        self._hash = dict_from_cookiejar(
                            self.session.cookies)['pass']

                        self.cookies = {'uid': self._uid, 'pass': self._hash}
                        return True
                except Exception:
                    logger.log('Unable to login to provider (cookie)',
                               logger.WARNING)

                    return False
            else:
                if re.search('Username does not exist in the userbase or the account is not confirmed yet.', response.text) or \
                    re.search('Username or password is incorrect. If you have an account here please use the'
                              ' recovery system or try again.', response.text):
                    logger.log(
                        'Invalid username or password. Check your settings',
                        logger.WARNING)

                if re.search('DDoS protection by CloudFlare', response.text):
                    logger.log(
                        'Unable to login to provider due to CloudFlare DDoS javascript check',
                        logger.WARNING)

                    return False
Ejemplo n.º 20
0
    def search_by_author(self, surname, name=None, year=None):
        form_url = 'http://www.scopus.com'
        post_url = 'http://www.scopus.com/search/submit/basic.url'

        with self.throttler():
            r_form = self.session.get(form_url)

        add_dict_to_cookiejar(self.session.cookies, {
            'javaScript': 'true',
            'xmlHttpRequest': 'true'
        })

        et = html5lib.parse(r_form.text, treebuilder="lxml")
        namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
        search_form_html = et.find(
            "//{http://www.w3.org/1999/xhtml}form[@name='SearchResultsForm']")

        search_form = HTMLForm(search_form_html)
        search_term = surname
        if name:
            search_term += u', {}'.format(name)
        search_form.set_value('searchterm1', search_term)
        search_form.set_value('field1', 'AUTHOR-NAME')
        if year != None:
            search_form.set_value('yearFrom', str(year))
            search_form.set_value('yearTo', str(year))
            search_form.set_value('dateType', 'Publication_Date_Type')

        headers = {'Referer': r_form.url}

        with self.throttler():
            r_results = self.session.post(post_url,
                                          data=search_form.to_params(),
                                          headers=headers)

        et = html5lib.parse(r_results.text, treebuilder="lxml")
        namespaces = {'html': 'http://www.w3.org/1999/xhtml'}
        errors = et.xpath(
            ".//html:form[@name='SearchResultsForm']//*[contains(concat(' ', normalize-space(@class), ' '), ' errText ')]",
            namespaces=namespaces)

        for error in errors:
            if error.text.strip() == 'No results were found.':
                return []
            raise IOError('Error encountered during document search: ' +
                          error.text.strip())

        return self._download_from_results_form(
            r_results, context=['_search_by_author', surname, name, year])
Ejemplo n.º 21
0
    def add_cookies_from_ui(self):
        """
        Adds the cookies configured from UI to the providers requests session
        :return: A tuple with the the (success result, and a descriptive message in str)
        """

        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies and self.cookies:
            cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
            if not cookie_validator.match(self.cookies):
                return False, 'Cookie is not correctly formatted: {0}'.format(self.cookies)
            add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
            return True, 'torrent cookie'

        return False, 'No Cookies added from ui for provider: {0}'.format(self.name)
Ejemplo n.º 22
0
    def add_cookies_from_ui(self):
        """
        Adds the cookies configured from UI to the providers requests session
        :return: A tuple with the the (success result, and a descriptive message in str)
        """

        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies and self.cookies:
            cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
            if not cookie_validator.match(self.cookies):
                return False, 'Cookie is not correctly formatted: {0}'.format(self.cookies)
            add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
            return True, 'torrent cookie'

        return False, 'No Cookies added from ui for provider: {0}'.format(self.name)
Ejemplo n.º 23
0
    def add_cookies_from_ui(self):
        """
        Adds the cookies configured from UI to the providers requests session
        :return: A tuple with the the (success result, and a descriptive message in str)
        """

        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies and self.cookies:
            cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
            if cookie_validator.match(self.cookies):
                add_dict_to_cookiejar(sickrage.srCore.srWebSession.cookies,
                                      dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
                return True

        return False
Ejemplo n.º 24
0
def captcha_login_post():
    mongo_accounts = mongoCollection('accounts')
    data = request.get_data().decode('utf-8')
    data = json.loads(data)
    logger.debug(data)
    login_cookie = redis_db.get(settings.LOGIN_KEY % data['username'])
    if not login_cookie:
        return jsonify({
            'status': False,
            'message': {
                'reason': '更换用户名后请刷新验证码'
            }
        })
    login_cookie = json.loads(login_cookie.decode('utf-8'))
    session = requests.Session()
    login_cookie_jar = add_dict_to_cookiejar(session.cookies, login_cookie)
    # session = login_session[data['username']]
    cookies, result = pylogins.bilibili_login.login(data['username'],
                                                    data['password'],
                                                    data['captcha'], session)
    # Write cookie to mongo
    if cookies:
        logger.debug('Write login to mongo...')
        mongo_accounts.update_one(
            {
                'username': data['username'],
                'source': data['source'],
            }, {'$set': {
                'cookies': cookies,
                'update_ts': datetime.now(),
            }})
    return jsonify(result)
Ejemplo n.º 25
0
    def add_cookies_from_ui(self):
        """
        Adds the cookies configured from UI to the providers requests session
        :return: A tuple with the the (success result, and a descriptive message in str)
        """

        # This is the generic attribute used to manually add cookies for provider authentication
        if self.enable_cookies and self.cookies:
            cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
            if cookie_validator.match(self.cookies):
                add_dict_to_cookiejar(
                    sickrage.srCore.srWebSession.cookies,
                    dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
                return True

        return False
Ejemplo n.º 26
0
def test_add_dict_to_cookiejar(cookiejar):
    """Ensure add_dict_to_cookiejar works for
    non-RequestsCookieJar CookieJars
    """
    cookiedict = {"test": "cookies", "good": "cookies"}
    cj = add_dict_to_cookiejar(cookiejar, cookiedict)
    cookies = {cookie.name: cookie.value for cookie in cj}
    assert cookiedict == cookies
Ejemplo n.º 27
0
def test_add_dict_to_cookiejar(cookiejar):
    """Ensure add_dict_to_cookiejar works for
    non-RequestsCookieJar CookieJars
    """
    cookiedict = {'test': 'cookies', 'good': 'cookies'}
    cj = add_dict_to_cookiejar(cookiejar, cookiedict)
    cookies = dict((cookie.name, cookie.value) for cookie in cj)
    assert cookiedict == cookies
Ejemplo n.º 28
0
    def _new_session(self):
        """
        A new `requests.Session` which is authenticated with umd and can be
        used to access umd websites.

        "authenticated" here just means that this session has the `auth_cookies`
        we retrieved set, which is all being authenticated means to umd.
        """
        print("creating a new session")
        if not self.auth_cookies:
            self.authenticate()
        session = requests.Session()
        add_dict_to_cookiejar(session.cookies, self.auth_cookies)
        session.cookies.set("JSESSION",
                            self.identity_jsession_id,
                            domain="identity.umd.edu",
                            path="/")
        return session
Ejemplo n.º 29
0
def run(url):
    # 第一次请求
    response = ss.get(
        url,
        headers=headers,
    )
    if response.status_code == 200:
        return response
    else:
        # 获取参数及加密方式
        parameter, js_file = get_parameter(response, url)
        # 获取cookie
        clearance = get_cookie(parameter, js_file)
        # 修改cookie
        add_dict_to_cookiejar(ss.cookies, {'__jsl_clearance_s': clearance})
        # 第三次请求
        html = ss.get(url, headers=headers)
        return html
Ejemplo n.º 30
0
def test_add_dict_to_cookiejar(cookiejar):
    """Ensure add_dict_to_cookiejar works for
    non-RequestsCookieJar CookieJars
    """
    cookiedict = {'test': 'cookies',
                  'good': 'cookies'}
    cj = add_dict_to_cookiejar(cookiejar, cookiedict)
    cookies = dict((cookie.name, cookie.value) for cookie in cj)
    assert cookiedict == cookies
Ejemplo n.º 31
0
    def validateRSS(self):  # pylint: disable=too-many-return-statements

        try:
            if self.cookies:
                cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
                if not cookie_validator.match(self.cookies):
                    return False, 'Cookie is not correctly formatted: {0}'.format(
                        self.cookies)
                add_dict_to_cookiejar(
                    self.session.cookies,
                    dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

            # pylint: disable=protected-access
            # Access to a protected member of a client class
            data = self.cache._get_rss_data()['entries']
            if not data:
                return False, 'No items found in the RSS feed {0}'.format(
                    self.url)

            title, url = self._get_title_and_url(data[0])

            if not title:
                return False, 'Unable to get title from first item'

            if not url:
                return False, 'Unable to get torrent url from first item'

            if url.startswith('magnet:') and re.search(
                    r'urn:btih:([\w]{32,40})', url):
                return True, 'RSS feed Parsed correctly'
            else:
                torrent_file = self.get_url(url, returns='content')
                try:
                    helpers.bdecode(torrent_file, True)
                except (BTFailure, Exception) as error:
                    self.dumpHTML(torrent_file)
                    return False, 'Torrent link is not a valid torrent file: {0}'.format(
                        error)

            return True, 'RSS feed Parsed correctly'

        except Exception as error:
            return False, 'Error when trying to load RSS: {0}'.format(
                ex(error))
Ejemplo n.º 32
0
    def login(self):

        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:

            login_params = {
                'username': self.username,
                'password': self.password,
                'submit.x': 0,
                'submit.y': 0
            }

            response = self.get_url(self.urls['login'],
                                    post_data=login_params,
                                    timeout=30)
            if not response:
                logger.log(u"Unable to connect to provider", logger.WARNING)
                return False

            if re.search('You tried too often', response):
                logger.log(u"Too many login access attempts", logger.WARNING)
                return False

            try:
                if dict_from_cookiejar(
                        self.session.cookies)['uid'] and dict_from_cookiejar(
                            self.session.cookies)['pass']:
                    self._uid = dict_from_cookiejar(
                        self.session.cookies)['uid']
                    self._hash = dict_from_cookiejar(
                        self.session.cookies)['pass']

                    self.cookies = {'uid': self._uid, 'pass': self._hash}
                    return True
            except Exception:
                pass

            logger.log(u"Unable to obtain cookie", logger.WARNING)
            return False
Ejemplo n.º 33
0
    def __init__(self, ansible_module):
        if not HAS_REQUESTS:
            ansible_module.fail_json(
                msg='The "requests"'
                ' python library is required to use the aoscx connection type.'
            )
        s = RequestsSession()
        connection = Connection(ansible_module._socket_path)
        session_data = connection.get_session()

        if session_data['success'] is False:
            ansible_module.fail_json(msg="Connection Failed")

        add_dict_to_cookiejar(s.cookies, session_data["cookies"])
        if session_data["use_proxy"] is False:
            s.proxies = {"http": None, "https": None}
        self._session = dict(s=s,
                             url=session_data["url"],
                             credentials=session_data["credentials"])
Ejemplo n.º 34
0
def run():
    # 第一次请求
    response = session.get(url, params=params, headers=headers, verify=False)
    # 获取参数及加密方式
    parameter, js_file = get_parameter(response)
    # 获取cookie
    clearance = get_cookie(parameter, js_file)
    # 修改cookie
    add_dict_to_cookiejar(session.cookies, {'__jsl_clearance_s': clearance})
    # 第三次请求
    html = session.get(
        url,
        params=params,
        headers=headers,
    )
    if html.status_code == 200:
        content = html.content.decode('utf-8')

    print()
Ejemplo n.º 35
0
    def add_cookies_from_ui(self):
        """
        Add the cookies configured from UI to the providers requests session.

        :return: A dict with the the keys result as bool and message as string
        """
        # Added exception for rss torrent providers, as for them adding cookies initial should be optional.
        from medusa.providers.torrent.rss.rsstorrent import TorrentRssProvider
        if isinstance(self, TorrentRssProvider) and not self.cookies:
            return {'result': True,
                    'message': 'This is a TorrentRss provider without any cookies provided. '
                               'Cookies for this provider are considered optional.'}

        # This is the generic attribute used to manually add cookies for provider authentication
        if not self.enable_cookies:
            return {'result': False,
                    'message': 'Adding cookies is not supported for provider: {0}'.format(self.name)}

        if not self.cookies:
            return {'result': False,
                    'message': 'No Cookies added from ui for provider: {0}'.format(self.name)}

        cookie_validator = re.compile(r'^([\w%]+=[\w%]+)(;[\w%]+=[\w%]+)*$')
        if not cookie_validator.match(self.cookies):
            ui.notifications.message(
                'Failed to validate cookie for provider {provider}'.format(provider=self.name),
                'Cookie is not correctly formatted: {0}'.format(self.cookies))
            return {'result': False,
                    'message': 'Cookie is not correctly formatted: {0}'.format(self.cookies)}

        if not all(req_cookie in [x.rsplit('=', 1)[0] for x in self.cookies.split(';')]
                   for req_cookie in self.required_cookies):
            return {
                'result': False,
                'message': "You haven't configured the requied cookies. Please login at {provider_url}, "
                           'and make sure you have copied the following cookies: {required_cookies!r}'
                           .format(provider_url=self.name, required_cookies=self.required_cookies)
            }

        # cookie_validator got at least one cookie key/value pair, let's return success
        add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
        return {'result': True,
                'message': ''}
Ejemplo n.º 36
0
    def add_cookies_from_ui(self):
        """
        Add the cookies configured from UI to the providers requests session.
        :return: dict
        """

        if isinstance(self, TorrentRssProvider) and not self.cookies:
            return {'result': True,
                    'message': 'This is a TorrentRss provider without any cookies provided. '
                               'Cookies for this provider are considered optional.'}

        # This is the generic attribute used to manually add cookies for provider authentication
        if not self.enable_cookies:
            return {'result': False,
                    'message': 'Adding cookies is not supported for provider: {}'.format(self.name)}

        if not self.cookies:
            return {'result': False,
                    'message': 'No Cookies added from ui for provider: {}'.format(self.name)}

        cookie_validator = re.compile(r'^([\w%]+=[\w%]+)(;[\w%]+=[\w%]+)*$')
        if not cookie_validator.match(self.cookies):
            sickrage.app.alerts.message(
                'Failed to validate cookie for provider {}'.format(self.name),
                'Cookie is not correctly formatted: {}'.format(self.cookies))

            return {'result': False,
                    'message': 'Cookie is not correctly formatted: {}'.format(self.cookies)}

        if hasattr(self, 'required_cookies') and not all(
                req_cookie in [x.rsplit('=', 1)[0] for x in self.cookies.split(';')] for req_cookie in
                self.required_cookies):
            return {'result': False,
                    'message': "You haven't configured the required cookies. Please login at {provider_url}, "
                               "and make sure you have copied the following cookies: {required_cookies!r}"
                        .format(provider_url=self.name, required_cookies=self.required_cookies)}

        # cookie_validator got at least one cookie key/value pair, let's return success
        add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

        return {'result': True,
                'message': ''}
Ejemplo n.º 37
0
    def login(self):
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        login_params = {
            'username': self.username,
            'password': self.password,
        }

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:
            response = self.get_url(self.urls['login'], post_data=login_params, returns='text')
            if not response:
                logger.log('Unable to connect to provider', logger.WARNING)
                return False

            if re.search('/logout.php', response):
                try:
                    if dict_from_cookiejar(self.session.cookies)['uid'] and \
                            dict_from_cookiejar(self.session.cookies)['pass']:
                        self._uid = dict_from_cookiejar(self.session.cookies)['uid']
                        self._hash = dict_from_cookiejar(self.session.cookies)['pass']

                        self.cookies = {'uid': self._uid,
                                        'pass': self._hash}
                        return True
                except Exception:
                    logger.log('Unable to login to provider (cookie)', logger.WARNING)

                    return False
            else:
                if re.search('Username does not exist in the userbase or the account is not confirmed yet.', response) or \
                    re.search('Username or password is incorrect. If you have an account here please use the'
                              ' recovery system or try again.', response):
                    logger.log('Invalid username or password. Check your settings', logger.WARNING)

                if re.search('DDoS protection by CloudFlare', response):
                    logger.log('Unable to login to provider due to CloudFlare DDoS javascript check', logger.WARNING)

                    return False
Ejemplo n.º 38
0
    def validateRSS(self):

        try:
            if self.cookies:
                cookie_validator = re.compile(r"^(\w+=\w+)(;\w+=\w+)*$")
                if not cookie_validator.match(self.cookies):
                    return False, 'Cookie is not correctly formatted: ' + self.cookies

            # pylint: disable=protected-access
            # Access to a protected member of a client class
            data = self.cache._getRSSData()['entries']
            if not data:
                return False, 'No items found in the RSS feed ' + self.url

            (title, url) = self._get_title_and_url(data[0])

            if not title:
                return False, 'Unable to get title from first item'

            if not url:
                return False, 'Unable to get torrent url from first item'

            if url.startswith('magnet:') and re.search(r'urn:btih:([\w]{32,40})', url):
                return True, 'RSS feed Parsed correctly'
            else:
                if self.cookies:
                    add_dict_to_cookiejar(self.session.cookies,
                                                         dict(x.rsplit('=', 1) for x in self.cookies.split(';')))
                torrent_file = self.get_url(url, need_bytes=True)
                try:
                    bdecode(torrent_file)
                except Exception as e:
                    self.dumpHTML(torrent_file)
                    return False, 'Torrent link is not a valid torrent file: ' + ex(e)

            return True, 'RSS feed Parsed correctly'

        except Exception as e:
            return False, 'Error when trying to load RSS: ' + ex(e)
Ejemplo n.º 39
0
    def login(self):
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:
            login_params = {"username": self.username, "password": self.password, "login": "******"}

            response = self.get_url(self.urls["login"], post_data=login_params, returns="text")
            if not response:
                logger.log(u"Unable to connect to provider", logger.WARNING)
                return False

            if re.search("/logout.php", response):

                try:
                    if (
                        dict_from_cookiejar(self.session.cookies)["uid"]
                        and dict_from_cookiejar(self.session.cookies)["pass"]
                    ):
                        self._uid = dict_from_cookiejar(self.session.cookies)["uid"]
                        self._hash = dict_from_cookiejar(self.session.cookies)["pass"]

                        self.cookies = {"uid": self._uid, "pass": self._hash}
                        return True
                except Exception:
                    logger.log(u"Unable to login to provider (cookie)", logger.WARNING)
                    return False

            else:
                if re.search("Username does not exist in the userbase or the account is not confirmed yet.", response):
                    logger.log(u"Invalid username or password. Check your settings", logger.WARNING)

                if re.search("DDoS protection by CloudFlare", response):
                    logger.log(u"Unable to login to provider due to CloudFlare DDoS javascript check", logger.WARNING)

                    return False
Ejemplo n.º 40
0
    def login(self):

        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:

            login_params = {
                'username': self.username,
                'password': self.password,
                'submit.x': 0,
                'submit.y': 0
            }

            response = self.get_url(self.urls['login'], post_data=login_params, timeout=30)
            if not response:
                logger.log(u"Unable to connect to provider", logger.WARNING)
                return False

            if re.search('You tried too often', response):
                logger.log(u"Too many login access attempts", logger.WARNING)
                return False

            try:
                if dict_from_cookiejar(self.session.cookies)['uid'] and dict_from_cookiejar(self.session.cookies)['pass']:
                    self._uid = dict_from_cookiejar(self.session.cookies)['uid']
                    self._hash = dict_from_cookiejar(self.session.cookies)['pass']

                    self.cookies = {'uid': self._uid,
                                    'pass': self._hash}
                    return True
            except Exception:
                pass

            logger.log(u"Unable to obtain cookie", logger.WARNING)
            return False
Ejemplo n.º 41
0
    def login(self):
        if any(dict_from_cookiejar(self.session.cookies).values()):
            return True

        if self._uid and self._hash:
            add_dict_to_cookiejar(self.session.cookies, self.cookies)
        else:
            login_params = {'username': self.username,
                            'password': self.password,
                            'login': '******'}

            response = self.get_url(self.urls['login'], post_data=login_params, timeout=30)
            if not response:
                logger.log(u"Unable to connect to provider", logger.WARNING)
                return False

            if re.search('/logout.php', response):

                try:
                    if dict_from_cookiejar(self.session.cookies)['uid'] and dict_from_cookiejar(self.session.cookies)['pass']:
                        self._uid = dict_from_cookiejar(self.session.cookies)['uid']
                        self._hash = dict_from_cookiejar(self.session.cookies)['pass']

                        self.cookies = {'uid': self._uid,
                                        'pass': self._hash}
                        return True
                except Exception:
                    logger.log(u"Unable to login to provider (cookie)", logger.WARNING)
                    return False

            else:
                if re.search('Username does not exist in the userbase or the account is not confirmed yet.', response):
                    logger.log(u"Invalid username or password. Check your settings", logger.WARNING)

                if re.search('DDoS protection by CloudFlare', response):
                    logger.log(u"Unable to login to provider due to CloudFlare DDoS javascript check", logger.WARNING)

                    return False
Ejemplo n.º 42
0
    def validateRSS(self):  # pylint: disable=too-many-return-statements

        try:
            if self.cookies:
                cookie_validator = re.compile(r'^(\w+=\w+)(;\w+=\w+)*$')
                if not cookie_validator.match(self.cookies):
                    return False, 'Cookie is not correctly formatted: {0}'.format(self.cookies)
                add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

            # pylint: disable=protected-access
            # Access to a protected member of a client class
            data = self.cache._get_rss_data()['entries']
            if not data:
                return False, 'No items found in the RSS feed {0}'.format(self.url)

            title, url = self._get_title_and_url(data[0])

            if not title:
                return False, 'Unable to get title from first item'

            if not url:
                return False, 'Unable to get torrent url from first item'

            if url.startswith('magnet:') and re.search(r'urn:btih:([\w]{32,40})', url):
                return True, 'RSS feed Parsed correctly'
            else:
                torrent_file = self.get_url(url, returns='content')
                try:
                    bencode.bdecode(torrent_file)
                except (bencode.BTL.BTFailure, Exception) as error:
                    self.dumpHTML(torrent_file)
                    return False, 'Torrent link is not a valid torrent file: {0}'.format(error)

            return True, 'RSS feed Parsed correctly'

        except Exception as error:
            return False, 'Error when trying to load RSS: {0}'.format(ex(error))
Ejemplo n.º 43
0
    def _get_rss_data(self):
        if self.provider.cookies:
            add_dict_to_cookiejar(self.provider.session.cookies, dict(x.rsplit('=', 1) for x in self.provider.cookies.split(';')))

        return self.get_rss_feed(self.provider.url)
Ejemplo n.º 44
-1
    def login(self):
        """Login method used for logging in before doing a search and torrent downloads."""
        cookie_dict = dict_from_cookiejar(self.session.cookies)
        if cookie_dict.get('session'):
            return True

        if self.cookies:
            add_dict_to_cookiejar(self.session.cookies, dict(x.rsplit('=', 1) for x in self.cookies.split(';')))

        cookie_dict = dict_from_cookiejar(self.session.cookies)
        if cookie_dict.get('session'):
            return True

        login_params = {
            'submit': 'Login',
            'username': self.username,
            'password': self.password,
            'keeplogged': 1,
        }

        response = self.get_url(self.urls['login'], post_data=login_params, returns='text')
        if not response:
            logger.log(u"Unable to connect to provider", logger.WARNING)
            return False

        if re.search('<title>Login :: BJ-Share</title>', response):
            logger.log(u"Invalid username or password. Check your settings", logger.WARNING)
            return False

        return True