diff --git a/snscrape/base.py b/snscrape/base.py index bac500c..6425dc7 100644 --- a/snscrape/base.py +++ b/snscrape/base.py @@ -14,8 +14,18 @@ import warnings logger = logging.getLogger(__name__) +def _module_deprecation_helper(all, **names): + def __getattr__(name): + if name in names: + warnings.warn(f'{name} is deprecated, use {names[name].__name__} instead', DeprecatedFeatureWarning, stacklevel = 2) + return names[name] + raise AttributeError(f'module {__name__!r} has no attribute {name!r}') + def __dir__(): + return sorted(all + list(names.keys())) + return __getattr__, __dir__ -class DeprecatedPropertyAccessWarning(FutureWarning): + +class DeprecatedFeatureWarning(FutureWarning): pass @@ -28,7 +38,7 @@ class _DeprecatedProperty: def __get__(self, obj, objType): if obj is None: # if the access is through the class using _DeprecatedProperty rather than an instance of the class: return self - warnings.warn(f'{self.name} is deprecated, use {self.replStr} instead', DeprecatedPropertyAccessWarning, stacklevel = 2) + warnings.warn(f'{self.name} is deprecated, use {self.replStr} instead', DeprecatedFeatureWarning, stacklevel = 2) return self.repl(obj) diff --git a/snscrape/modules/twitter.py b/snscrape/modules/twitter.py index 82ee1f2..5cfa4c5 100644 --- a/snscrape/modules/twitter.py +++ b/snscrape/modules/twitter.py @@ -38,17 +38,6 @@ import urllib3.util.ssl_ import warnings -# DescriptionURL deprecation -_DEPRECATED_NAMES = {'DescriptionURL': 'TextLink'} -def __getattr__(name): - if name in _DEPRECATED_NAMES: - warnings.warn(f'{name} is deprecated, use {_DEPRECATED_NAMES[name]} instead', FutureWarning, stacklevel = 2) - return globals()[_DEPRECATED_NAMES[name]] - raise AttributeError(f'module {__name__!r} has no attribute {name!r}') -def __dir__(): - return sorted(__all__ + list(_DEPRECATED_NAMES.keys())) - - _logger = logging.getLogger(__name__) _API_AUTHORIZATION_HEADER = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA' _globalGuestTokenManager = None @@ -2082,3 +2071,6 @@ class TwitterTrendsScraper(_TwitterAPIScraper): for item in entry['content']['timelineModule']['items']: trend = item['item']['content']['trend'] yield Trend(name = trend['name'], metaDescription = trend['trendMetadata'].get('metaDescription'), domainContext = trend['trendMetadata']['domainContext']) + + +__getattr__, __dir__ = snscrape.base._module_deprecation_helper(__all__, DescriptionURL = TextLink)