urlicon 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: urlicon
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: `URLicon` helps you to discover an possible icon from a URL.
5
5
  Author-email: Cesar Cardoso <hello@cesarcardoso.cc>
6
6
  License-Expression: MIT
@@ -10,7 +10,7 @@ Requires-Dist: bs4>=0.0.2
10
10
  Requires-Dist: dotenv>=0.9.9
11
11
  Requires-Dist: requests>=2.32.5
12
12
 
13
- # URLicon - v0.1.0
13
+ # URLicon - v0.2.0
14
14
 
15
15
  `URLicon` helps you to discover an possible icon from a URL.
16
16
 
@@ -45,16 +45,16 @@ print("icon:", icon_url)
45
45
  `URLicon` use a simple "cache" method to avoid unecessary URL requests.
46
46
  It uses a [temp dir](https://docs.python.org/3/library/tempfile.html) for each
47
47
  execution. But you can define a your own directory and use the cache as much as
48
- you want setting `STRING_CACHE_ROOT_DIR` env var.
48
+ you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
49
49
 
50
50
  ```python
51
- STRING_CACHE_ROOT_DIR = os.getenv("STRING_CACHE_ROOT_DIR", None)
52
- cache = string_cache(cache_folder=STRING_CACHE_ROOT_DIR)
51
+ SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
52
+ cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
53
  ```
54
54
 
55
55
  And you can clean the cache with:
56
56
  ```python
57
- urlicon.string_cache.clean()
57
+ urlicon.simple_cache.clean()
58
58
  ```
59
59
 
60
60
  ## See Also
@@ -1,4 +1,4 @@
1
- # URLicon - v0.1.0
1
+ # URLicon - v0.2.0
2
2
 
3
3
  `URLicon` helps you to discover an possible icon from a URL.
4
4
 
@@ -33,16 +33,16 @@ print("icon:", icon_url)
33
33
  `URLicon` use a simple "cache" method to avoid unecessary URL requests.
34
34
  It uses a [temp dir](https://docs.python.org/3/library/tempfile.html) for each
35
35
  execution. But you can define a your own directory and use the cache as much as
36
- you want setting `STRING_CACHE_ROOT_DIR` env var.
36
+ you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
37
37
 
38
38
  ```python
39
- STRING_CACHE_ROOT_DIR = os.getenv("STRING_CACHE_ROOT_DIR", None)
40
- cache = string_cache(cache_folder=STRING_CACHE_ROOT_DIR)
39
+ SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
40
+ cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
41
41
  ```
42
42
 
43
43
  And you can clean the cache with:
44
44
  ```python
45
- urlicon.string_cache.clean()
45
+ urlicon.simple_cache.clean()
46
46
  ```
47
47
 
48
48
  ## See Also
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "urlicon"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  license = "MIT"
5
5
  description = "`URLicon` helps you to discover an possible icon from a URL."
6
6
  authors = [
@@ -1,9 +1,9 @@
1
1
  import os
2
2
 
3
3
 
4
- class string_cache:
4
+ class simple_cache:
5
5
  cache_folder: str | None = None
6
- cache_files_extension: str = "html"
6
+ cache_files_extension: str = "cache"
7
7
 
8
8
  def __init__(self, cache_folder: str | None = None):
9
9
  if cache_folder is not None:
@@ -45,7 +45,7 @@ class string_cache:
45
45
 
46
46
  new_file_name = f"{new_file_index}.{self.cache_files_extension}"
47
47
  new_file_path = os.path.join(cache_folder, new_file_name)
48
- with open(new_file_path, "w+") as new_file_writer:
48
+ with open(new_file_path, "wb") as new_file_writer:
49
49
  new_file_writer.write(text)
50
50
 
51
51
  @safe_cache_id
@@ -90,7 +90,12 @@ class string_cache:
90
90
  cached_file_path = os.path.join(cache_folder, cached_file_name)
91
91
  if not os.path.exists(cached_file_path):
92
92
  return None
93
- with open(cached_file_path, "r") as cached_file_reader:
93
+ if self.is_file_binary(cached_file_path):
94
+ read_mode = "rb"
95
+ else:
96
+ read_mode = "r"
97
+
98
+ with open(cached_file_path, read_mode) as cached_file_reader:
94
99
  code = cached_file_reader.read()
95
100
  return code
96
101
 
@@ -114,3 +119,11 @@ class string_cache:
114
119
 
115
120
  tmpdirname = tempfile.mkdtemp()
116
121
  return tmpdirname
122
+
123
+ def is_file_binary(self, file_path: str) -> bool:
124
+ try:
125
+ with open(file_path, "r") as fp:
126
+ fp.read(16)
127
+ return False
128
+ except UnicodeDecodeError:
129
+ return True
@@ -7,12 +7,12 @@ from bs4 import BeautifulSoup
7
7
  from dotenv import load_dotenv
8
8
 
9
9
  from urlicon import urls
10
- from urlicon.string_cache import string_cache
10
+ from urlicon.simple_cache import simple_cache
11
11
 
12
12
  load_dotenv()
13
13
 
14
- STRING_CACHE_ROOT_DIR = os.getenv("STRING_CACHE_ROOT_DIR", None)
15
- cache = string_cache(cache_folder=STRING_CACHE_ROOT_DIR)
14
+ SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
15
+ cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
16
16
 
17
17
 
18
18
  def get_url_icon(url):
@@ -152,6 +152,15 @@ def requests_get(url):
152
152
  if req.status_code != 200:
153
153
  return None
154
154
 
155
- code = req.text
155
+ code = req.content
156
156
  cache.set(text=code, cache_id=cache_prefix + url)
157
157
  return code
158
+
159
+
160
+ def is_file_binary(file_path: str) -> bool:
161
+ try:
162
+ with open(file_path, "r") as fp:
163
+ fp.read(16)
164
+ return False
165
+ except UnicodeDecodeError:
166
+ return True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: urlicon
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: `URLicon` helps you to discover an possible icon from a URL.
5
5
  Author-email: Cesar Cardoso <hello@cesarcardoso.cc>
6
6
  License-Expression: MIT
@@ -10,7 +10,7 @@ Requires-Dist: bs4>=0.0.2
10
10
  Requires-Dist: dotenv>=0.9.9
11
11
  Requires-Dist: requests>=2.32.5
12
12
 
13
- # URLicon - v0.1.0
13
+ # URLicon - v0.2.0
14
14
 
15
15
  `URLicon` helps you to discover an possible icon from a URL.
16
16
 
@@ -45,16 +45,16 @@ print("icon:", icon_url)
45
45
  `URLicon` use a simple "cache" method to avoid unecessary URL requests.
46
46
  It uses a [temp dir](https://docs.python.org/3/library/tempfile.html) for each
47
47
  execution. But you can define a your own directory and use the cache as much as
48
- you want setting `STRING_CACHE_ROOT_DIR` env var.
48
+ you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
49
49
 
50
50
  ```python
51
- STRING_CACHE_ROOT_DIR = os.getenv("STRING_CACHE_ROOT_DIR", None)
52
- cache = string_cache(cache_folder=STRING_CACHE_ROOT_DIR)
51
+ SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
52
+ cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
53
  ```
54
54
 
55
55
  And you can clean the cache with:
56
56
  ```python
57
- urlicon.string_cache.clean()
57
+ urlicon.simple_cache.clean()
58
58
  ```
59
59
 
60
60
  ## See Also
@@ -1,6 +1,6 @@
1
1
  README.md
2
2
  pyproject.toml
3
- src/urlicon/string_cache.py
3
+ src/urlicon/simple_cache.py
4
4
  src/urlicon/urlicon.py
5
5
  src/urlicon/urls.py
6
6
  src/urlicon.egg-info/PKG-INFO
File without changes
File without changes