From 20af89c0c29abc63ef5e84e669fd4ef14998de40 Mon Sep 17 00:00:00 2001 From: "t.me/xtekky" <98614666+xtekky@users.noreply.github.com> Date: Sun, 23 Apr 2023 17:57:39 +0100 Subject: phind, user_agent and cf_clearance param for cloudflare bypass --- phind/README.md | 1 + phind/__init__.py | 20 ++++++++++++++------ phind/phind_test.py | 32 ++++++++++++++++++++++++++++++++ testing/phind_test.py | 31 ------------------------------- 4 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 phind/phind_test.py delete mode 100644 testing/phind_test.py diff --git a/phind/README.md b/phind/README.md index c9b85dbb..f17f487f 100644 --- a/phind/README.md +++ b/phind/README.md @@ -5,6 +5,7 @@ import phind # set cf_clearance cookie phind.cf_clearance = 'xx.xx-1682166681-0-160' +phind.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' # same as the one from browser you got cf_clearance from prompt = 'who won the quatar world cup' diff --git a/phind/__init__.py b/phind/__init__.py index 8bae9f17..629befa5 100644 --- a/phind/__init__.py +++ b/phind/__init__.py @@ -8,6 +8,7 @@ from re import findall from curl_cffi.requests import post cf_clearance = '' +user_agent = '' class PhindResponse: @@ -52,6 +53,9 @@ class PhindResponse: class Search: def create(prompt: str, actualSearch: bool = True, language: str = 'en') -> dict: # None = no search + if user_agent == '': + raise ValueError('user_agent must be set, refer to documentation') + if not actualSearch: return { '_type': 'SearchResponse', @@ -83,7 +87,7 @@ class Search: 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', - 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', + 'user-agent': user_agent } return post('https://www.phind.com/api/bing/search', headers = headers, json = { @@ -102,6 +106,9 @@ class Completion: codeContext: str = '', language: str = 'en') -> PhindResponse: + if user_agent == '': + raise ValueError('user_agent must be set, refer to documentation') + if results is None: results = Search.create(prompt, actualSearch = True) @@ -141,7 +148,7 @@ class Completion: 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', - 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', + 'user-agent': user_agent } completion = '' @@ -192,9 +199,7 @@ class StreamingCompletion: 'creative': creative } } - - print(cf_clearance) - + headers = { 'authority': 'www.phind.com', 'accept': '*/*', @@ -209,7 +214,7 @@ class StreamingCompletion: 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-origin', - 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', + 'user-agent': user_agent } response = post('https://www.phind.com/api/infer/answer', @@ -228,6 +233,9 @@ class StreamingCompletion: codeContext : str = '', language : str = 'en'): + if user_agent == '': + raise ValueError('user_agent must be set, refer to documentation') + if results is None: results = Search.create(prompt, actualSearch = True) diff --git a/phind/phind_test.py b/phind/phind_test.py new file mode 100644 index 00000000..94354fd8 --- /dev/null +++ b/phind/phind_test.py @@ -0,0 +1,32 @@ +import phind + +# set cf_clearance cookie +phind.cf_clearance = 'heguhSRBB9d0sjLvGbQECS8b80m2BQ31xEmk9ChshKI-1682268995-0-160' +phind.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36' + +prompt = 'hello world' + +# normal completion +result = phind.Completion.create( + model = 'gpt-4', + prompt = prompt, + results = phind.Search.create(prompt, actualSearch = False), # create search (set actualSearch to False to disable internet) + creative = False, + detailed = False, + codeContext = '') # up to 3000 chars of code + +print(result.completion.choices[0].text) + +prompt = 'who won the quatar world cup' + +# help needed: not getting newlines from the stream, please submit a PR if you know how to fix this +# stream completion +for result in phind.StreamingCompletion.create( + model = 'gpt-3.5', + prompt = prompt, + results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet) + creative = False, + detailed = False, + codeContext = ''): # up to 3000 chars of code + + print(result.completion.choices[0].text, end='', flush=True) \ No newline at end of file diff --git a/testing/phind_test.py b/testing/phind_test.py deleted file mode 100644 index 3e9217e1..00000000 --- a/testing/phind_test.py +++ /dev/null @@ -1,31 +0,0 @@ -import phind - -# set cf_clearance cookie -phind.cf_clearance = 'hWfIdYKgcnxnU5ayolWe9t7eEmAbULywS.qfHkm1T_A-1682166681-0-160' - -prompt = 'hello world' - -# normal completion -result = phind.Completion.create( - model = 'gpt-4', - prompt = prompt, - results = phind.Search.create(prompt, actualSearch = False), # create search (set actualSearch to False to disable internet) - creative = False, - detailed = False, - codeContext = '') # up to 3000 chars of code - -print(result.completion.choices[0].text) - -prompt = 'who won the quatar world cup' - -# help needed: not getting newlines from the stream, please submit a PR if you know how to fix this -# stream completion -for result in phind.StreamingCompletion.create( - model = 'gpt-3.5', - prompt = prompt, - results = phind.Search.create(prompt, actualSearch = True), # create search (set actualSearch to False to disable internet) - creative = False, - detailed = False, - codeContext = ''): # up to 3000 chars of code - - print(result.completion.choices[0].text, end='', flush=True) \ No newline at end of file -- cgit v1.2.3