Skip to content

Commit 4677bd7

Browse files
committed
modules: Made text_replacements and dynamic_tags optional
1 parent 421b2ca commit 4677bd7

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
config.json
22
tests.py
33
test.sh
4+
dynamic_tags.py
5+
text_replacements.py
46

57
*.secret
68
*.db

dynamic_tags.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

main.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import urllib.request
1414
from bs4 import BeautifulSoup
1515

16-
import text_replacements
17-
import dynamic_tags
18-
1916
from fake_useragent import UserAgent
2017

2118
from config import (
@@ -27,9 +24,12 @@
2724
MAXIMUM_TOOTS_COUNT,
2825
)
2926

30-
from utils import determine_content_language
27+
from utils import determine_content_language, try_import
3128
from database import init_db, get_last_entry_posted, save_posted_entry
3229

30+
dynamic_tags = try_import("dynamic_tags.py", "dynamic_tags")
31+
text_replacements = try_import("text_replacements.py", "text_replacements")
32+
3333
def parse_args():
3434
parser = argparse.ArgumentParser(description="Mastodon RSS Bot")
3535
parser.add_argument("--rss", help="RSS feed URL")
@@ -161,7 +161,11 @@ def parse_args():
161161
raise ValueError('The title is missing')
162162

163163
toot_language = determine_content_language(feed_entry_title)
164-
toot_body = text_replacements.apply(feed_entry_title, toot_language)
164+
165+
if text_replacements:
166+
toot_body = text_replacements.apply(feed_entry_title, toot_language)
167+
else:
168+
toot_body = feed_entry_title
165169

166170
media_urls = []
167171
media_urls_posted = []
@@ -248,11 +252,8 @@ def parse_args():
248252
if INCLUDE_AUTHOR and 'authors' in feed_entry:
249253
toot_body += '\nby ' + feed_entry.authors[0].name
250254

251-
all_tags_to_add = ''
252-
dynamic_tags_to_add = dynamic_tags.get(toot_body, toot_language)
253-
254-
if tags_to_add: all_tags_to_add += ' ' + tags_to_add
255-
if dynamic_tags_to_add: all_tags_to_add += ' ' + dynamic_tags_to_add
255+
all_tags_to_add = tags_to_add
256+
if dynamic_tags: all_tags_to_add += ' ' + dynamic_tags.get(toot_body, toot_language)
256257

257258
if all_tags_to_add != '':
258259
filtered_tags_to_add = ''

text_replacements.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import importlib.util
2+
import os
3+
14
def determine_content_language(text):
25
language = 'en'
36

@@ -24,4 +27,15 @@ def determine_content_language(text):
2427
'interzis' in text):
2528
language = 'ro'
2629

27-
return language
30+
return language
31+
32+
33+
def try_import(filepath, module_name):
34+
if not os.path.exists(filepath):
35+
return None
36+
37+
spec = importlib.util.spec_from_file_location(module_name, filepath)
38+
module = importlib.util.module_from_spec(spec)
39+
spec.loader.exec_module(module)
40+
41+
return module

0 commit comments

Comments
 (0)