Skip to content

Commit

Permalink
#42 - Change stderr prints to warnings
Browse files Browse the repository at this point in the history
* Also create new category, tiktokapipy.TikTokAPIWarning
  • Loading branch information
Russell-Newton committed May 23, 2023
1 parent 7106aba commit 7f18280
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ async with AsyncTikTokAPI() as api:
More examples, including how to download videos and slideshows, can be found in the
[documentation](https://tiktokpy.readthedocs.io/en/latest/users/usage.html#examples).

Warnings can be ignored as follows:

```py
import warnings

from tiktokapipy import TikTokAPIWarning

warnings.filterwarnings("ignore", category=TikTokAPIWarning)
```

## Documentation

You can view the full documentation on [Read the Docs](https://tiktokpy.readthedocs.io/en/latest/).
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
project = "TikTokPy"
copyright = "2023, Russell Newton"
author = "Russell Newton"
release = "0.1.13"
release = "0.1.13.post1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "tiktokapipy"
version = "0.1.13"
version = "0.1.13.post1"
authors = [
{ name="Russell Newton", email="[email protected]" },
]
Expand Down
4 changes: 4 additions & 0 deletions src/tiktokapipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ class TikTokAPIError(Exception):
"""Raised when the API encounters an error"""

pass


class TikTokAPIWarning(RuntimeWarning):
pass
14 changes: 9 additions & 5 deletions src/tiktokapipy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import json
import sys
import traceback
import warnings
from abc import ABC, abstractmethod
Expand All @@ -29,7 +28,7 @@
import playwright.sync_api
from playwright.sync_api import Page, Route, TimeoutError, sync_playwright
from pydantic import ValidationError
from tiktokapipy import TikTokAPIError
from tiktokapipy import TikTokAPIError, TikTokAPIWarning
from tiktokapipy.models import DeferredIterator, TikTokDataModel
from tiktokapipy.models.challenge import Challenge, LightChallenge, challenge_link
from tiktokapipy.models.raw_data import (
Expand Down Expand Up @@ -434,7 +433,11 @@ def capture_api_extras(route: Route):
page.close()
continue
except TimeoutError:
print("Reached navigation timeout. Retrying...", file=sys.stderr)
warnings.warn(
"Reached navigation timeout. Retrying...",
category=TikTokAPIWarning,
stacklevel=2,
)
page.close()
continue
break
Expand Down Expand Up @@ -546,10 +549,11 @@ def _extract_video_from_response(

video.comments = comments
if not video.comments:
print(
warnings.warn(
"Was unable to collect comments.\n"
"A second attempt or setting a nonzero value for scroll_down_time might work.",
file=sys.stderr,
category=TikTokAPIWarning,
stacklevel=2,
)
if isinstance(video.author, LightUser):
video.creator = self._light_user_getter_type(video.author.unique_id, self)
Expand Down
10 changes: 7 additions & 3 deletions src/tiktokapipy/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from __future__ import annotations

import json
import sys
import traceback
import warnings
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Callable, Generic, List, Tuple, Type

Expand All @@ -16,7 +16,7 @@
import playwright.async_api
from playwright.async_api import Page, Route, TimeoutError, async_playwright
from pydantic import ValidationError
from tiktokapipy import TikTokAPIError
from tiktokapipy import TikTokAPIError, TikTokAPIWarning
from tiktokapipy.api import (
LightUserGetter,
TikTokAPI,
Expand Down Expand Up @@ -215,7 +215,11 @@ async def capture_api_extras(route: Route):
await page.close()
continue
except TimeoutError:
print("Reached navigation timeout. Retrying...", file=sys.stderr)
warnings.warn(
"Reached navigation timeout. Retrying...",
category=TikTokAPIWarning,
stacklevel=2,
)
await page.close()
continue
break
Expand Down

0 comments on commit 7f18280

Please sign in to comment.