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

[INSTALLATION] - Create a fetch loop #1165

Open
dmtrung14 opened this issue Jun 22, 2024 · 0 comments
Open

[INSTALLATION] - Create a fetch loop #1165

dmtrung14 opened this issue Jun 22, 2024 · 0 comments
Labels
installation_help Someone is having problems installing the api

Comments

@dmtrung14
Copy link

dmtrung14 commented Jun 22, 2024

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):

  • OS: Windows 11
  • TikTokApi Version 6.3.*

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:

@dmtrung14 dmtrung14 added the installation_help Someone is having problems installing the api label Jun 22, 2024
@dmtrung14 dmtrung14 changed the title [INSTALLATION] - Create a download loop [INSTALLATION] - Create a fetch loop Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installation_help Someone is having problems installing the api
Projects
None yet
Development

No branches or pull requests

1 participant