attachments-fetcher 0.0.1__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Dennis Sitelew
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.1
2
+ Name: attachments-fetcher
3
+ Version: 0.0.1
4
+ Summary: A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links with the local ones.
5
+ Author-email: Dennis Sitelew <yowidin@gmail.com>
6
+ Project-URL: homepage, https://github.com/yowidin/attachments-fetcher
7
+ Project-URL: bugtrack, https://github.com/yowidin/attachments-fetcher/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Requires-Python: >=3.7
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+
14
+ # Attachments Fetcher
15
+ A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links
16
+ with the local (fetched) ones.
17
+
18
+ ## Usage example
19
+
20
+ For example, if you have a Markdown called `input.md`, with the following contents:
21
+
22
+ ```markdown
23
+ Hello world!
24
+ ![image](https://example.com/image/storage/sample.png)
25
+ ```
26
+
27
+ After running this script against it:
28
+
29
+ ```shell
30
+ af-make-local -i input.md -o output.md -m ./media
31
+ ```
32
+
33
+ You will find a new file called `output.md` with the following content:
34
+
35
+ ```markdown
36
+ Hello world!
37
+ ![image](./media/sample.png)
38
+ ```
39
+
40
+ and a `sample.png` image file, stored in the `./media` directory.
@@ -0,0 +1,27 @@
1
+ # Attachments Fetcher
2
+ A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links
3
+ with the local (fetched) ones.
4
+
5
+ ## Usage example
6
+
7
+ For example, if you have a Markdown called `input.md`, with the following contents:
8
+
9
+ ```markdown
10
+ Hello world!
11
+ ![image](https://example.com/image/storage/sample.png)
12
+ ```
13
+
14
+ After running this script against it:
15
+
16
+ ```shell
17
+ af-make-local -i input.md -o output.md -m ./media
18
+ ```
19
+
20
+ You will find a new file called `output.md` with the following content:
21
+
22
+ ```markdown
23
+ Hello world!
24
+ ![image](./media/sample.png)
25
+ ```
26
+
27
+ and a `sample.png` image file, stored in the `./media` directory.
File without changes
@@ -0,0 +1,3 @@
1
+ def run_fetcher():
2
+ from af.cli.make_local import main
3
+ main()
@@ -0,0 +1,70 @@
1
+ import argparse
2
+ import os
3
+ import re
4
+ import requests
5
+
6
+
7
+ def download_image(url, destination):
8
+ print(f'Downloading: {url}')
9
+ response = requests.get(url, stream=True)
10
+ if response.status_code == 200:
11
+ with open(destination, 'wb') as file:
12
+ for chunk in response.iter_content(1024):
13
+ file.write(chunk)
14
+
15
+
16
+ def replace_image_links(md_file: str, output: str, media_dir: str, force: bool):
17
+ if not os.path.isfile(md_file):
18
+ raise RuntimeError(f'Input file not found: {md_file}')
19
+
20
+ if os.path.isfile(output) and not force:
21
+ raise RuntimeError(f'Output file already exists, use -f to override: {output}')
22
+
23
+ # Create the output directory if it doesn't exist
24
+ os.makedirs(media_dir, exist_ok=True)
25
+
26
+ with open(md_file, 'r') as file:
27
+ content = file.read()
28
+
29
+ # Match image links in the Markdown file
30
+ pattern = r"!\[(.*?)\]\((.*?)\)"
31
+ matches = re.findall(pattern, content)
32
+
33
+ for alt_text, url in matches:
34
+ # Check if the URL has an image file extension
35
+ if not any(url.lower().endswith(ext) for ext in ['.png', '.jpg', '.jpeg', '.gif']):
36
+ continue
37
+
38
+ # Extract the image filename from the URL
39
+ filename = url.split("/")[-1]
40
+
41
+ # Download the image
42
+ image_path = os.path.join(media_dir, filename)
43
+ download_image(url, image_path)
44
+
45
+ # Replace the image link with the local file path
46
+ local_path = f"{media_dir}/{filename}"
47
+ content = content.replace(url, local_path)
48
+
49
+ # Write the updated content to a new Markdown file
50
+ with open(output, 'w') as file:
51
+ file.write(content)
52
+
53
+ print("Image links replaced successfully!")
54
+ print(f"Updated Markdown file saved as: {output}")
55
+
56
+
57
+ def main():
58
+ parser = argparse.ArgumentParser('Attachments Fetcher')
59
+ parser.add_argument('--input', '-i', type=str, required=True, help='Input file')
60
+ parser.add_argument('--output', '-o', type=str, required=True, help='Output file')
61
+ parser.add_argument('--media-dir', '-m', type=str, required=True, help='Directory to store downloaded images')
62
+ parser.add_argument('--force', '-f', action='store_true', required=False, default=False,
63
+ help='Override existing output file')
64
+
65
+ args = parser.parse_args()
66
+ replace_image_links(args.input, args.output, args.media_dir, args.force)
67
+
68
+
69
+ if __name__ == '__main__':
70
+ main()
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.1
2
+ Name: attachments-fetcher
3
+ Version: 0.0.1
4
+ Summary: A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links with the local ones.
5
+ Author-email: Dennis Sitelew <yowidin@gmail.com>
6
+ Project-URL: homepage, https://github.com/yowidin/attachments-fetcher
7
+ Project-URL: bugtrack, https://github.com/yowidin/attachments-fetcher/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Requires-Python: >=3.7
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+
14
+ # Attachments Fetcher
15
+ A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links
16
+ with the local (fetched) ones.
17
+
18
+ ## Usage example
19
+
20
+ For example, if you have a Markdown called `input.md`, with the following contents:
21
+
22
+ ```markdown
23
+ Hello world!
24
+ ![image](https://example.com/image/storage/sample.png)
25
+ ```
26
+
27
+ After running this script against it:
28
+
29
+ ```shell
30
+ af-make-local -i input.md -o output.md -m ./media
31
+ ```
32
+
33
+ You will find a new file called `output.md` with the following content:
34
+
35
+ ```markdown
36
+ Hello world!
37
+ ![image](./media/sample.png)
38
+ ```
39
+
40
+ and a `sample.png` image file, stored in the `./media` directory.
@@ -0,0 +1,13 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ requirements.txt
5
+ af/__init__.py
6
+ af/cli/__init__.py
7
+ af/cli/make_local.py
8
+ attachments_fetcher.egg-info/PKG-INFO
9
+ attachments_fetcher.egg-info/SOURCES.txt
10
+ attachments_fetcher.egg-info/dependency_links.txt
11
+ attachments_fetcher.egg-info/entry_points.txt
12
+ attachments_fetcher.egg-info/requires.txt
13
+ attachments_fetcher.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ af-make-local = af.cli:run_fetcher
@@ -0,0 +1,2 @@
1
+ requests==2.31.0
2
+ urllib3==1.26.6
@@ -0,0 +1,28 @@
1
+ [project]
2
+ name = "attachments-fetcher"
3
+ version = "0.0.1"
4
+ authors = [
5
+ { name = "Dennis Sitelew", email = "yowidin@gmail.com" },
6
+ ]
7
+ description = "A tool for downloading embedded images in a Markdown document into a dedicated directory and replacing the image links with the local ones."
8
+ readme = "README.md"
9
+ requires-python = ">=3.7"
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "License :: OSI Approved :: MIT License",
13
+ ]
14
+ dynamic = ["dependencies"]
15
+
16
+ [project.scripts]
17
+ af-make-local = "af.cli:run_fetcher"
18
+
19
+ [project.urls]
20
+ homepage = "https://github.com/yowidin/attachments-fetcher"
21
+ bugtrack = "https://github.com/yowidin/attachments-fetcher/issues"
22
+
23
+ [build-system]
24
+ requires = ["setuptools>=61.0", "wheel"]
25
+ build-backend = "setuptools.build_meta"
26
+
27
+ [tool.setuptools.dynamic]
28
+ dependencies = { file = ["requirements.txt"] }
@@ -0,0 +1,4 @@
1
+ requests==2.31.0
2
+
3
+ # The newer versions of urllib3 require OpenSSL > 1.1 and do not work with LibreSSL
4
+ urllib3==1.26.6
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+