davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.85k stars 974 forks source link

[BUG] - Your Error Here #869

Closed oodles-prince closed 1 year ago

oodles-prince commented 2 years ago

Bug: There is no current event loop in thread 'Thread-1'. while i fetch view count from video using tiktok api

Code:

from flask import request, Flask, jsonify from flask_cors import CORS from flask_restful import Resource, Api from TikTokApi import TikTokApi app = Flask(name) api = Api(app) cors = CORS(app) tikTokApi = TikTokApi()

@app.errorhandler(404) def page_not_found(e): return {'status': 'fail'}, 404

class test(Resource): def post(self): return 'TIKTOK API IS Running'

class twitterviewcount(Resource): def post(self): if request.method == 'POST': if 'URL' not in request.json: return 'No request URL found!' data = request.json HASHTAG = data["hashtag"] URL = data["URL"] video = tikTokApi.video(url=URL) video_views = video.as_dict['stats']['playCount'] hashtag_detected = HASHTAG in video.as_dict['desc'] res = {} res['Hashtag'] = hashtag_detected res['view'] = video_views print(HASHTAG), return jsonify(res)

class twitterfollowercount(Resource): def post(self): if request.method == 'POST': if 'URL' not in request.json: return 'No request URL found!' data = request.json data = request.json URL = data["URL"] video = tikTokApi.video(url=URL) user_id = video.as_dict['author']['id'] user = api.user(user_id) user_followers = user.info_full()['stats']['followerCount'] res = {} res['Follower'] = user_followers return jsonify(res)

api.add_resource(test, '/') api.add_resource(twitterviewcount, '/tiktokview') api.add_resource(twitterfollowercount, '/tiktokfollower')

if name == 'main': app.run(host='0.0.0.0',debug=True, threaded=False)


**Expected behavior**

A need a view count from tiktok video URL

Error:

<textarea cols="50" rows="10" name="code" readonly>Traceback (most recent call last):
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 2080, in wsgi_app
    response = self.handle_exception(e)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_cors/extension.py&quot;, line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py&quot;, line 271, in error_router
    return original_handler(e)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_cors/extension.py&quot;, line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py&quot;, line 271, in error_router
    return original_handler(e)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/app.py&quot;, line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py&quot;, line 467, in wrapper
    resp = resource(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask/views.py&quot;, line 84, in view
    return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs)
  File &quot;/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py&quot;, line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File &quot;/main.py&quot;, line 27, in post
    video_views = video.as_dict[&#x27;stats&#x27;][&#x27;playCount&#x27;]
  File &quot;/usr/local/lib/python3.8/dist-packages/TikTokApi/api/video.py&quot;, line 145, in __getattr__
    self.as_dict = self.info()
  File &quot;/usr/local/lib/python3.8/dist-packages/TikTokApi/api/video.py&quot;, line 70, in info
    return self.info_full(**kwargs)[&quot;itemInfo&quot;][&quot;itemStruct&quot;]
  File &quot;/usr/local/lib/python3.8/dist-packages/TikTokApi/api/video.py&quot;, line 92, in info_full
    return self.parent.get_data(path, **kwargs)
  File &quot;/usr/local/lib/python3.8/dist-packages/TikTokApi/tiktok.py&quot;, line 224, in get_data
    ) = asyncio.get_event_loop().run_until_complete(
  File &quot;/usr/lib/python3.8/asyncio/events.py&quot;, line 639, in get_event_loop
    raise RuntimeError(&#x27;There is no current event loop in thread %r.&#x27;
RuntimeError: There is no current event loop in thread &#x27;Thread-1&#x27;.
</textarea>

**Desktop

kangear commented 2 years ago

same error on docker.