Initial commit
This commit is contained in:
commit
b74e999df1
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
.vscode/
|
||||
.venv/
|
||||
__pycache__/
|
||||
dist/
|
||||
build/
|
||||
*.egg-info/
|
13
LICENSE
Normal file
13
LICENSE
Normal file
@ -0,0 +1,13 @@
|
||||
Copyright (c) 2021, samedamci <samedamci@disroot.org>
|
||||
|
||||
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.
|
1
gitfiler/__init__.py
Normal file
1
gitfiler/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .app import GitFiler as gitfiler
|
58
gitfiler/app.py
Normal file
58
gitfiler/app.py
Normal file
@ -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
|
34
setup.py
Normal file
34
setup.py
Normal file
@ -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"]),
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user