This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Simplified error classes. - Added the LENGTH_ID constant. - Now the KEY parameter has become optional. To delete or rename a file, it is enough to specify the KEY in the ID parameter. - Now the ID and URL parameters are determined directly from the server response. - The URL_IMPORT parameter no longer receives a link if the file ID is specified instead of the KEY parameter. - The UploadgramUsingKeyError error now has an updated description. - Now the userTelegramId and userId parameters can take the value None of the "None" type if they are not in the response from the server. - The self variable has been renamed.json on the self.readfile of the UploadgramPyAPI.NewFile class. - Minor edits have been made to __doc__. - Other edits have been made to the code.
- Loading branch information
1 parent
f019e5c
commit a08b4bc
Showing
2 changed files
with
52 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,20 +11,24 @@ | |
- github: https://github.com/tankalxat34/UploadgramPyAPI | ||
- email: [email protected] | ||
Contacts (Uploadgram Author): | ||
- telegrams channel: https://t.me/uploadgramme | ||
- telegram channel: https://t.me/uploadgramme | ||
**************************************************************** | ||
*----------------------Example of use:-------------------------* | ||
**************************************************************** | ||
import UploadgramPyAPI | ||
# upload any file | ||
# Upload any file | ||
up_new_file = UploadgramPyAPI.NewFile("D:\\image.jpg") | ||
up_new_file.upload() | ||
# set up connection to file | ||
# Set up connection to file like here | ||
up_file = UploadgramPyAPI.File("611e5e6237f6fg", "e3da26e9dddd2e01b8c0831370695e9088a96ff81e262fc2g") | ||
# or like here | ||
# up_file = UploadgramPyAPI.File("e3da26e9dddd2e01b8c0831370695e9088a96ff81e262fc2g") | ||
# or like this | ||
# up_file = UploadgramPyAPI.File("611e5e6237f6fg") | ||
# Download any file | ||
up_file.download() | ||
|
@@ -48,33 +52,34 @@ | |
global NONE | ||
NONE = "none" | ||
|
||
# Const for default length of 'id' parameter | ||
global LENGTH_ID | ||
LENGTH_ID = 14 | ||
|
||
class UploadgramConnectionError(Exception): | ||
def __init__(self): | ||
self.message = "uploadgram.me currently unavailable. Please, try again later." | ||
super().__init__(self.message) | ||
super().__init__("uploadgram.me currently unavailable. Please, try again later.") | ||
|
||
class UploadgramUsingKeyError(Exception): | ||
def __init__(self, text): | ||
self.text = text | ||
self.message = 'You can not ' + text + ' this file because you using the NONE-const for "key" parameter' | ||
super().__init__(self.message) | ||
super().__init__('You can not ' + text + ' this file because you using the NONE-const for "key" parameter or you using "id" file instead of "key" parameter') | ||
|
||
class UploadgramFileIsNotAvalible(Exception): | ||
def __init__(self, id): | ||
self.id = id | ||
self.message = "This file " + id + " does not exists" | ||
super().__init__(self.message) | ||
super().__init__("This file " + id + " does not exists") | ||
|
||
class File: | ||
def __init__(self, id: str, key: str): | ||
def __init__(self, id, key=NONE): | ||
""" | ||
:param id: Get id file that placed at the end URL for file | ||
:param key: Key for rename and remove file from server | ||
""" | ||
self.id = id | ||
self.key = key | ||
if len(self.id) > LENGTH_ID: | ||
self.key = self.id | ||
else: | ||
self.key = key | ||
|
||
self.url = "https://dl.uploadgram.me/" + self.id | ||
self.url_import = None | ||
|
||
try: | ||
|
@@ -85,17 +90,31 @@ def __init__(self, id: str, key: str): | |
if self.r: | ||
self.json = self.r.json() | ||
|
||
self.id = self.json["url"].split("/")[-1] | ||
self.url = "https://dl.uploadgram.me/" + self.id | ||
|
||
### Create attibutes for class ### | ||
self.name = self.json["filename"] | ||
self.size = self.json["size"] | ||
self.userTelegramId = self.json["userTelegramId"] | ||
self.userIp = self.json["userIp"] | ||
|
||
### Create import link ### | ||
dict_part_url_to_import = ({str(self.key): {'filename': os.path.basename(self.name), | ||
'size': int(self.size), | ||
'url': str(self.url)}}) | ||
self.url_import = "https://uploadgram.me/upload/#import:" + json.dumps(dict_part_url_to_import) | ||
|
||
try: | ||
self.userTelegramId = self.json["userTelegramId"] | ||
except Exception: | ||
self.userTelegramId = None | ||
|
||
try: | ||
self.userIp = self.json["userIp"] | ||
except Exception: | ||
self.userIp = None | ||
|
||
if str(self.key) != NONE and len(self.key) > LENGTH_ID: | ||
### Create import link ### | ||
dict_part_url_to_import = ({str(self.key): {'filename': os.path.basename(self.name), | ||
'size': int(self.size), | ||
'url': str(self.url)}}) | ||
self.url_import = "https://uploadgram.me/upload/#import:" + json.dumps(dict_part_url_to_import) | ||
else: | ||
self.url_import = 'UNABLE TO GENERATE url_import BECAUSE self.key GOT A STRING "none" VALUE' | ||
else: | ||
raise UploadgramFileIsNotAvalible(self.id) | ||
|
||
|
@@ -142,12 +161,11 @@ def __init__(self, path): | |
""" | ||
:param path: Path to file what you want to upload | ||
""" | ||
|
||
try: | ||
self.check_to_avaliable = requests.get("https://api.uploadgram.me/status", headers={"user-agent": USER_AGENT}) | ||
self.path = path | ||
self.url_upload = 'https://api.uploadgram.me/upload' | ||
self.json = open(self.path, "rb") | ||
self.readfile = open(self.path, "rb") | ||
|
||
self.key = None | ||
self.id = None | ||
|
@@ -160,7 +178,7 @@ def upload(self): | |
"""return {'ok': True, 'url': 'https://dl.uploadgram.me/new_file_id', 'delete': 'delete_key'}""" | ||
self.r = requests.post(self.url_upload, | ||
headers={"user-agent": USER_AGENT}, | ||
files={"file_upload": self.json}) | ||
files={"file_upload": self.readfile}) | ||
self.key = self.r.json()["delete"] | ||
self.id = self.r.json()["url"].split("/")[-1] | ||
self.url = self.r.json()["url"] | ||
|