commit b74e999df1bed805426a0c66cf1540b61bbcbe04 Author: samedamci Date: Wed Aug 11 18:44:49 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af3a9bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.vscode/ +.venv/ +__pycache__/ +dist/ +build/ +*.egg-info/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2d31e15 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2021, samedamci + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/gitfiler/__init__.py b/gitfiler/__init__.py new file mode 100644 index 0000000..2011e4a --- /dev/null +++ b/gitfiler/__init__.py @@ -0,0 +1 @@ +from .app import GitFiler as gitfiler \ No newline at end of file diff --git a/gitfiler/app.py b/gitfiler/app.py new file mode 100644 index 0000000..3333f9d --- /dev/null +++ b/gitfiler/app.py @@ -0,0 +1,58 @@ +from urllib.request import urlopen +from urllib.parse import urlparse +from urllib.error import HTTPError +import re + + +class UnsupportedURL(Exception): + pass + + +class InvalidURL(Exception): + pass + + +class GitFiler: + def __init__(self, url: str) -> None: + self.allowed_domains = ["github.com", "gitlab.com"] + self.base_url = url + self.final_url = self.convert_url(self.base_url) + try: + with urlopen(self.final_url) as req: + self.text = str(req.read(), encoding="utf-8") + except HTTPError as e: + error_code = e.code + if error_code == 404: + raise InvalidURL("Site/file does not exists") + + @property + def url_domain(self) -> str: + return re.sub(r"^https?://", "", self.base_url).split("/")[0] + + @property + def is_domain_allowed(self) -> bool: + return True if self.url_domain in self.allowed_domains else False + + def validate_url(self, url: str) -> str: + result = urlparse(url) + if ( + all([result.scheme, result.netloc]) + and re.search(r"^https?://", url) is not None + ): + if self.is_domain_allowed: + return url + else: + raise UnsupportedURL(f"URL not in {self.allowed_domains}") + else: + raise InvalidURL("Specified string is not valid URL") + + def convert_url(self, url: str) -> str: + url = self.validate_url(url) + domain = self.url_domain + if domain == "github.com": + converted_str = url.replace( + "github.com", "raw.githubusercontent.com" + ).replace("/blob/", "/") + elif domain == "gitlab.com": + converted_str = url.replace("/blob/", "/raw/") + return converted_str diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1e8ee6f --- /dev/null +++ b/setup.py @@ -0,0 +1,34 @@ +from setuptools import find_packages, setup + +source_url = "https://github.com/samedamci/gitfiler" + +setup( + name="gitfiler", + version="0.1", + description="Simple tool to download single file from GitHub or GitLab.", + author="samedamci", + author_email="samedamci@disroot.org", + url=source_url, + project_urls={ + "Source": source_url, + "Tracker": f"{source_url}/issues", + }, + classifiers=[ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "License :: OSI Approved :: ISC License (ISCL)", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", + "Topic :: Software Development :: Libraries", + "Typing :: Typed", + ], + keywords="github gitlab downloader", + python_requires=">=3.6", + packages=find_packages(include=["gitfiler"]), +) \ No newline at end of file