From f458d39c4da08b9ed5a6c1c4d61a97204a6c5326 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 9 Mar 2024 23:22:13 +0300 Subject: [PATCH 1/3] first commit --- main.py | 108 ++++++++++++++++++ sensitive_data.py | 4 + .../test_main.cpython-312-pytest-8.0.2.pyc | Bin 0 -> 12994 bytes tests/test_main.py | 101 ++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 main.py create mode 100644 sensitive_data.py create mode 100644 tests/__pycache__/test_main.cpython-312-pytest-8.0.2.pyc create mode 100644 tests/test_main.py 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/sensitive_data.py b/sensitive_data.py new file mode 100644 index 0000000..181ca1a --- /dev/null +++ b/sensitive_data.py @@ -0,0 +1,4 @@ +username = 'jeff_python' +password = 'paS*sword' +client_id = 'eBk7lmHkPDqjCHPXNa5hfw' +client_secret = 'RUQja4ziKe3Od7eSCp9yiSVoGgzWdg' \ No newline at end of file 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 0000000000000000000000000000000000000000..c0b050e592a936d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JTDFe6^Rr+~$T)raO)=&+rkpethe&;0~ImrH5x+oku5~G(pF~J7OGuui6Qer?M z(FVq`?TDR`1M|pGv~bouGsrFAGf;cEZmCh`#&9;wM{vVuZB#(vrFnJ>UT@_SO!Jw> zN>(w*UIfB^8@S(pbC8{Kn{BJu%btT89o0KGjYa*r(>wFU7k8dnbSk)c{1FVqI8Ofs zfGKjPwsCso{jj-dw^_4i$<2I#*6f`+^`+P|_s#!qh&|l&#ROX(^^od=H*y*#3iYwH z!E4#~fo1j*g0ldUMUw?6A$W#x&o7K~{Zdhn2nEm}j{blc<_X_EtfWCG*C2e9PlK!_ z#_&#Ih4Z>j8f0xT#)=$ydSOi(MD)qAg5L8ILZr?)ao+>}-4OQ`m4PMk?*;g(KsDg`{|Y?>2S*J6 zI9Ca(jiY8@h(A;hgus@6s2)5;Qb{>|3+jQrhI@AhJT-^aDy!S_|N4Dg6(}%1FJTDVP?j z^u4bo;-|d!#|W4=wc{=BgmHBY{}MnLe_Z%gsTCSZjl*|An)lLze-X<+58_{X@XrqT z8*z3Vhn_}&E)v7^DUYOZN_NqTLK+A@zlP6?E^FM8VyJ}q6DfHyzbHSQ$*E&$?I^nh zT`i`!xoR^6p|8(1%+rEaTXXrwJ z2pi@(x}>GWZD?6i-{IDG+?6SPnckHJnxuCl0#!IvT}bJ1-r9$a-FG7a+B;u^lefOv zI!Bv7qs?YZ_nlDfv^*0t>vx#Jc2j!tYRlEADK4$P8ob{8yMD9tS##%ergZw#nEA?U z<{8BlU!Ru(y}|+s2!XGGna+hUVDpICJo_d(cqdpl6`$Taz1;2|V&YKiC>aF=OUo)=$Fh4Z^ E275x7#sB~S literal 0 HcmV?d00001 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 + + From 206e42d76cb8225fe4c6a5d844366673f17aae43 Mon Sep 17 00:00:00 2001 From: Jeffpython <69818613+Jeffpython@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:23:39 +0300 Subject: [PATCH 2/3] Delete .gitignore --- .gitignore | 160 ----------------------------------------------------- 1 file changed, 160 deletions(-) delete mode 100644 .gitignore 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/ From 0001a9a42f246c31a0e2fb6898beaefe673a9d6a Mon Sep 17 00:00:00 2001 From: Jeffpython <69818613+Jeffpython@users.noreply.github.com> Date: Sat, 9 Mar 2024 23:25:01 +0300 Subject: [PATCH 3/3] Delete sensitive_data.py --- sensitive_data.py | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 sensitive_data.py diff --git a/sensitive_data.py b/sensitive_data.py deleted file mode 100644 index 181ca1a..0000000 --- a/sensitive_data.py +++ /dev/null @@ -1,4 +0,0 @@ -username = 'jeff_python' -password = 'paS*sword' -client_id = 'eBk7lmHkPDqjCHPXNa5hfw' -client_secret = 'RUQja4ziKe3Od7eSCp9yiSVoGgzWdg' \ No newline at end of file