Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Python 3.11 baseline; increase blocklist import verbosity #63

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
run: curl -sSL https://install.python-poetry.org | python3 -
- uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: '3.11'
cache: 'poetry'
- name: Install Python dependencies
run: poetry install --with=dev
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__pycache__/*
**/__pycache__/
*.db
ttnconfig/*
*.egg-info
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

If you want to run your own instance of `trackthenews`, you can download and install the package, and run its built-in configuration process. It can be installed with `pip`:

Python 3.9 is what we currently test against, though it may work with other versions.
Python 3.11 is what we currently test against, though it may work with other versions.


```bash
Expand Down Expand Up @@ -60,16 +60,16 @@ All articles are recorded in a sqlite database.

### Advanced feature: blocklist

In some cases, you may wish to suppress articles from being posted, even though they would otherwise match. You can do so by writing two new functions, `check_article` and `check_paragraph`, and placing them in a file named `blocklist.py` in the configuration directory:
- `check_article` takes an Article (and so has access to its `outlet`, `title`, and `url`) and should return `True` for any article that should be skipped in its entirety.
- `check_paragraph` takes an Article and an individual matching paragraph and should return `True` for any paragraph that should be skipped (if other paragraphs match, the article will still be posted, but without the skipped paragraphs).
In some cases, you may wish to suppress articles or paragraphs from being posted, even though they would otherwise match. To do so, implement a CustomBlocklist class following the abstract base class template in `trackthenews/base_blocklist.py`, and drop it as a file named `blocklist.py` in your `ttnconfig` directory.

You can import the `bs4` library in `blocklist.py` for advanced parsing.

## Development

### Quick Start

```bash
poetry env use 3.9 # Necessary if you have a different default python version
poetry env use 3.11 # Necessary if you have a different default python version
poetry install
poetry run trackthenews sample_project
# Follow the setup script instructions
Expand All @@ -83,8 +83,8 @@ poetry run trackthenews sample_project
To develop `trackthenews`, clone the repository and install the package using [poetry][] and run the CLI tool:

```bash
# Ensure you're using Python 3.9
poetry env use 3.9
# Ensure you're using Python 3.11
poetry env use 3.11
# This will create a virtual environment and install trackthenews and its dependencies
poetry install --with=dev
# This will run the setup script
Expand Down
891 changes: 495 additions & 396 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repository = "https://github.com/freedomofpress/trackthenews"
include = ["trackthenews/fonts/*"]

[tool.poetry.dependencies]
python = "~3.9"
python = "~3.11"
feedparser = "^6.0"
future = "^0.17.1"
html2text = "2018.1.9"
Expand All @@ -31,6 +31,9 @@ readability-lxml = "^0.8.1"
requests = "^2.31.0"
tweepy = "^4.14.0"
"Mastodon.py" = "^1.8.1"
lxml = {extras = ["html-clean"], version = "^5.3.0"}
# For blocklist implementers
beautifulsoup4 = "^4.12.3"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ambivalent about this. If it's not being used by the core script, it feels like this should be codified in the specific implementation that's using it instead. Like, ideally, our internal ttnconfig repo that includes the blocklist should have a separate requirements spec that lists both ttn and bs4. (If someone else wanted to use PyQuery for example, they'd still have to install that themselves.)

(Looks like I actually removed this requirement in the past for similar reasons heh 64bcb02)

On the other hand, this is a small project that's only used by a handful of people besides us, so I'd accept if it feels like too much bikeshedding to debate this. Probably not a big deal to include.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about dropping an abstract base class in this repo that does import bs4? Basically, more of an opinionated plugin-style design pattern intended to minimize the complexity folks may have to wrangle to add something like a basic blocklist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds reasonable enough!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll poke at this still in this PR, dragging back to WIP until that's done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done; I've avoided importing BeautifulSoup in the ABC since it serves no purpose there, but I've annotated pyproject.toml to make it clearer why it's there.


[tool.poetry.group.dev.dependencies]
black = "^23.7.0"
Expand Down
15 changes: 15 additions & 0 deletions trackthenews/base_blocklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod


class BaseBlocklist(ABC):
"""Abstract base class for blocklists."""

@abstractmethod
def check_article(self, article):
"""Check if an entire article should be blocked."""
pass

@abstractmethod
def check_paragraph(self, article, paragraph):
"""Check if an otherwise matchign paragraph should be blocked based on its content."""
pass
31 changes: 21 additions & 10 deletions trackthenews/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ def clean(self, http_session):

self.plaintext = h.handle(doc.summary())

def check_for_matches(self, http_session):
def check_for_matches(self, http_session, blocklist=None):
"""
Clean up an article, check it against a block list, then for matches.
"""
self.clean(http_session)
plaintext_grafs = self.plaintext.split("\n")

if blocklist_loaded and blocklist.check_article(self):
if blocklist and blocklist.check_article(self):
pass
else:
for graf in plaintext_grafs:
if any(word.lower() in graf.lower() for word in matchwords) or any(
word in graf for word in matchwords_case_sensitive
):
if blocklist_loaded and blocklist.check_paragraph(self, graf):
if blocklist and blocklist.check_paragraph(self, graf):
continue
self.matching_grafs.append(graf)

Expand Down Expand Up @@ -604,14 +604,25 @@ def main():
sys.path.append(home)
global blocklist_loaded
global blocklist
try:
import blocklist as blocklist

blocklist_loaded = True
print("Loaded blocklist.")
except ImportError:
blocklist_path = os.path.join(home, "blocklist.py")

if os.path.exists(blocklist_path):
try:
from blocklist import CustomBlocklist

blocklist_instance = CustomBlocklist()
blocklist_loaded = True
print("Loaded blocklist.")
except ImportError as e:
blocklist_loaded = False
print(f"Error loading blocklist: {e}")
except Exception as e:
blocklist_loaded = False
print(f"Unexpected error loading blocklist: {e}")
else:
blocklist_loaded = False
print("No blocklist to load.")
print("No blocklist file found to load.")

if matchwords:
print("Matching against the following words: {}".format(matchwords))
Expand Down Expand Up @@ -660,7 +671,7 @@ def main():
print("Checking {} article {}/{}".format(article.outlet, counter, len(deduped)))

try:
article.check_for_matches(http_session)
article.check_for_matches(http_session, blocklist=blocklist_instance)
except Exception as e:
print(e)
print("Having trouble with that article. Skipping for now.")
Expand Down
Loading