From eb3dc3fe5e14c5b8163173cd5b80b6d1c396725c Mon Sep 17 00:00:00 2001 From: buckbanzai Date: Thu, 13 Apr 2023 15:45:45 +0000 Subject: [PATCH 01/24] Initial commit --- .gitignore | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 9 +++ README.md | 2 + 3 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d381cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,162 @@ +# ---> Python +# 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/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b27183d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# kuow-mastodon-bot + From 21977293caed33a03d1894c735bce1a7e1712600 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 08:49:05 -0700 Subject: [PATCH 02/24] Initial version --- kuow_fetcher.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 2 files changed, 36 insertions(+) create mode 100644 kuow_fetcher.py create mode 100644 requirements.txt diff --git a/kuow_fetcher.py b/kuow_fetcher.py new file mode 100644 index 0000000..2aacd17 --- /dev/null +++ b/kuow_fetcher.py @@ -0,0 +1,33 @@ +import requests +from bs4 import BeautifulSoup +from mastodon import Mastodon +kuow_base_url="https://www.kuow.org" +url=kuow_base_url+"/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" +log_file = "kuow_bot_logfile.txt" +mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') + +r = requests.get(url) +soup = BeautifulSoup(r.content, 'html.parser') +for article in soup.find_all("span", class_ = "txt"): + article_link = article.find("a").attrs["href"] + + is_new_article = True + with open(log_file, 'r') as fp: + lines = fp.readlines() + for row in lines: + if row == article_link+"\n": + print("Article "+article_link+" has already been seen") + is_new_article = False + + if is_new_article: + print(article_link+" has not been seen, posting") + with open(log_file, "a") as fp: + fp.write(article_link+"\n") + article_lookup = requests.get(kuow_base_url+article_link) + article_soup = BeautifulSoup(article_lookup.content, 'html.parser') + try: + article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() + mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + except: + print("Could not load a description/post this article") + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4029575 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +beautifulsoup4==4.12.2 +Mastodon.py==1.8.0 +requests==2.28.2 \ No newline at end of file From 69eeefe2277a1c80d6d19910a47adb65504f7743 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 09:03:51 -0700 Subject: [PATCH 03/24] Update README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b27183d..5f15483 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# kuow-mastodon-bot +# KUOW Mastodon Bot +This bot scrapes the KUOW website, looks for links to news stories that haven't already been seen/posted, then posts them to Mastodon. +PRs welcome! \ No newline at end of file From cc2d7d54c22ecf20dd081b86aad9db2617460833 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 09:12:01 -0700 Subject: [PATCH 04/24] Only log article_link if post was successful --- kuow_fetcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 2aacd17..05a558b 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -21,13 +21,13 @@ for article in soup.find_all("span", class_ = "txt"): if is_new_article: print(article_link+" has not been seen, posting") - with open(log_file, "a") as fp: - fp.write(article_link+"\n") article_lookup = requests.get(kuow_base_url+article_link) article_soup = BeautifulSoup(article_lookup.content, 'html.parser') try: article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + with open(log_file, "a") as fp: + fp.write(article_link+"\n") except: print("Could not load a description/post this article") \ No newline at end of file From baeb1e85ffed0c5e67f50bbf903952479083c56a Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 11:22:50 -0700 Subject: [PATCH 05/24] Reverse order of article processing --- kuow_fetcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 05a558b..25618ec 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,7 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt"): +for article in soup.find_all("span", class_ = "txt").reverse(): article_link = article.find("a").attrs["href"] is_new_article = True From 73aae6ef1489623a977675ef0a2abd02f64fd850 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 11:26:11 -0700 Subject: [PATCH 06/24] Reverting reversal, doesn't work --- kuow_fetcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 25618ec..05a558b 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,7 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt").reverse(): +for article in soup.find_all("span", class_ = "txt"): article_link = article.find("a").attrs["href"] is_new_article = True From 089af0de590eb8a679c9d9ea7714ebfd7baec274 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 11:58:41 -0700 Subject: [PATCH 07/24] Reverse articles before processing --- kuow_fetcher.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 05a558b..940a982 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,11 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt"): +articles = soup.find_all("span", class_ = "txt") +# Reverse articles, so that if multiple new ones have been found, they'll be posted in order of when published +articles.reverse() + +for article in articles: article_link = article.find("a").attrs["href"] is_new_article = True From 08f814f20dbe85ce06b21a9e8d95492f5eba19d2 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 12:00:03 -0700 Subject: [PATCH 08/24] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5d381cc..3f82210 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +.vscode/ \ No newline at end of file From 8a018da763b23602c0fe60d663a2c7130e27eb77 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Thu, 13 Apr 2023 12:00:29 -0700 Subject: [PATCH 09/24] Fix formatting --- kuow_fetcher.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 940a982..0c329cf 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -1,14 +1,18 @@ import requests from bs4 import BeautifulSoup from mastodon import Mastodon -kuow_base_url="https://www.kuow.org" -url=kuow_base_url+"/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" + +kuow_base_url = "https://www.kuow.org" +url = ( + kuow_base_url + + "/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" +) log_file = "kuow_bot_logfile.txt" -mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') +mastodon = Mastodon(access_token="kuow_bot_mastodon.secret") r = requests.get(url) -soup = BeautifulSoup(r.content, 'html.parser') -articles = soup.find_all("span", class_ = "txt") +soup = BeautifulSoup(r.content, "html.parser") +articles = soup.find_all("span", class_="txt") # Reverse articles, so that if multiple new ones have been found, they'll be posted in order of when published articles.reverse() @@ -16,22 +20,32 @@ for article in articles: article_link = article.find("a").attrs["href"] is_new_article = True - with open(log_file, 'r') as fp: + with open(log_file, "r") as fp: lines = fp.readlines() for row in lines: - if row == article_link+"\n": - print("Article "+article_link+" has already been seen") + if row == article_link + "\n": + print("Article " + article_link + " has already been seen") is_new_article = False if is_new_article: - print(article_link+" has not been seen, posting") - article_lookup = requests.get(kuow_base_url+article_link) - article_soup = BeautifulSoup(article_lookup.content, 'html.parser') + print(article_link + " has not been seen, posting") + article_lookup = requests.get(kuow_base_url + article_link) + article_soup = BeautifulSoup(article_lookup.content, "html.parser") try: - article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() - mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + article_description = ( + (article_soup.find("meta", attrs={"property": "description"})) + .attrs["content"] + .strip() + ) + mastodon.status_post( + status=article_description + + "\n" + + kuow_base_url + + article_link + + "\n#KUOW #News", + visibility="unlisted", + ) with open(log_file, "a") as fp: - fp.write(article_link+"\n") + fp.write(article_link + "\n") except: print("Could not load a description/post this article") - \ No newline at end of file From 42bcf5a16aa044dacb50e7235bc445312d7189f9 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 19 Apr 2023 01:38:06 +0000 Subject: [PATCH 10/24] Add year and name to license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 2071b23..5baee6c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2023 Liam Steckler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 631d366889ccf1c96a1655b72321b327db7f95d8 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 11/24] Initial commit --- .gitignore | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++ LICENSE | 9 +++ README.md | 2 + 3 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d381cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,162 @@ +# ---> Python +# 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/LICENSE b/LICENSE new file mode 100644 index 0000000..2071b23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b27183d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# kuow-mastodon-bot + From 3d613e30754adb117fcd296de52dcaa82c7b872f Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 12/24] Initial version --- kuow_fetcher.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 2 files changed, 36 insertions(+) create mode 100644 kuow_fetcher.py create mode 100644 requirements.txt diff --git a/kuow_fetcher.py b/kuow_fetcher.py new file mode 100644 index 0000000..2aacd17 --- /dev/null +++ b/kuow_fetcher.py @@ -0,0 +1,33 @@ +import requests +from bs4 import BeautifulSoup +from mastodon import Mastodon +kuow_base_url="https://www.kuow.org" +url=kuow_base_url+"/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" +log_file = "kuow_bot_logfile.txt" +mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') + +r = requests.get(url) +soup = BeautifulSoup(r.content, 'html.parser') +for article in soup.find_all("span", class_ = "txt"): + article_link = article.find("a").attrs["href"] + + is_new_article = True + with open(log_file, 'r') as fp: + lines = fp.readlines() + for row in lines: + if row == article_link+"\n": + print("Article "+article_link+" has already been seen") + is_new_article = False + + if is_new_article: + print(article_link+" has not been seen, posting") + with open(log_file, "a") as fp: + fp.write(article_link+"\n") + article_lookup = requests.get(kuow_base_url+article_link) + article_soup = BeautifulSoup(article_lookup.content, 'html.parser') + try: + article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() + mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + except: + print("Could not load a description/post this article") + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4029575 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +beautifulsoup4==4.12.2 +Mastodon.py==1.8.0 +requests==2.28.2 \ No newline at end of file From 73cd62b70760a7613466f8c801c9dfff7172ae07 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 13/24] Update README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b27183d..5f15483 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# kuow-mastodon-bot +# KUOW Mastodon Bot +This bot scrapes the KUOW website, looks for links to news stories that haven't already been seen/posted, then posts them to Mastodon. +PRs welcome! \ No newline at end of file From ab6e68588e2e8e05cff31a6a2a983ab71e367d78 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 14/24] Only log article_link if post was successful --- kuow_fetcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 2aacd17..05a558b 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -21,13 +21,13 @@ for article in soup.find_all("span", class_ = "txt"): if is_new_article: print(article_link+" has not been seen, posting") - with open(log_file, "a") as fp: - fp.write(article_link+"\n") article_lookup = requests.get(kuow_base_url+article_link) article_soup = BeautifulSoup(article_lookup.content, 'html.parser') try: article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + with open(log_file, "a") as fp: + fp.write(article_link+"\n") except: print("Could not load a description/post this article") \ No newline at end of file From 46818f830ca4fdca7c78c39fb30a4a8b17067e37 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 15/24] Reverse order of article processing --- kuow_fetcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 05a558b..25618ec 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,7 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt"): +for article in soup.find_all("span", class_ = "txt").reverse(): article_link = article.find("a").attrs["href"] is_new_article = True From 9cbb416868f3628a109b8f06c1bfcca9fb23bb70 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 16/24] Reverting reversal, doesn't work --- kuow_fetcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 25618ec..05a558b 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,7 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt").reverse(): +for article in soup.find_all("span", class_ = "txt"): article_link = article.find("a").attrs["href"] is_new_article = True From 048dcb0377d44daefae89fa38d56020edb8fd223 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 17/24] Reverse articles before processing --- kuow_fetcher.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 05a558b..940a982 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -8,7 +8,11 @@ mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') -for article in soup.find_all("span", class_ = "txt"): +articles = soup.find_all("span", class_ = "txt") +# Reverse articles, so that if multiple new ones have been found, they'll be posted in order of when published +articles.reverse() + +for article in articles: article_link = article.find("a").attrs["href"] is_new_article = True From abd9ff4630421e60769f03ae70dd19963881d820 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 18/24] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5d381cc..3f82210 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +.vscode/ \ No newline at end of file From bb1aa387219b4e9ccd12a497a6f5b25f9514c23e Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 19/24] Fix formatting --- kuow_fetcher.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 940a982..0c329cf 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -1,14 +1,18 @@ import requests from bs4 import BeautifulSoup from mastodon import Mastodon -kuow_base_url="https://www.kuow.org" -url=kuow_base_url+"/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" + +kuow_base_url = "https://www.kuow.org" +url = ( + kuow_base_url + + "/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" +) log_file = "kuow_bot_logfile.txt" -mastodon = Mastodon(access_token = 'kuow_bot_mastodon.secret') +mastodon = Mastodon(access_token="kuow_bot_mastodon.secret") r = requests.get(url) -soup = BeautifulSoup(r.content, 'html.parser') -articles = soup.find_all("span", class_ = "txt") +soup = BeautifulSoup(r.content, "html.parser") +articles = soup.find_all("span", class_="txt") # Reverse articles, so that if multiple new ones have been found, they'll be posted in order of when published articles.reverse() @@ -16,22 +20,32 @@ for article in articles: article_link = article.find("a").attrs["href"] is_new_article = True - with open(log_file, 'r') as fp: + with open(log_file, "r") as fp: lines = fp.readlines() for row in lines: - if row == article_link+"\n": - print("Article "+article_link+" has already been seen") + if row == article_link + "\n": + print("Article " + article_link + " has already been seen") is_new_article = False if is_new_article: - print(article_link+" has not been seen, posting") - article_lookup = requests.get(kuow_base_url+article_link) - article_soup = BeautifulSoup(article_lookup.content, 'html.parser') + print(article_link + " has not been seen, posting") + article_lookup = requests.get(kuow_base_url + article_link) + article_soup = BeautifulSoup(article_lookup.content, "html.parser") try: - article_description = (article_soup.find("meta", attrs={"property": "description"})).attrs["content"].strip() - mastodon.status_post(status=article_description+"\n"+kuow_base_url+article_link+"\n#KUOW #News", visibility="unlisted") + article_description = ( + (article_soup.find("meta", attrs={"property": "description"})) + .attrs["content"] + .strip() + ) + mastodon.status_post( + status=article_description + + "\n" + + kuow_base_url + + article_link + + "\n#KUOW #News", + visibility="unlisted", + ) with open(log_file, "a") as fp: - fp.write(article_link+"\n") + fp.write(article_link + "\n") except: print("Could not load a description/post this article") - \ No newline at end of file From a4ee9abc73cb393d7a26bfe9cde940d5129c4137 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:48:09 -0800 Subject: [PATCH 20/24] Add year and name to license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 2071b23..5baee6c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2023 Liam Steckler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 8d6d953d031a32b62d28328fafd52e0cd8612566 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 14:50:15 -0800 Subject: [PATCH 21/24] Update requirements --- requirements.txt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 4029575..f406dab 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,13 @@ beautifulsoup4==4.12.2 +blurhash==1.1.4 +certifi==2023.11.17 +charset-normalizer==3.3.2 +decorator==5.1.1 +idna==3.6 Mastodon.py==1.8.0 -requests==2.28.2 \ No newline at end of file +python-dateutil==2.8.2 +python-magic==0.4.27 +requests==2.28.2 +six==1.16.0 +soupsieve==2.5 +urllib3==1.26.18 From 3c1dbf0b694bac967d6934cf543765efb013bc76 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 15:02:48 -0800 Subject: [PATCH 22/24] Update year --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 5baee6c..f795c23 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Liam Steckler +Copyright (c) 2024 Liam Steckler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From fde9904d8a795c32aa9bcd3922bcf853a2956cf2 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 15:12:01 -0800 Subject: [PATCH 23/24] Add sample config --- config-sample.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 config-sample.yml diff --git a/config-sample.yml b/config-sample.yml new file mode 100644 index 0000000..61137b9 --- /dev/null +++ b/config-sample.yml @@ -0,0 +1,5 @@ +mastodon: + client_id: + client_secret: + access_token: + api_base_url: From f901c89a64f11c4902d6649b0011e23594150309 Mon Sep 17 00:00:00 2001 From: Liam Steckler Date: Sun, 14 Jan 2024 15:12:33 -0800 Subject: [PATCH 24/24] Switch to yaml config --- kuow_fetcher.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/kuow_fetcher.py b/kuow_fetcher.py index 0c329cf..293cf9c 100644 --- a/kuow_fetcher.py +++ b/kuow_fetcher.py @@ -1,4 +1,5 @@ import requests +import yaml from bs4 import BeautifulSoup from mastodon import Mastodon @@ -8,10 +9,18 @@ url = ( + "/fragments?name=story_grid&source=homepage&view_id=1&page=1&per_page=12" ) log_file = "kuow_bot_logfile.txt" -mastodon = Mastodon(access_token="kuow_bot_mastodon.secret") +config = yaml.safe_load(open("config.yml")) +stadiamaps_api_key = config["stadiamaps"]["api_key"] +nominatim_url = config["nominatim"]["api_base_url"] +mastodon = Mastodon( + client_id=config["mastodon"]["client_id"], + client_secret=config["mastodon"]["client_secret"], + access_token=config["mastodon"]["access_token"], + api_base_url=config["mastodon"]["api_base_url"], +) -r = requests.get(url) -soup = BeautifulSoup(r.content, "html.parser") +kuow_response = requests.get(url) +soup = BeautifulSoup(kuow_response.content, "html.parser") articles = soup.find_all("span", class_="txt") # Reverse articles, so that if multiple new ones have been found, they'll be posted in order of when published articles.reverse()