nstrydom2 / anonfile-api

An unofficial Python Anonfiles.com API
MIT License
62 stars 24 forks source link

Return download_url, from which one can get original filename #18

Closed Divkix closed 4 years ago

Divkix commented 4 years ago

A user can get original filename by using:

from anonfile.anonfile import AnonFile
anon = AnonFile('api_key')

file_url = "https://anonfiles.com/F6nbp9Hdoe/example_txt"
download_url = anon.download_file(file_url) 

filename = download_url.split("/")[-1]
nstrydom2 commented 4 years ago

The desired result is to have the original file name returned upon downloading, correct? I suppose I don't have a problem accept this request, however it will not be available on PYPI until another release. If this is a feature most users are wanting, then we can add this to the project.

Divkix commented 4 years ago

Yes, the desired result is to have the original filename. If the original file is returned, it would be easy to sort files using a Python program itself as we can get the extract filename.

nstrydom2 commented 4 years ago

There is already a JSON object that is returned on the back-end, why not return that? The docs cover the extent of it https://anonfiles.com/docs/api

nstrydom2 commented 4 years ago

Now that I look over the code again, I think this should be it's own method. Like 'get_filename()' or something..

Divkix commented 4 years ago

We can take the filename by doing as following:

from anonfile.anonfile import AnonFile
anon = AnonFile('api_key')

file_url = "https://anonfiles.com/F6nbp9Hdoe/example_txt"
filename = anon.download_file(file_url) 

By using this, filename would be returned for each file when it is downloaded.

In the library, it could be also edited as:

    @authenticated
    def download_file(self, url, location=None):
        # Scrapes the provided url for the url to the
        # actual file. Only called by 'download_file()'
        def scrape_file_location(url):
            # Get method, retrieving the web page
            response = requests.get(url, timeout=self.timeout)
            soup = BeautifulSoup(response.text, 'lxml')

            return soup.find_all('a')[1].attrs['href']

        try:
            download_url = scrape_file_location(url)
            filename = download_url.split("/")[-1]

            # download code goes here
            if download_url is not None:
                wget.download(download_url, location)

        except Exception as ex:
            print("[*] Error -- " + str(ex))
            return
        return filename

I also intend to use pySmartDL instead of wget to accelerate downloads and allow multiple files to be downloaded and get things such as total file size, ETA, speed, etc.

As for the anonfile api, we couldn't get the exact extension of the file, filename is returned as: example_file_txt

nstrydom2 commented 4 years ago

Alright, we can go with returning the filename from the 'download_file()' method. I would, however, prefer this to be in it's own branch, maybe even labeled with the next version. For the purpose of merging at the same time as I release to PYPI. Also, I really like the idea of integrating the alternative file download module. You write it, you get credit for it. Any other ideas are encouraged as long as the lightweight nature of this wrapper remains intact.

nstrydom2 commented 4 years ago

Ok. I made a new branch, and merged your commits into that branch. Would you mind making a new branch and any future pull requests can be based for 'v_0_1_3_stable' from that branch.

As for the filename issue.. This is a bit hacky, and I don't want to commit this to the repo. But this is one way to grab the filename in the proper string format.

return f"{filename.replace('_'+filename.split('_')[-1], '')}.{filename.split('_')[-1]}"

That being said, I am going to essentially use the same technique, but it will be cleaner and allow for easier reading. Something closer to this.

ext = filename.split('_')[-1]
file = filename.replace(f'_{ext}', '')
return f'{file}.{ext}'

I'll look into it more.

nstrydom2 commented 4 years ago

Forget the solution above, it was not necessary. I have refactored the library to scrape and return the appropriate url when uploading. So, the 'upload_file()' method now returns status and the accurate url to download your uploaded file. Does this work for you? Try it out in the 'v0_1_3_stable' branch.

Divkix commented 4 years ago

It was actually the problem with the download, where I wasn't able to get the exact filename that was downloaded.

I'll just check the new branch and will open a pr if any change would be there!

nstrydom2 commented 4 years ago

Yeah, just show me what you and I will merge with the 'v0_1_3_stable' branch. Make sure to make your own branch and direct all pull requests to the 'v0_1_3_stable' branch. Sorry for that confusion.