1

I'm trying to create a Next.js api endpoint in python, its full path is api/search/[slug].py. It makes calls to a python TikTok api wrapper. The following code works but I would like to make the tag value be obtained dynamically from the slug.

from http.server import BaseHTTPRequestHandler
from TikTokApi import TikTokApi
import json

#search
api = TikTokApi()

videos=list(api.hashtag(name='tag').videos(count=10))
userDict={}

for v in videos:
  userDict[v.author.username]=v.as_dict['authorStats']['followerCount']

userDict = dict(sorted(userDict.items(),key=lambda item: item[1], reverse=True))
json_object = json.dumps(userDict, indent = 4)
#end search

class handler(BaseHTTPRequestHandler):

  def do_GET(self):

    self.send_response(200)
    self.send_header('Content-type', 'text/plain')
    self.end_headers()
    self.wfile.write(json_object.encode())
    return

If I extract the function and call it in the BaseHTTPRequestHandler handler I get the error:

127.0.0.1 - - [03/Oct/2022 15:02:36] "GET /api/search/slug?slug=slug HTTP/1.1" 200 -
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 59466)
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socketserver.py", line 747, in __init__
    self.handle()
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/server.py", line 432, in handle
    self.handle_one_request()
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/server.py", line 420, in handle_one_request
    method()
  File "/private/var/folders/5r/_0bfsh314nxcvsxckx0nk8mm0000gn/T/zeit-fun-2f2e7e68416d5/./api/search/[slug].py", line 25, in do_GET
    json_object = search()
  File "/private/var/folders/5r/_0bfsh314nxcvsxckx0nk8mm0000gn/T/zeit-fun-2f2e7e68416d5/./api/search/[slug].py", line 6, in search
    api = TikTokApi()
  File "/opt/homebrew/lib/python3.10/site-packages/TikTokApi/tiktok.py", line 159, in __init__
    self._initialize(
  File "/opt/homebrew/lib/python3.10/site-packages/TikTokApi/tiktok.py", line 205, in _initialize
    self._browser = asyncio.get_event_loop().run_until_complete(
  File "/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/events.py", line 656, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Dummy-1'.
----------------------------------------
ajnobre
  • 11
  • 1

0 Answers0