Skip to content

Commit

Permalink
Bump 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanvikash committed Oct 6, 2020
1 parent 47c86b8 commit b873d1e
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 75 deletions.
96 changes: 60 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Don't Use it in Production or Live Projects Currently Its Unstable
___
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Python 3.6](https://img.shields.io/badge/python-3-blue.svg)](https://www.python.org/downloads/release/python-360/)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/aryanvikash/pyaiodl)
[![PyPI license](https://img.shields.io/pypi/l/ansicolortags.svg)](https://github.com/aryanvikash/pyaiodl)
[![Open Source Love png3](https://badges.frapsoft.com/os/v3/open-source.png?v=103)](https://github.com/aryanvikash/pyaiodl)
Expand All @@ -11,11 +11,11 @@ ___
## Version
[![Beta badge](https://img.shields.io/badge/STATUS-BETA-red.svg)](https://github.com/aryanvikash/pyaiodl)

[![PyPI version](https://badge.fury.io/py/pyaiodl.svg)](https://badge.fury.io/py/pyaiodl)
[![PyPI version](https://badge.fury.io/py/pyaiodl.svg)](https://pypi.org/project/pyaiodl/)


## installation
pypi Method (recommanded)
pypi Method (Recommended)

pip3 install pyaiodl

Expand All @@ -24,23 +24,32 @@ Github repo method
pip3 install git+https://github.com/aryanvikash/pyaiodl.git


# Avalible Methods
# Available Methods
- Downloader class Instance
`Non-blocking , params = [fake_useragent:bool,chunk_size:int ,download_path:str] optinals`

dl = Downloader()
- Download [ `download(self.url)` ]
- Download [ `download(self,url)` ]

uuid = await dl.download(url)
- Errors [` iserror(self, uuid) `]
` : Returns - Error Or None
, Even On cancel It returns an error "{uuid} Cancelled"`

```
await dl.iserror(uuid)
```


- cancel [ `cancel(self, uuid)` ]

await dl.cancel(uuid)
- Get Status [ `status(self, uuid)` ]

response = await dl.status(uuid)



returns a dict

"""
Expand All @@ -52,12 +61,12 @@ Github repo method
downloaded_str :str
progress:int
download_speed:str
active: bool
complete :bool
download_path:str

"""

- is_active returns : bool [ `is_active( self,uuid )` ]` on cancel and download complete it will return False`
- is_active returns : bool [ `is_active( self,uuid )` ]` - on cancel ,error , download complete return False`

result = await dl.is_active(uuid)

Expand All @@ -68,56 +77,71 @@ ___

```py

from pyaiodl import Downloader, errors
import asyncio
from pyaiodl import Downloader

url = "https://speed.hetzner.de/100MB.bin"

async def main(url):
dl = Downloader()

async def main():
dl = Downloader()
# you can pass your
# custom chunk size and Download Path
# dl = Downloader(download_path="/your_dir/", chunk_size=10000)
uuid = await dl.download(url)
try:
#Non-blocking
uuid = await dl.download(url)



#progress
while await dl.is_active(uuid):

r = await dl.status(uuid)
#cancel
# await dl.cancel(uuid)
print(f"""
Filename: {r['filename']}
Total : {r['total_size_str']}
Downloaded : {r['downloaded_str']}
Download Speed : {r['download_speed']}
progress: {r['progress']}
""")

#cancel
if r['progress'] > 0:
try:
await dl.cancel("your_uuid")
except errors.DownloadNotActive as na:
print(na)


print(f"""
Filename: {r['filename']}
Total : {r['total_size_str']}
Downloaded : {r['downloaded_str']}
Download Speed : {r['download_speed']}
progress: {r['progress']}
""")

# let him breath for a second:P
await asyncio.sleep(1)


# If You are putting uuid manually Than its better handle This Exception
except errors.InvalidId:
print("not valid uuid")
return

# when loop Breaks There are 2 Possibility
# either Its An error Or Download Complete
# Cancelled Is also count as error
if await dl.iserror(uuid):
print(await dl.iserror(uuid))

else:
# Final filename / path
print( "download completed : ",r['download_path'])
print("Download completed : ", r['download_path'])


except Exception as e:
print(e)
asyncio.get_event_loop().run_until_complete(main())

asyncio.get_event_loop().run_until_complete(main(url))
```

___
### known Bugs -
- Error is Not Handled Correctly
- None Please Report :)

___
# TODO

- Multipart Download
- Queue Download / Parallel Downloads Limit
- Better Error Handling
- [x] Better Error Handling



Expand Down
55 changes: 55 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from pyaiodl import Downloader, errors
import asyncio
url = "https://speed.hetzner.de/100MB.bin"


async def main():
dl = Downloader()

# or
# dl = Downloader(download_path="/your_dir/", chunk_size=10000)

uuid = await dl.download(url)


try:
while await dl.is_active(uuid):

r = await dl.status(uuid)

#cancel
if r['progress'] > 0:
try:
await dl.cancel("your_uuid")
except errors.DownloadNotActive as na:
print(na)


print(f"""
Filename: {r['filename']}
Total : {r['total_size_str']}
Downloaded : {r['downloaded_str']}
Download Speed : {r['download_speed']}
progress: {r['progress']}
""")

# let him breath for a second:P
await asyncio.sleep(1)

# If You are putting uuid manually Than its better handle This Exception
except errors.InvalidId:
print("not valid uuid")
return

# when loop Breaks There are 2 Possibility either Its An error Or Download Complete
# Cancelled Is also count as error
if await dl.iserror(uuid):
print(await dl.iserror(uuid))

else:
# Final filename / path
print("Download completed : ", r['download_path'])


asyncio.get_event_loop().run_until_complete(main())

51 changes: 45 additions & 6 deletions pyaiodl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@


from .pyaiodl import PrivateDl
from .errors import DownloadNotActive, InvalidId


class Downloader:
def __init__(self, chunk_size=10000, download_path=None):
def __init__(self, chunk_size=None, download_path=None):
self._alldownloads = {}
self.download_path = download_path
# custom chunk size
Expand All @@ -34,14 +35,52 @@ async def download(self, url):
raise Exception(e)
return _uuid

# To check If Download Is active
async def is_active(self, uuid):
_tempobj = self._alldownloads[uuid]["obj"]
return _tempobj.isActive
try:
_tempobj = self._alldownloads[uuid]["obj"]
except KeyError:
raise InvalidId()

return not _tempobj.task.done()

# Get Status
async def status(self, uuid):
_tempobj = self._alldownloads[uuid]["obj"]
try:
_tempobj = self._alldownloads[uuid]["obj"]
except KeyError:
raise InvalidId()
return await _tempobj.getStatus()

#Cancel your Download

async def cancel(self, uuid):
_tempobj = self._alldownloads[uuid]["obj"]
return await _tempobj.cancel(uuid)
try:
_tempobj = self._alldownloads[uuid]["obj"]

# mark as cancelled
self._alldownloads[uuid]["iscancel"] = True
_tempobj._cancelled = True

cancelstatus = await _tempobj.cancel(uuid) or False

except KeyError:
raise InvalidId()

if _tempobj.task.done():

raise DownloadNotActive(f"{uuid} : Download Not active")

return cancelstatus

async def iserror(self, uuid):
try:
_tempobj = self._alldownloads[uuid]["obj"]
_iscancel = self._alldownloads[uuid]["iscancel"]

if _iscancel:
return f"{uuid} Cancelled"
except KeyError:
raise InvalidId()

return _tempobj.iserror
8 changes: 5 additions & 3 deletions pyaiodl/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
__all_ = ['download_not_active']
__all_ = ['DownloadNotActive', 'InvalidId']


class DownloadNotActive(Exception):
""" Download Not active """


class download_not_active(Exception):
""" Download Not active """
class InvalidId(Exception):
""" on Invalid uuid """
Loading

0 comments on commit b873d1e

Please sign in to comment.