helallao / perplexity-ai

Unofficial API Wrapper for Perplexity.ai + Account Generator
https://perplexity.ai
MIT License
285 stars 50 forks source link

Stuck on search #6

Closed phamxtien closed 1 year ago

phamxtien commented 1 year ago

I run it with below code:

import json
import time
import requests
import random
from bs4 import BeautifulSoup
from websocket import WebSocketApp
from uuid import uuid4
from threading import Thread

cookies = {
    'cf_clearance': 'LH02k9dzyA4wEdXW0m5WANalfq.sx6VDjhgkiGXkHag-1690102887-0-160.0.0',
    '_ga': 'GA1.1.415630709.1690102894',
    'g_state': '{"i_l":0}',
    .............
}

headers = {
    'authority': 'www.perplexity.ai',
    'accept': '*/*',
    'accept-language': 'en-US,en;q=0.9,vi;q=0.8',
    'content-type': 'application/json',
    ..........
}

def souper(x):
    return BeautifulSoup(x, 'lxml')

class Emailnator:
    def __init__(self, headers, cookies, domain=False, plus=True, dot=True, google_mail=False):
        self.inbox = []
        self.inbox_ads = []

        self.s = requests.Session()
        self.s.headers.update(headers)
        self.s.cookies.update(cookies)

        data = {'email': []}

        if domain:
            data['email'].append('domain')
        if plus:
            data['email'].append('plusGmail')
        if dot:
            data['email'].append('dotGmail')
        if google_mail:
            data['email'].append('googleMail')

        response = self.s.post('https://www.emailnator.com/generate-email', json=data).json()
        self.email = response['email'][0]

        for ads in self.s.post('https://www.emailnator.com/message-list', json={'email': self.email}).json()['messageData']:
            self.inbox_ads.append(ads['messageID'])

    def reload(self, wait=False, retry_timeout=5):
        self.new_msgs = []

        while True:
            for msg in self.s.post('https://www.emailnator.com/message-list', json={'email': self.email}).json()['messageData']:
                if msg['messageID'] not in self.inbox_ads and msg not in self.inbox:
                    self.new_msgs.append(msg)

            if wait and not self.new_msgs:
                time.sleep(retry_timeout)
            else:
                break

        self.inbox += self.new_msgs
        return self.new_msgs

    def open(self, msg_id):
        return self.s.post('https://www.emailnator.com/message-list', json={'email': self.email, 'messageID': msg_id}).text

class Client:
    def __init__(self, headers, cookies):
        self.session = requests.Session()
        self.session.headers.update(headers)
        self.session.cookies.update(cookies)
        self.session.get(f'https://www.perplexity.ai/search/{str(uuid4())}')

        self.t = format(random.getrandbits(32), '08x')
        self.sid = json.loads(self.session.get(f'https://www.perplexity.ai/socket.io/?EIO=4&transport=polling&t={self.t}').text[1:])['sid']
        self.frontend_uuid = str(uuid4())
        self.frontend_session_id = str(uuid4())
        self._last_answer = None
        self.copilot = 0
        self.n = 1

        assert self.session.post(f'https://www.perplexity.ai/socket.io/?EIO=4&transport=polling&t={self.t}&sid={self.sid}', data='40{"jwt":"anonymous-ask-user"}').text == 'OK'

        self.ws = WebSocketApp(
            url=f'wss://www.perplexity.ai/socket.io/?EIO=4&transport=websocket&sid={self.sid}',
            cookie='; '.join([f'{x}={y}' for x, y in self.session.cookies.get_dict().items()]),
            on_open=lambda ws: ws.send('2probe'),
            on_message=self.on_message,
            on_error=lambda ws, err: print(f'Error: {err}'),
        )

        Thread(target=self.ws.run_forever).start()
        time.sleep(1)

    def create_account(self, headers, cookies):
        emailnator_cli = Emailnator(headers, cookies, dot=False, google_mail=True)
        resp = self.session.post('https://www.perplexity.ai/api/auth/signin/email', data={
            'email': emailnator_cli.email,
            'csrfToken': self.session.cookies.get_dict()['next-auth.csrf-token'].split('%')[0],
            'callbackUrl': 'https://www.perplexity.ai/',
            'json': 'true',
        })

        if resp.ok:
            new_msgs = emailnator_cli.reload(wait=True)
            new_account_link = souper(emailnator_cli.open(new_msgs[0]['messageID'])).select('a')[1].get('href')

            self.session.get(new_account_link)
            self.session.get('https://www.perplexity.ai/')

            self.copilot = 5

            return True

    def on_message(self, ws, message):
        if message == '2':
            ws.send('3')
        elif message == '3probe':
            ws.send('5')

        if message.startswith(str(430 + self.n)):
            response = json.loads(message[3:])[0]
            response['text'] = json.loads(response['text'])
            self._last_answer = response

    def search(self, query, mode='concise', focus='internet'):
        assert mode in ['concise', 'copilot'], 'Search modes --> ["concise", "copilot"]'
        assert focus in ['internet', 'scholar', 'writing', 'wolfram', 'youtube', 'reddit', 'wikipedia'], 'Search focus modes --> ["internet", "scholar", "writing", "wolfram", "youtube", "reddit", "wikipedia"]'
        assert self.copilot > 0 if mode == 'copilot' else True, 'You have used all of your copilots'

        self.copilot = self.copilot - 1 if mode == 'copilot' else self.copilot
        self.n += 1
        self._last_answer = None

        self.ws.send(f'{420 + self.n}' + json.dumps([
            'perplexity_ask',
            query,
            {
                'source': 'default',
                'mode': mode,
                'last_backend_uuid': None,
                'read_write_token': '',
                'conversational_enabled': True,
                'frontend_session_id': self.frontend_session_id,
                'search_focus': focus,
                'frontend_uuid': self.frontend_uuid,
                'gpt4': False,
                'language': 'en-US',
            }
        ]))

        while not self._last_answer:
            pass

        return self._last_answer

print('__init__')
perplexity_cli = Client(headers, cookies)
print('__search__')
print(perplexity_cli.search('What is iso 9001?'))

Output: init search

helallao commented 1 year ago

i just replaced my cookies/headers and it's working for me:

__init__
__search__
{'status': 'completed', 'uuid': '335ee6b9-1f0f-4311-b3d6-7063e6a3e0dd', 'read_write_token': '4d822bc6-0b2f-4884-be1a-ae9e79f878d6', 'text': {'answer': 'ISO 9001 is an international standard for quality management systems that provides a framework for processes to systematically improve all areas of an organization[2][5]. It is one of the five quality management systems standards that make up the ISO 9000 family[1]. The ISO 9001 standard is designed to help organizations focus on customer requirements and meet customer and other stakeholder needs within statutory and regulatory requirements related to a product or service[1][3][6]. \n\nISO 9001 sets out the criteria for a quality management system and is the only standard in the family that can be certified to, although certification is not a requirement[2][5]. The standard is based on a number of quality management principles, including a strong customer focus, the motivation and implication of top management, the process approach, and continual improvement[2]. \n\nAll the requirements of ISO 9001 are generic and are intended to be applicable to any organization, regardless of its type or size, or the products and services it provides[3]. The latest version of the ISO 9001 standard is ISO 9001:2015, which was published in September 2015[3][6].', 'web_results': [{'name': 'ISO 9000', 'url': 'https://en.wikipedia.org/wiki/ISO_9000', 'snippet': 'The ISO 9000 family is a set of five quality management systems standards that help organizations ensure they meet customer and other stakeholder needs within statutory and regulatory requirements related to a product or service.', 'meta_data': None, 'is_attachment': False}, {'name': 'ISO 9001 and related standards — Quality management', 'url': 'https://www.iso.org/iso-9001-quality-management.html', 'snippet': 'ISO 9001 sets out the criteria for a quality management system and is the only standard in the family that can be certified to (although this is not a ...', 'meta_data': None, 'is_attachment': False}, {'name': 'ISO 9001:2015 - Quality management systems — Requirements', 'url': 'https://www.iso.org/standard/62085.html', 'snippet': 'All the requirements of ISO 9001:2015 are generic and are intended to be applicable to any organization, regardless of its type or size, or the products and ...', 'meta_data': None, 'is_attachment': False}, {'name': 'What is the ISO 9001 standard? A straightforward overview - Advisera', 'url': 'https://advisera.com/9001academy/what-is-iso-9001/', 'snippet': 'ISO 9001 is the international standard for creating a Quality Management Systems (QMS), published by ISO (the International Organization for Standardization).', 'meta_data': None, 'is_attachment': False}, {'name': 'What Is ISO 9001 Certification? Standards and Requirements - Diligent Corporation', 'url': 'https://www.diligent.com/insights/compliance/what-is-iso-9001-certification-standards-and-requirements/', 'snippet': 'ISO 9001 is one of the most popular quality management system standards in the world today. It provides a framework for processes to ...', 'meta_data': None, 'is_attachment': False}, {'name': 'ISO 9001 Standard: What is it? Who needs Certification and Why? - 9000 Store', 'url': 'https://the9000store.com/what-are-iso-9000-standards/what-is-iso-9001/', 'snippet': 'ISO 9001 is the international standard for a quality management system (“QMS”). The standard is designed to help companies focus on customer requirements...', 'meta_data': None, 'is_attachment': False}], 'chunks': ['', 'ISO 9001 is an', ' international', ' standard for', ' quality management', ' systems that', ' provides a framework', ' for processes', ' to systematically', ' improve all areas', ' of an organization', '[2][5]. It is', ' one of the five', ' quality management', ' systems standards', ' that make up', ' the ISO 9000 family', '[1]. The ISO', ' 9001 standard', ' is designed to', ' help organizations', ' focus on customer', ' requirements', ' and meet customer', ' and other stake', 'holder needs within', ' statutory and', ' regulatory requirements', ' related to a', ' product or service', '[1][3][6]. \n\nISO', ' 9001 sets out', ' the criteria', ' for a quality', ' management system', ' and is the only', ' standard in the', ' family that can', ' be certified', ' to, although', ' certification', ' is not a requirement', '[2][5]. The standard', ' is based on a', ' number of quality', ' management principles', ', including a', ' strong customer', ' focus, the motivation', ' and implication', ' of top management', ', the process', ' approach, and', ' continual improvement', '[2]. \n\nAll the', ' requirements', ' of ISO 9001 are', ' generic and are', ' intended to be', ' applicable to', ' any organization', ', regardless of', ' its type or size', ', or the products', ' and services', ' it provides[3]. The', ' latest version', ' of the ISO 9001 standard', ' is ISO 9001:2015, which', ' was published', ' in September', ' 2015[3][6].'], 'extra_web_results': [{'name': 'ISO 9001 Certification - Quality Management Standard - NQA', 'url': 'https://www.nqa.com/en-us/certification/standards/iso-9001', 'snippet': 'ISO 9001 is the internationally recognized standard for Quality Management Systems (QMS). It is the most widely used QMS standard in the ...', 'meta_data': None, 'is_attachment': False}, {'name': 'What is ISO 9001? Quick Guide to ISO 9001:2015 Quality Management Systems - YouTube', 'url': 'https://youtube.com/watch?v=O5T4H8K_rwQ', 'snippet': 'What is ISO 9001:2015? We delve into the world of ISO ...', 'meta_data': None, 'is_attachment': False}], 'deleted_urls': [], 'image_metadata': []}, 'final': True, 'backend_uuid': 'd9dd73a7-0145-4f59-962e-c4701b42bb1f', 'search_focus': 'internet', 'mode': 'concise', 'gpt4': False, 'personalized': False, 'label': '', 'step_type': 'FINAL', 'query_str': 'What is iso 9001?', 'related_queries': ['what are the benefits of implementing ISO 9001 in an organization', 'how to get ISO 9001 certification', 'what are the requirements of ISO 9001:2015'], 'context_uuid': '650ed804-09e6-458f-8dbc-9a9a038bffa1', 'thread_title': 'What is iso 9001?', 'author_username': None, 'author_image': None, 's3_social_preview_url': 'https://ppl-ai-public.s3.amazonaws.com/static/img/logo-dot-field.png'}

Try to update your cookies/headers correctly, look at How To Get The Cookies.