diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 68bc17f..0000000 --- a/.gitignore +++ /dev/null @@ -1,160 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..88b20f4 --- /dev/null +++ b/main.py @@ -0,0 +1,108 @@ +import datetime + +import httpx +from sensitive_data import client_id, client_secret, username, password + +subreddit = 'learnpython' + + +def get_token(client_id: str, client_secret: str, username: str, password: str) -> str: + ''' получить токен ''' + post_data = {"grant_type": "password", "username": username, "password": password} + headers = {"User-Agent": "User-Agent"} + response = httpx.post("https://www.reddit.com/api/v1/access_token", + auth=(client_id, client_secret), data=post_data, headers=headers) + return response.json()['access_token'] + + +def get_latest_posts(token: str, subreddit: str) -> list: + ''' получить публикации субреддита за последние 3 дня ''' + latest_posts = [] + today = datetime.date.today() + + headers = { + "Authorization": f"bearer {token}", + "User-Agent": "User-Agent"} + + response = httpx.get(f"https://oauth.reddit.com/r/{subreddit}/top.json?t=week", headers=headers) + for post in response.json()['data']['children']: + created = int(post['data']['created']) + if datetime.date.fromtimestamp(created) > today - datetime.timedelta(days=3): + latest_posts.append(post) + return latest_posts + + +def get_authors(posts: list[dict]) -> list: + ''' получить авторов публикаций ''' + return [post['data']['author'] for post in posts] + + +def count_items_in_list(authors: list[str]) -> list[tuple[str, int]]: + ''' посчитать количество публикаций авторами ''' + posts_by_authors = {} + for author in authors: + if author not in posts_by_authors: + posts_by_authors[author] = 1 + else: + posts_by_authors[author] = posts_by_authors[author] + 1 + + return list(posts_by_authors.items()) + + +def sort_list_by_second_element(list_for_sort: list[tuple[str, int]]) -> list[tuple[str, int]]: + return sorted(list_for_sort, key=lambda item: item[1], reverse=True) + + +def get_first_items(temp_list: list[tuple[str, int]]) -> list[str]: + return [item[0] for item in temp_list] + + +def get_top_authors(posts: list[dict]) -> list[str]: + authors = get_authors(posts) + counted_posts_by_author = count_items_in_list(authors) + sorted_authors_by_count_posts = sort_list_by_second_element(counted_posts_by_author) + return get_first_items(sorted_authors_by_count_posts) + + +def get_commentators(data: dict, commentators: list[str]) -> None: + for comment_data in data['data']['children']: + author = comment_data['data']['author'] + commentators.append(author) + if comment_data['data']['replies']: + get_commentators(comment_data['data']['replies'], commentators) + + +def get_comments(token: str, post_id: str) -> dict: + headers = { + "Authorization": f"bearer {token}", + "User-Agent": "ChangeMeClient/0.1 by YourUsername"} + + response = httpx.get(f"https://oauth.reddit.com/r/{subreddit}/comments/{post_id}/comment.json", headers=headers) + return response.json()[1] + + +def get_top_commentators(token: str, posts: list) -> list: + commentators: list[str] = [] + + for post in posts: + post_id = post['data']['id'] + comments = get_comments(token, post_id) + get_commentators(comments, commentators) + + counted_comments_by_author = count_items_in_list(commentators) + sorted_authors_by_count_comments = sort_list_by_second_element(counted_comments_by_author) + return get_first_items(sorted_authors_by_count_comments) + + +def main(): + token = get_token(client_id, client_secret, username, password) + posts = get_latest_posts(token, subreddit) + top_authors = get_top_authors(posts) + top_commentators = get_top_commentators(token, posts) + + print(top_authors) + print(top_commentators) + + +if __name__ == '__main__': + main() diff --git a/tests/__pycache__/test_main.cpython-312-pytest-8.0.2.pyc b/tests/__pycache__/test_main.cpython-312-pytest-8.0.2.pyc new file mode 100644 index 0000000..c0b050e Binary files /dev/null and b/tests/__pycache__/test_main.cpython-312-pytest-8.0.2.pyc differ diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..0f326c1 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,101 @@ +import pytest +import httpx + +from sensitive_data import client_id, client_secret, username, password +from main import (get_token, get_latest_posts, get_authors, count_items_in_list, sort_list_by_second_element, + get_top_authors, get_first_items, get_comments) + + +@pytest.fixture +def token(): + return get_token(client_id, client_secret, username, password) + + +@pytest.fixture +def subreddit(): + return 'learnpython' + + +@pytest.fixture +def posts(token, subreddit): + return get_latest_posts(token, subreddit) + + +def test__get_token__returns_string(): + assert isinstance(get_token(client_id, client_secret, username, password), str) + + +def test__get_token__returns_exception_with_blank_client_id(): + with pytest.raises(KeyError): + get_token(client_id='', client_secret=client_secret, username=username, password=password) + + +def test__get_token__returns_exception_with_blank_client_secret(): + with pytest.raises(KeyError): + get_token(client_id=client_id, client_secret='', username=username, password=password) + + +def test__get_token__returns_exception_with_blank_username(): + with pytest.raises(KeyError): + get_token(client_id=client_id, client_secret=client_secret, username='', password=password) + + +def test__get_token__returns_exception_with_blank_password(): + with pytest.raises(KeyError): + get_token(client_id=client_id, client_secret=client_secret, username=username, password='') + + +def test__get_latest_posts__returns_list(token, subreddit): + assert isinstance(get_latest_posts(token, subreddit), list) + + +def test__get_latest_posts__returns_exception_with_blank_token(subreddit): + with pytest.raises(httpx.LocalProtocolError): + get_latest_posts(token='', subreddit=subreddit) + + +def test__get_latest_posts__returns_exception_with_blank_subreddit(token): + with pytest.raises(KeyError): + get_latest_posts(token=token, subreddit='') + + +def test__get_authors__returns_authors_list(): + data = [{'data': {'author': 'author1'}}, + {'data': {'author': 'author2'}}] + assert get_authors(data) == ['author1', 'author2'] + + +@pytest.mark.parametrize(('data', 'expected_result'), [ + ([{'data': {'author': 'author1'}}], ['author1']), + ([{'data': {'author': 'author1'}}, {'data': {'author': 'author2'}}], ['author1', 'author2']) +]) +def test__get_authors__returns_authors_list(data, expected_result): + assert get_authors(data) == expected_result + + +@pytest.mark.parametrize(('items', 'expected_result'), [ + (['author'], [('author', 1)]), + (['author', 'author'], [('author', 2)]), + (['author1', 'author2'], [('author1', 1), ('author2', 1)]), +]) +def test__count_items_in_list__returns_count_items(items, expected_result): + assert count_items_in_list(items) == expected_result + + +@pytest.mark.parametrize(('items', 'expected_result'), [ + ([], []), + ([('author', 1), ('author', 1)], [('author', 1), ('author', 1)]), + ([('author', 1), ('author', 2)], [('author', 2), ('author', 1)]), +]) +def test__sort_list_by_second_element__returns_sorted_items(items, expected_result): + assert sort_list_by_second_element(items) == expected_result + + +@pytest.mark.parametrize(('tuples_list', 'expected_result'), [ + ([], []), + ([('author1', 2), ('author2', 1)], ['author1', 'author2']), +]) +def test__get_first_items__returns_first_items(tuples_list, expected_result): + assert get_first_items(tuples_list) == expected_result + +