├── README.md ├── perplexitymain.py └── req.txt /README.md: -------------------------------------------------------------------------------- 1 | # perplexity-thing 2 | 3 | update: 4 | Hi everyone :] 5 | i was really busy with work and had no time to update this code, but everything finally settled down and now i can work on this thing again 👍🏻 6 | 7 | 8 | this is an api wrapper of perplexity.ai that was made by me, a very dumb fella. 9 | (i am new to github and python in general, so please don't expect me to fix all of the bugs :>) 10 | 11 | 12 | **Usage:** 13 | 14 | you can use the python file in your project by importing it: 15 | 16 | ```import perplexitymain``` 17 | 18 | due to the fact that i don't know how to make good wrappers, this script uses selenium and Chrome to get the text and other stuff. to download it, go to https://chromedriver.chromium.org/downloads 19 | 20 | to send a request, you need to call ```ask(*your input text*, *path to chrome*)``` 21 | 22 | you will get a list of multiple objects, the first is going to be the main text that the ai has generated, and the others are the sources. 23 | 24 | Please expect for this script not to work sometimes <3 25 | 26 | 27 | **TODO:** 28 | 29 | * Add a proper way to send requests to the webstite 30 | * (somehow) bypass the cloudflare check 31 | * add a support for a follow up 32 | 33 | 34 | *also please dont sue me this is a small test project, i will take it down if this gets illegal :[* 35 | -------------------------------------------------------------------------------- /perplexitymain.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from selenium.webdriver.common.by import By 3 | from selenium.webdriver.chrome.options import Options 4 | from selenium.webdriver.common.keys import Keys 5 | import time 6 | import json 7 | def ask(input_text: str, path: str): 8 | # create webdriver object 9 | options = Options() 10 | options.set_capability("goog:loggingPrefs", {"performance": "ALL", "browser": "ALL"}) 11 | options.add_argument('--headless') 12 | options.add_argument('--disable-gpu') 13 | driver = webdriver.Chrome(options=options,executable_path=path) 14 | 15 | 16 | driver.get("https://www.perplexity.ai/") 17 | # get element 18 | element = driver.find_element(By.ID, "ppl-query-input") 19 | element.send_keys(input_text) 20 | element.send_keys(Keys.ENTER) 21 | listofzero=[] 22 | while True: 23 | list = [] 24 | log_entries = driver.get_log("performance") 25 | for entry in log_entries: 26 | obj_serialized: str = entry.get("message") 27 | obj = json.loads(obj_serialized) 28 | message = obj.get("message").get("params").get("type") 29 | if(str(message) == "Fetch"): 30 | list.append(message) 31 | if len(list) == 0: 32 | listofzero.append("0") 33 | time.sleep(0.5) 34 | else: 35 | time.sleep(0.5) 36 | if len(listofzero) >= 3: 37 | time.sleep(1) 38 | break 39 | finlist = [] 40 | finlist.append(driver.find_element(By.XPATH,'/html/body/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]').text) 41 | for i in driver.find_element(By.XPATH,"//div[@class='min-h-[81px]'][1]").find_elements(By.XPATH,".//a"): 42 | finlist.append(f"{i.text}: {i.get_attribute('href')}") 43 | return finlist -------------------------------------------------------------------------------- /req.txt: -------------------------------------------------------------------------------- 1 | selenium 2 | time 3 | json --------------------------------------------------------------------------------