Skip to content

Commit

Permalink
Bump Version to 2.0.0 (#53, #56, #61)
Browse files Browse the repository at this point in the history
Move configurations to TweeterPy constructor\nAdd Proxy support\nOption to modify log_level
  • Loading branch information
iSarabjitDhiman committed Sep 26, 2024
1 parent add6349 commit e475813
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 264 deletions.
22 changes: 3 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ OR

```python
from twitter import TweeterPy

TweeterPy()
# proxy = {'http': 'proxy_here', 'https': 'proxy_here'}
proxy = None
TweeterPy(proxies=proxy, log_level="INFO")
```

> ### Example - Get User ID of a User.
Expand All @@ -53,23 +54,6 @@ Check out step by step guide.

[Documentation](docs/docs.md)

## Configuration

> ### Example - Config Usage
```python
from tweeterpy import config

config.PROXY = {"http":"127.0.0.1","https":"127.0.0.1"}
config.TIMEOUT = 10
config.UPDATE_API = False

```

Check out configuration docs for the available settings.

[Configurations](docs/config.md)

## Features

- Extracts Tweets
Expand Down
78 changes: 0 additions & 78 deletions docs/config.md

This file was deleted.

7 changes: 3 additions & 4 deletions quickstart.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from tweeterpy import TweeterPy
from tweeterpy import config
from tweeterpy.util import find_nested_key


def main():
# config.TIMEOUT = 5
# config.PROXY = {'http': 'proxy_here', 'https': 'proxy_here'}
twitter = TweeterPy()
# proxy = {'http': 'proxy_here', 'https': 'proxy_here'}
proxy = None
twitter = TweeterPy(proxies=proxy, log_level="INFO")
print(twitter.get_user_id('elonmusk'))
print(twitter.get_user_info('elonmusk'))
# print(twitter.get_user_data('elonmusk'))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = "1.2.4"
VERSION = "2.0.0"
SHORT_DESCRIPTION = "TweeterPy is a python library to extract data from Twitter. TweeterPy API lets you scrape data from a user's profile like username, userid, bio, followers/followings list, profile media, tweets, etc."

with open("requirements.txt") as file:
Expand Down
5 changes: 2 additions & 3 deletions tweeterpy/api_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import tempfile
import demjson3
import logging.config
from tweeterpy import config
from tweeterpy.request_util import RequestClient
from tweeterpy.constants import Path, FeatureSwitch, API_TMP_FILE
from tweeterpy.constants import Path, FeatureSwitch, API_TMP_FILE, LOGGING_CONFIG

logging.config.dictConfig(config.LOGGING_CONFIG)
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger(__name__)

dataset_regex = re.compile(r'''exports\s*=\s*{(.*?)},''', re.VERBOSE)
Expand Down
76 changes: 0 additions & 76 deletions tweeterpy/config.py

This file was deleted.

53 changes: 53 additions & 0 deletions tweeterpy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,62 @@

PUBLIC_TOKEN = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'

USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'

# Filename to store api data/endpoints as a backup.
API_TMP_FILE = "tweeterpy_api.json"

# Directory path/name to save and load logged in sessions/cookies. Default path is current directory. i.e. current_path/Twitter Saved Sessions
DEFAULT_SESSION_DIRECTORY = "Twitter Saved Sessions"

# File name to save logs.
LOG_FILE_NAME = "tweeterpy.log"

# Logging level : "DEBUG","INFO","WARNING","ERROR","CRITICAL"
# If None, "INFO" will be used for Stream/Console logs and "DEBUG" will be used for file logs.
# LOG_LEVEL = "INFO"
LOG_LEVEL = "INFO"

# Log Configuration.
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] [Line No. %(lineno)d] %(name)s : %(funcName)s :: %(message)s'
},
'custom': {
# 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
'class': 'tweeterpy.logging_util.CustomFormatter',
}
},
'handlers': {
'stream': {
'level': LOG_LEVEL,
'formatter': 'custom',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout'
},
'file': {
'level': 'DEBUG',
'formatter': 'standard',
'class': 'logging.FileHandler',
'filename': LOG_FILE_NAME,
"encoding": "utf-8"
}
},
'loggers': {
'': { # root logger
'handlers': ['stream', 'file'],
'level': 'DEBUG'
},
'__main__': { # if __name__ == '__main__'
'handlers': ['stream', 'file'],
'level': 'DEBUG',
}
}
}


class Color:
BLACK = "\033[0;30m"
Expand Down
8 changes: 4 additions & 4 deletions tweeterpy/logging_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from tweeterpy import config
from tweeterpy.constants import Color


Expand All @@ -23,6 +22,8 @@ def format(self, record):


def set_log_level(log_level=None, return_loggers=False, external_only=False):
if log_level and log_level not in logging._levelToName and log_level not in logging._levelToName.values():
raise Exception("Invalid Log Level")
if log_level is None:
log_level = logging.ERROR
all_loggers = {}
Expand All @@ -44,9 +45,8 @@ def wrapper(*args, **kwargs):
except Exception as error:
raise error
finally:
if not config.DISABLE_LOGS:
[logging.getLogger(current_logger).setLevel(all_loggers.get(
current_logger)) for current_logger in logging.root.manager.loggerDict.keys() if current_logger in list(all_loggers.keys())]
[logging.getLogger(current_logger).setLevel(all_loggers.get(current_logger))
for current_logger in logging.root.manager.loggerDict.keys() if current_logger in list(all_loggers.keys())]
return returned_output
return wrapper

Expand Down
Loading

0 comments on commit e475813

Please sign in to comment.