urlicon 0.2.0__tar.gz → 0.2.2__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.2.0
3
+ Version: 0.2.2
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
@@ -9,8 +9,9 @@ Description-Content-Type: text/markdown
9
9
  Requires-Dist: bs4>=0.0.2
10
10
  Requires-Dist: dotenv>=0.9.9
11
11
  Requires-Dist: requests>=2.32.5
12
+ Requires-Dist: unforgettable
12
13
 
13
- # URLicon - v0.2.0
14
+ # URLicon - v0.2.2
14
15
 
15
16
  `URLicon` helps you to discover an possible icon from a URL.
16
17
 
@@ -42,19 +43,19 @@ print("icon:", icon_url)
42
43
 
43
44
  ### Caching
44
45
 
45
- `URLicon` use a simple "cache" method to avoid unecessary URL requests.
46
+ `URLicon` use the [unforgettable "cache"](https://github.com/bouli/unforgettable) to avoid unecessary URL requests.
46
47
  It uses a [temp dir](https://docs.python.org/3/library/tempfile.html) for each
47
48
  execution. But you can define a your own directory and use the cache as much as
48
49
  you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
49
50
 
50
51
  ```python
51
52
  SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
52
- cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
+ cache = unforgettable(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
54
  ```
54
55
 
55
56
  And you can clean the cache with:
56
57
  ```python
57
- urlicon.simple_cache.clean()
58
+ urlicon.unforgettable.clean()
58
59
  ```
59
60
 
60
61
  ## See Also
@@ -1,4 +1,4 @@
1
- # URLicon - v0.2.0
1
+ # URLicon - v0.2.2
2
2
 
3
3
  `URLicon` helps you to discover an possible icon from a URL.
4
4
 
@@ -30,19 +30,19 @@ print("icon:", icon_url)
30
30
 
31
31
  ### Caching
32
32
 
33
- `URLicon` use a simple "cache" method to avoid unecessary URL requests.
33
+ `URLicon` use the [unforgettable "cache"](https://github.com/bouli/unforgettable) 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
36
  you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
37
37
 
38
38
  ```python
39
39
  SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
40
- cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
40
+ cache = unforgettable(cache_folder=SIMPLE_CACHE_ROOT_DIR)
41
41
  ```
42
42
 
43
43
  And you can clean the cache with:
44
44
  ```python
45
- urlicon.simple_cache.clean()
45
+ urlicon.unforgettable.clean()
46
46
  ```
47
47
 
48
48
  ## See Also
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "urlicon"
3
- version = "0.2.0"
3
+ version = "0.2.2"
4
4
  license = "MIT"
5
5
  description = "`URLicon` helps you to discover an possible icon from a URL."
6
6
  authors = [
@@ -12,6 +12,7 @@ dependencies = [
12
12
  "bs4>=0.0.2",
13
13
  "dotenv>=0.9.9",
14
14
  "requests>=2.32.5",
15
+ "unforgettable",
15
16
  ]
16
17
 
17
18
  [tool.bumpversion]
@@ -5,14 +5,14 @@ import urllib
5
5
  import requests
6
6
  from bs4 import BeautifulSoup
7
7
  from dotenv import load_dotenv
8
+ from unforgettable import unforgettable
8
9
 
9
10
  from urlicon import urls
10
- from urlicon.simple_cache import simple_cache
11
11
 
12
12
  load_dotenv()
13
13
 
14
14
  SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
15
- cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
15
+ cache = unforgettable(cache_folder=SIMPLE_CACHE_ROOT_DIR)
16
16
 
17
17
 
18
18
  def get_url_icon(url):
@@ -53,6 +53,14 @@ def get_meta_icon_from_url(url, url_soup=None):
53
53
  final_icon = soup_icon
54
54
  img = final_icon["href"]
55
55
 
56
+ try:
57
+ url_request = requests_get(url)
58
+ except:
59
+ return None
60
+
61
+ if url_request is None:
62
+ img = None
63
+
56
64
  img = urls.ensure_relative_path(img, url)
57
65
  return img, url_soup
58
66
 
@@ -153,7 +161,7 @@ def requests_get(url):
153
161
  return None
154
162
 
155
163
  code = req.content
156
- cache.set(text=code, cache_id=cache_prefix + url)
164
+ cache.set(content=code, cache_id=cache_prefix + url)
157
165
  return code
158
166
 
159
167
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: urlicon
3
- Version: 0.2.0
3
+ Version: 0.2.2
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
@@ -9,8 +9,9 @@ Description-Content-Type: text/markdown
9
9
  Requires-Dist: bs4>=0.0.2
10
10
  Requires-Dist: dotenv>=0.9.9
11
11
  Requires-Dist: requests>=2.32.5
12
+ Requires-Dist: unforgettable
12
13
 
13
- # URLicon - v0.2.0
14
+ # URLicon - v0.2.2
14
15
 
15
16
  `URLicon` helps you to discover an possible icon from a URL.
16
17
 
@@ -42,19 +43,19 @@ print("icon:", icon_url)
42
43
 
43
44
  ### Caching
44
45
 
45
- `URLicon` use a simple "cache" method to avoid unecessary URL requests.
46
+ `URLicon` use the [unforgettable "cache"](https://github.com/bouli/unforgettable) to avoid unecessary URL requests.
46
47
  It uses a [temp dir](https://docs.python.org/3/library/tempfile.html) for each
47
48
  execution. But you can define a your own directory and use the cache as much as
48
49
  you want setting `SIMPLE_CACHE_ROOT_DIR` env var.
49
50
 
50
51
  ```python
51
52
  SIMPLE_CACHE_ROOT_DIR = os.getenv("SIMPLE_CACHE_ROOT_DIR", None)
52
- cache = simple_cache(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
+ cache = unforgettable(cache_folder=SIMPLE_CACHE_ROOT_DIR)
53
54
  ```
54
55
 
55
56
  And you can clean the cache with:
56
57
  ```python
57
- urlicon.simple_cache.clean()
58
+ urlicon.unforgettable.clean()
58
59
  ```
59
60
 
60
61
  ## See Also
@@ -1,6 +1,5 @@
1
1
  README.md
2
2
  pyproject.toml
3
- src/urlicon/simple_cache.py
4
3
  src/urlicon/urlicon.py
5
4
  src/urlicon/urls.py
6
5
  src/urlicon.egg-info/PKG-INFO
@@ -1,3 +1,4 @@
1
1
  bs4>=0.0.2
2
2
  dotenv>=0.9.9
3
3
  requests>=2.32.5
4
+ unforgettable
@@ -1,129 +0,0 @@
1
- import os
2
-
3
-
4
- class simple_cache:
5
- cache_folder: str | None = None
6
- cache_files_extension: str = "cache"
7
-
8
- def __init__(self, cache_folder: str | None = None):
9
- if cache_folder is not None:
10
- self.cache_folder = cache_folder
11
- else:
12
- self.cache_folder = self.get_cache_folder()
13
- pass
14
-
15
- def safe_cache_id(func):
16
- def filter_cache_id(cache_id):
17
- cache_id = cache_id.replace('"', "").replace("\\", "")
18
- cache_id = f'"{cache_id}"'
19
- return cache_id
20
-
21
- def _filter_cache_id_func(*args, **kwargs):
22
- if "cache_id" in args:
23
- args["cache_id"] = filter_cache_id(cache_id=args["cache_id"])
24
-
25
- if "cache_id" in kwargs:
26
- kwargs["cache_id"] = filter_cache_id(cache_id=kwargs["cache_id"])
27
-
28
- return func(*args, **kwargs)
29
-
30
- return _filter_cache_id_func
31
-
32
- @safe_cache_id
33
- def set(self, text: str, cache_id: str):
34
- cache_folder = self.get_cache_folder()
35
- cache_index_file_path = self.get_cache_index_path()
36
-
37
- cache_folder_files = os.listdir(cache_folder)
38
- cached_file_index = self.get_index_from_file_index(_safe_cache_id=cache_id)
39
- if cached_file_index is not None:
40
- new_file_index = cached_file_index
41
- else:
42
- new_file_index = len(cache_folder_files)
43
- with open(cache_index_file_path, "a") as cache_index_file_writer:
44
- cache_index_file_writer.write(f"\n{new_file_index}: {cache_id}")
45
-
46
- new_file_name = f"{new_file_index}.{self.cache_files_extension}"
47
- new_file_path = os.path.join(cache_folder, new_file_name)
48
- with open(new_file_path, "wb") as new_file_writer:
49
- new_file_writer.write(text)
50
-
51
- @safe_cache_id
52
- def get(self, cache_id: str) -> str:
53
- cached_file_index = self.get_index_from_file_index(_safe_cache_id=cache_id)
54
- code = self.get_cached_file_by_index(cached_file_index=cached_file_index)
55
- return code
56
-
57
- def get_index_from_file_index(self, _safe_cache_id):
58
- cache_index_file = self.get_cache_index_file()
59
- if cache_index_file.find(_safe_cache_id) < 1:
60
- return None
61
- cache_index_file = cache_index_file[: cache_index_file.find(_safe_cache_id) - 2]
62
- cached_file_index = int(cache_index_file.split("\n")[-1].strip())
63
- return cached_file_index
64
-
65
- def get_cache_index_path(
66
- self,
67
- ) -> str:
68
- cache_index_file_name = "cache_index.yaml"
69
- cache_folder = self.get_cache_folder()
70
- cache_index_file_path = os.path.join(cache_folder, cache_index_file_name)
71
-
72
- if not os.path.exists(cache_index_file_path):
73
- with open(cache_index_file_path, "w+") as cache_index_file_writer:
74
- cache_index_file_writer.write(f"0: {cache_index_file_name}")
75
-
76
- return cache_index_file_path
77
-
78
- def get_cache_index_file(
79
- self,
80
- ) -> str:
81
- with open(self.get_cache_index_path(), "r") as f:
82
- cache_index_file_content = f.read()
83
-
84
- return cache_index_file_content
85
-
86
- def get_cached_file_by_index(self, cached_file_index: int) -> str:
87
- code = None
88
- cache_folder = self.get_cache_folder()
89
- cached_file_name = f"{cached_file_index}.{self.cache_files_extension}"
90
- cached_file_path = os.path.join(cache_folder, cached_file_name)
91
- if not os.path.exists(cached_file_path):
92
- return None
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:
99
- code = cached_file_reader.read()
100
- return code
101
-
102
- def clean(
103
- self,
104
- ):
105
- cache_folder = self.get_cache_folder()
106
- cache_folder_files = os.listdir(cache_folder)
107
- for file in cache_folder_files:
108
- file_to_clean = os.path.join(cache_folder, file)
109
- if os.path.exists(file_to_clean):
110
- os.remove(file_to_clean)
111
-
112
- def get_cache_folder(
113
- self,
114
- ):
115
- import tempfile
116
-
117
- if self.cache_folder is not None:
118
- return self.cache_folder
119
-
120
- tmpdirname = tempfile.mkdtemp()
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
File without changes
File without changes