Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make API work with an already running event loop #198 #232

Merged
merged 6 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed TikTokApi/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/acrawler.cpython-37.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/browser.cpython-36.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/browser.cpython-38.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/stealth.cpython-37.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/stealth.cpython-38.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/tiktok.cpython-36.pyc
Binary file not shown.
Binary file removed TikTokApi/__pycache__/tiktok.cpython-38.pyc
Binary file not shown.
25 changes: 17 additions & 8 deletions TikTokApi/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import atexit
import requests
import logging
from threading import Thread

# Import Detection From Stealth
from .stealth import stealth
Expand Down Expand Up @@ -49,15 +50,20 @@ def __init__(self, url, language='en', proxy=None, find_redirect=False, api_url=
}

try:
self.loop = asyncio.new_event_loop()
loop = asyncio.new_event_loop()

t = Thread(target=self.__start_background_loop, args=(loop, ))
t.setDaemon(True)
t.start()
if find_redirect:
self.loop.run_until_complete(self.find_redirect())
fut = asyncio.run_coroutine_threadsafe(self.find_redirect(), loop)
elif newParams:
self.loop.run_until_complete(self.newParams())
fut = asyncio.run_coroutine_threadsafe(self.newParams(), loop)
else:
self.loop.run_until_complete(self.start())
fut = asyncio.run_coroutine_threadsafe(self.start(), loop)
fut.result()
except:
self.loop.close()
loop.close()

async def newParams(self):
self.browser = await pyppeteer.launch(self.options)
Expand Down Expand Up @@ -110,7 +116,6 @@ async def start(self):
string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(16))

await self.page.evaluate("() => { " + get_acrawler() + " }")

self.signature = await self.page.evaluate('''() => {
var url = "''' + self.url + "&verifyFp=" + self.verifyFp + '''"
var token = window.byted_acrawler.sign({url: url});
Expand All @@ -125,8 +130,8 @@ async def start(self):
})

self.data = await self.page.content()
self.data = json.loads(self.data.replace("</pre></body></html>", "").replace(
'<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">', ""))
#self.data = json.loads(self.data.replace("</pre></body></html>", "").replace(
# '<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">', ""))

await self.browser.close()
self.browser.process.communicate()
Expand Down Expand Up @@ -184,3 +189,7 @@ def __format_proxy(self, proxy):

def __get_js(self, proxy=None):
return requests.get("https://sf16-muse-va.ibytedtos.com/obj/rc-web-sdk-gcs/acrawler.js", proxies=self.__format_proxy(proxy)).text

def __start_background_loop(self, loop):
asyncio.set_event_loop(loop)
loop.run_forever()