Skip to content

Commit feccac6

Browse files
committed
we do not need no stinkin redis
1 parent 549088d commit feccac6

File tree

4 files changed

+26
-54
lines changed

4 files changed

+26
-54
lines changed

app/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from typing import Any
1313
from services.link_tracker import link_tracker
1414
from utils import copy_default_to_configs
15-
from models.utils import pwd
1615

1716
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.WARN)
1817

app/services/favicon_store.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22
import re
33
import logging
4-
from services.redis_store import RedisStore
5-
from services.favicon_utils import base, download_favicon, get_favicon_filename, normalize_domain
4+
from services.favicon_utils import base, download_favicon, favicon_failed_filename, favicon_filename
65
from models.scheduler import Scheduler
76
from models.utils import pwd
87

@@ -16,7 +15,6 @@ def __init__(self, icon_dir='static/assets/icons'):
1615
self.icon_dir = pwd.joinpath(icon_dir).resolve()
1716
self.icon_dir.mkdir(parents=True, exist_ok=True)
1817

19-
self.redis_store = RedisStore()
2018
self.ip_pattern = re.compile(
2119
r"^(?:(?:https?://)?(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
2220
r"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?::\d{1,5})?(?:\/)?$"
@@ -30,14 +28,18 @@ def icon_path(self, url) -> str:
3028
if not url:
3129
return None
3230

33-
favicon_filename = get_favicon_filename(url)
34-
favicon_relative_path = f"{self.relative_icon_dir}/{favicon_filename}"
31+
filename = favicon_filename(url)
32+
favicon_relative_path = f"{self.relative_icon_dir}/{filename}"
3533

3634
if pwd.joinpath(favicon_relative_path).exists():
3735
return f"/{favicon_relative_path}"
3836
else:
3937
return None
4038

39+
def favicon_failed(self, url) -> bool:
40+
favicon_filename = favicon_failed_filename(url)
41+
return pwd.joinpath(self.relative_icon_dir, favicon_filename).exists()
42+
4143
def fetch_favicons_from(self, urls):
4244
base_urls = sorted(set(map(lambda url: base(url), urls)))
4345
processable_urls = set(filter(lambda url: self.should_processed(url), base_urls))
@@ -71,5 +73,5 @@ def should_processed(self, url):
7173
not url
7274
or bool(self.ip_pattern.match(url))
7375
or self.icon_path(url)
74-
or self.redis_store.is_domain_processed(normalize_domain(url))
76+
or self.favicon_failed(url)
7577
)

app/services/favicon_utils.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import os
33
import requests
44
from bs4 import BeautifulSoup
5-
from services.redis_store import RedisStore
65
from urllib.parse import urljoin, urlparse
6+
from models.utils import pwd
77

88
logger = logging.getLogger(__name__)
99
logger.setLevel(logging.INFO)
@@ -25,19 +25,23 @@ def base(url):
2525
return url
2626

2727

28-
def get_favicon_filename(url):
28+
def favicon_filename(url):
2929
return f"{normalize_domain(url)}.favicon.ico"
3030

3131

32+
def favicon_failed_filename(url):
33+
return f"{normalize_domain(url)}.failed"
34+
35+
3236
def make_request(url):
3337
request_headers = {
3438
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
3539
}
3640
return requests.get(url, headers=request_headers, allow_redirects=True, timeout=5)
3741

3842

39-
def favicon_path(icon_path, url):
40-
favicon_filename = get_favicon_filename(url)
43+
def favicon_path(icon_path, favicon_filename):
44+
favicon_filename = favicon_filename(url)
4145
return os.path.join(icon_path, favicon_filename)
4246

4347

@@ -66,22 +70,22 @@ def download_favicon(url, icon_dir):
6670

6771

6872
def _download(url, icon_dir, icon_url):
69-
redis_store = RedisStore()
7073
try:
7174
response = make_request(icon_url)
72-
7375
if response.status_code == 200 and response.headers.get('content-type', '').lower().startswith('image/'):
74-
filename = favicon_path(icon_dir, url)
75-
with open(favicon_path(icon_dir, url), 'wb') as file:
76+
filename = pwd.joinpath(icon_dir, favicon_filename(url))
77+
with open(filename, 'wb') as file:
7678
file.write(response.content)
7779
logger.debug(f"saving {url} as {filename}")
7880
else:
79-
redis_store.save_processed_domain(
80-
normalize_domain(url),
81-
reason=f'response_code: {response.status_code} content-type: {response.headers.get("content-type", "")}'
82-
)
83-
logger.debug(f"issues {url} complete")
81+
filename = pwd.joinpath(icon_dir, favicon_failed_filename(url))
82+
with open(filename, 'wb') as file:
83+
file.write(f'response_code: {response.status_code} content-type: {response.headers.get("content-type", "")}')
84+
logger.debug(f"Marking {url} as failed with {filename}")
8485
except Exception as ex:
85-
redis_store.save_processed_domain(normalize_domain(url), reason=f'{ex}')
86+
filename = pwd.joinpath(icon_dir, favicon_failed_filename(url))
87+
with open(filename, 'wb') as file:
88+
file.write(f'Error: {ex}')
89+
logger.debug(f"Marking {url} as failed with {filename}")
8690

8791
logger.debug(f"_download({icon_url}) completed")

app/services/redis_store.py

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

0 commit comments

Comments
 (0)