davidteather / TikTok-Api

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

[INSTALLATION] - Create a fetch loop #1165

Open dmtrung14 opened 1 week ago

dmtrung14 commented 1 week ago

I am trying to do a frontier BFS search on the hashtags. But I cannot go to the second hashtag.

    async def get_hashtags(self, depth = 5):
        async with TikTokApi() as api:
            current_depth = 0
            frontier = self.hashtags_seeds
            visited = set()
            while current_depth < depth:
                current_frontier = set()
                for old_tag in frontier:
                    await api.create_sessions(ms_tokens=[self.ms_token], 
                                      num_sessions=1, 
                                      sleep_after=3,
                                      headless=False,
                                      suppress_resource_load_types=self.suppress_resource_load_types)
                    if old_tag in visited:
                        continue
                    visited.add(old_tag)
                    tag = api.hashtag(name=old_tag)
                    number_video = 10
                    await api.close_sessions()
                    new_tags = await self.expand_frontier(tag_name = old_tag,
                                                          number_video = number_video)
                    current_frontier.update(new_tags)
                    time.sleep(0.5)
                frontier = current_frontier
                current_depth += 1
            with open('data/hashtags.json', 'w') as f:
                json.dump(self.hashtags, f, indent=4)

    async def expand_frontier(self, tag_name: str, number_video: int):
        async with TikTokApi() as api:
            await api.create_sessions(ms_tokens=[self.ms_token], 
                                      num_sessions=1, 
                                      sleep_after=10, 
                                      headless=False,
                                      suppress_resource_load_types=self.suppress_resource_load_types)
            tag = api.hashtag(name=tag_name)
            count = 0
            new_tags = set()
            async for video in tag.videos(count=100):
                count += 1
                video_dict = video.as_dict
                for tag in video_dict['challenges']:
                    new_tags.add(tag['title'])
                if count >= number_video:
                    break
            await api.close_sessions()
            return list(new_tags)

Please insert the code that is throwing errors or is giving you weird unexpected results.

Error Trace (if any)

Put the error trace below if there's any error thrown.

Traceback (most recent call last):
  File "C:\Users\progr\Documents\GitHub\tiktok-techjam24-factcheck\backend\explore.py", line 79, in <module>
    asyncio.run(explorer.get_hashtags())
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\progr\Documents\GitHub\tiktok-techjam24-factcheck\backend\explore.py", line 38, in get_hashtags
    tag_info = await tag.info()
               ^^^^^^^^^^^^^^^^
  File "C:\Users\progr\Documents\GitHub\tiktok-techjam24-factcheck\tiktok\Lib\site-packages\TikTokApi\api\hashtag.py", line 71, in info
    resp = await self.parent.make_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\Documents\GitHub\tiktok-techjam24-factcheck\tiktok\Lib\site-packages\TikTokApi\tiktok.py", line 403, in make_request
    i, session = self._get_session(**kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\Documents\GitHub\tiktok-techjam24-factcheck\tiktok\Lib\site-packages\TikTokApi\tiktok.py", line 318, in _get_session
    return i, self.sessions[i]
              ~~~~~~~~~~~~~^^^
IndexError: list index out of range
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000002B5FFC368E0>
Traceback (most recent call last):
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 116, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
                               ^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 80, in __repr__
    info.append(f'fd={self._sock.fileno()}')
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_utils.py", line 102, in fileno
    raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x000002B5FFC34FE0>
Traceback (most recent call last):
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_subprocess.py", line 125, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
                               ^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_subprocess.py", line 70, in __repr__
    info.append(f'stdin={stdin.pipe}')
                        ^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 80, in __repr__
    info.append(f'fd={self._sock.fileno()}')
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_utils.py", line 102, in fileno
    raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe
Exception ignored in: <function StreamWriter.__del__ at 0x000002B5FFC2A200>
Traceback (most recent call last):
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\streams.py", line 397, in __del__
    self.close()
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\streams.py", line 343, in close
    return self._transport.close()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 109, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 772, in call_soon
    self._check_closed()
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 519, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000002B5FFC368E0>
Traceback (most recent call last):
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 116, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
                               ^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 80, in __repr__
    info.append(f'fd={self._sock.fileno()}')
                      ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\progr\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_utils.py", line 102, in fileno
    raise ValueError("I/O operation on closed pipe")
ValueError: I/O operation on closed pipe

Desktop (please complete the following information):

Additional context

It is basically a BFS Algorithm so don't worry about it too much. I tried to close the sessions, otherwise it will throw errors because it is trying to write something to the local browser cookies files and it cannot. It throws error on the second item in the for loop: for old_tag in frontier: