Skip to content

Commit

Permalink
Merge pull request #2040 from MachciaD/vassert-except-assert
Browse files Browse the repository at this point in the history
Vassert except assert
  • Loading branch information
subzeroid authored Oct 5, 2024
2 parents d0434d0 + 596525a commit 5e17f8a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions instagrapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,7 @@ class InvalidMediaId(PrivateError):

class MediaUnavailable(PrivateError):
"""Media is unavailable"""


class ValidationError(AssertionError):
pass
4 changes: 2 additions & 2 deletions instagrapi/mixins/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from instagrapi.exceptions import HighlightNotFound
from instagrapi.extractors import extract_highlight_v1
from instagrapi.types import Highlight
from instagrapi.utils import dumps
from instagrapi.utils import dumps, vassert


class HighlightMixin:
Expand All @@ -31,7 +31,7 @@ def highlight_pk_from_url(self, url: str) -> str:
--------
https://www.instagram.com/stories/highlights/17895485201104054/ -> 17895485201104054
"""
assert "/highlights/" in url, 'URL must contain "/highlights/"'
vassert("/highlights/" in url, "URL must contain '/highlights/'")
path = urlparse(url).path
parts = [p for p in path.split("/") if p and p.isdigit()]
return str(parts[0])
Expand Down
6 changes: 6 additions & 0 deletions instagrapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import string
import time
import urllib
from exceptions import ValidationError


class InstagramIdCodec:
Expand Down Expand Up @@ -107,3 +108,8 @@ def date_time_original(localtime):
def random_delay(delay_range: list):
"""Trigger sleep of a random floating number in range min_sleep to max_sleep"""
return time.sleep(random.uniform(delay_range[0], delay_range[1]))


def vassert(pred, message):
if not pred:
raise ValidationError(message)

0 comments on commit 5e17f8a

Please sign in to comment.