ading2210 / poe-api

[UNMAINTAINED] A reverse engineered Python API wrapper for Quora's Poe, which provides free access to ChatGPT, GPT-4, and Claude.
https://pypi.org/project/poe-api
GNU General Public License v3.0
2.51k stars 316 forks source link

fix function extract_formkey() #186

Closed canxin121 closed 1 year ago

canxin121 commented 1 year ago
def extract_formkey(html):
  script_regex = r"<script>.*?;</script>"
  script_text = re.findall(script_regex, html)[0]
  pattern = r'<script>window.\w+=function\(\){return window.\w+\("(\w+)"\);};</script>'
  match = re.search(pattern, html)
  if match:
      key = match.group(1)
  else:
      raise Exception("Failed to get key for decoding form key")
  pattern = r'return q\(\w+,(\[.*?\])\)\['
  match = re.search(pattern, script_text)
  if match:
      js_array_str = match.group(1)
      js_array = re.findall(r'0x[\da-fA-F]+', js_array_str)  # match hexadecimal numbers
      index_array = [int(num, 16) for num in js_array]  # convert to decimal
  else:
      raise Exception("Failed to decode js_array")
  return ''.join([key[index] for index in index_array])[:32]