Skip to content

Commit

Permalink
chore: lots of docs and some tests (#215)
Browse files Browse the repository at this point in the history
chore: lots of docs and some tests
  • Loading branch information
vn-ki committed Jul 17, 2019
2 parents 6c948a5 + 14bcf32 commit b90781e
Show file tree
Hide file tree
Showing 25 changed files with 798 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ matrix:
before_install:
- pip install pytest-cov
install:
- pip install -e ".[cloudflare]"
- pip install -e ".[dev]"
script:
- python -m pytest tests --cov=.
after_success:
Expand Down
3 changes: 3 additions & 0 deletions anime_downloader/extractors/base_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, url, quality=None, headers=None):

@property
def stream_url(self):
"""
URL of the video stream.
"""
if not self._stream_url:
self.get_data()

Expand Down
26 changes: 24 additions & 2 deletions anime_downloader/sites/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ class AnimeEpisode:
Title of the anime
meta: dict
metadata about the anime. [Can be empty]
QUALITIES: list
Possible qualities for the site
ep_no: string
Episode number/title of the episode
pretty_title: string
Pretty title of episode in format <animename>-<ep_no>
"""
QUALITIES = []
title = ''
Expand Down Expand Up @@ -309,6 +311,14 @@ def config(self):
return Config['siteconfig'][self.sitename]

def source(self, index=0):
"""
Get the source for episode
Returns
-------
`anime_downloader.extractors.base_extractor.BaseExtractor`
Extractor depending on the source.
"""
if not self._sources:
self.get_data()
try:
Expand All @@ -330,6 +340,18 @@ def _get_sources(self):

def download(self, force=False, path=None,
format='{anime_title}_{ep_no}', range_size=None):
"""
Downloads episode. This might be removed in a future release.
Parameters
----------
force: bool
Whether to force download or not.
path: string
Path to the directory/file where the file should be downloaded to.
format: string
The format of the filename if not provided.
"""
# TODO: Remove this shit
logger.info('Downloading {}'.format(self.pretty_title))
if format:
Expand Down
4 changes: 4 additions & 0 deletions anime_downloader/sites/animeflv.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def _scrape_episodes(self):
]
return links[::-1]

def _scrape_metadata(self):
soup = helpers.soupify(helpers.get(self.url).text)
self.title = soup.select_one('h2.Title').text


class AnimeflvEpisode(AnimeEpisode, sitename='animeflv'):
SERVERS = [
Expand Down
4 changes: 3 additions & 1 deletion anime_downloader/sites/helpers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
cf_session = cfscrape.create_scraper(sess=req_session)
default_headers = get_random_header()
temp_dir = tempfile.mkdtemp(prefix='animedl')
logger.debug(f"HTML file temp_dir: {temp_dir}")


def setup(func):
Expand Down Expand Up @@ -136,6 +137,7 @@ def soupify(res):

def _log_response_body(res):
import json
import pathlib
file = tempfile.mktemp(dir=temp_dir)
logger.debug(file)
with open(file, 'w') as f:
Expand All @@ -151,7 +153,7 @@ def _log_response_body(res):
data.append({
'method': res.request.method,
'url': res.url,
'file': '/' + file.split('/')[-1],
'file': pathlib.Path(file).name,
})
with open(data_file, 'w') as f:
json.dump(data, f)
14 changes: 14 additions & 0 deletions anime_downloader/sites/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@


def get_anime_class(url):
"""
Get anime class corresposing to url or name.
See :py:data:`anime_downloader.sites.ALL_ANIME_SITES` to get the possible anime sites.
Parameters
----------
url: string
URL of the anime.
Returns
-------
:py:class:`anime_downloader.sites.anime.Anime`
Concrete implementation of :py:class:`anime_downloader.sites.anime.Anime`
"""
for site in ALL_ANIME_SITES:
if site[1] in url:
try:
Expand Down
6 changes: 5 additions & 1 deletion anime_downloader/sites/itsaturday.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class Itsaturday(Anime, sitename='itsaturday'):
sitename = 'itsaturday'
DOMAIN = 'http://itsaturday.com'
DOMAIN = 'http://www.itsaturday.com'

@classmethod
def search(cls, query):
Expand All @@ -22,6 +22,10 @@ def _scrape_episodes(self):
]
return ret

def _scrape_metadata(self):
soup = helpers.soupify(helpers.get(self.url))
self.title = soup.select_one('h1.h3').text


class ItsaturdayEpisode(AnimeEpisode, sitename='itsaturday'):
def _get_sources(self):
Expand Down
7 changes: 7 additions & 0 deletions docs/api/base_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ Base classes

.. automodule:: anime_downloader.sites.anime


.. autofunction:: anime_downloader.sites.init.get_anime_class
.. autodata:: anime_downloader.commands.dl.sitenames

.. autoclass:: anime_downloader.sites.anime.Anime
:members: search, get_data, _scrape_episodes, _scrape_metadata

.. autoclass:: anime_downloader.sites.anime.AnimeEpisode
:members: source, download

.. autoclass:: anime_downloader.sites.anime.SearchResult
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ NOTE: To download from sites marked `[cloudflare]`, anime-downloader has to be i
usage/watch
usage/config
usage/sites
usage/api
advanced/custom_site
api/base_classes.rst
api/helper_functions.rst
30 changes: 30 additions & 0 deletions docs/usage/api.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Library usage
=============

Anime downloader can be used as a library too.


The following code searches for 'one punch' from twist.moe

:py:func:`~anime_downloader.sites.init.get_anime_class` can be used to import specific sites using the url one of :py:data:`~anime_downloader.commands.dl.sitenames`.

.. code:: python
from anime_downloader.sites import get_anime_class
Twist = get_anime_class('twist.moe')
search = Twist.search('one punch')
print(search[0].title)
# You can directly import twist too
from anime_downloader.sites.twistmoe import TwistMoe
anime = TwistMoe(search[0].url)
print(anime)
print(len(anime))
# Get first episodes url
print(anime[0].source().stream_url)
In the above example `TwistMoe` is a concrete implementation of :py:class:`anime_downloader.sites.anime.Anime`.
Search results is list of :py:class:`anime_downloader.sites.anime.SearchResult`.
51 changes: 35 additions & 16 deletions docs/usage/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@ The default config file is given below.

.. code:: json
{
"dl": {
"download_dir": ".",
"force_download": false,
"file_format": "{anime_title}/{anime_title}_{ep_no}",
"log_level": "INFO",
"player": null,
"quality": "720p",
"skip_download": false,
"url": false
},
"watch": {
"log_level": "INFO",
"quality": "720p"
}
}
{
"dl": {
"download_dir": ".",
"external_downloader": "{aria2}",
"fallback_qualities": [
"720p",
"480p",
"360p"
],
"file_format": "{anime_title}/{anime_title}_{ep_no}",
"force_download": false,
"player": null,
"provider": "9anime",
"quality": "720p",
"skip_download": false,
"url": false
},
"siteconfig": {
"animeflv": {
"server": "natsuki",
"version": "subbed"
},
"anistream.xyz": {
"version": "subbed"
},
"nineanime": {
"server": "mp4upload"
}
},
"watch": {
"log_level": "INFO",
"provider": "9anime",
"quality": "720p"
}
}
.. note::
- For the key ``file_format``, you can set ``anime_title``\ (which refers to the title of the anime) and ``ep_no`` which is the number of the epiosde.
Expand Down
77 changes: 16 additions & 61 deletions docs/usage/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@ Installation
The following are extended installation instructions for Windows and
Linux users. (For windows choco users, scroll to last)

Windows
~~~~~~~

This tool requires python >= 3.3.

- Go to python windows `downloads section`_. Choose the option
``Latest python 3 release``. *Note, not python 2 but python 3*
- Download and install it like any other application. **Don’t forget to
select ‘Add Python to PATH’ in the installation screen**
- After installation open a command prompt and type in the following
command.

::

pip install anime-downloader[cloudflare]

- Enjoy downloading anime.

NOTE: If you want to use ``watch`` you have to do some more steps.
(Trust me, it is work the additional steps) - Download mpv from `here`_.
- Extract the zip and place ``mpv.exe`` in the folder your command
prompt opens. (The path you see when you open command prompt). - Enjoy.
Read documentation for watch to know more.

Windows via ``choco``
~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -37,17 +13,26 @@ Windows via ``choco``
.. note::
make sure you are running the Command Prompt in "Run as Adminstrator" mode

- Install `Chocolatey`_ Package manager.

- Using the Chocolatey Package Manager::

choco install -y git mpv python3 aria2 nodejs
- Once these are installed::

pip3 install -U git+https://github.com/vn-ki/anime-downloader.git#egg=anime-downloader[cloudflare]
pip3 install -U git+https://github.com/vn-ki/anime-downloader.git

- then the commands to view a show would be::

anime watch --provider *Insert provider name* --new

Mac
~~~

Anime downloader is avaible from brew.::

brew install anime-downloader

Linux
~~~~~

Expand All @@ -56,51 +41,21 @@ If you are using linux, you most probably already have python installed.
Type ``pip --version`` into your terminal. If it says python2, replace
all the following ``pip`` with ``pip3``.

- You only need one command.
- Install aria2.

::
- Install anime-downloader ::

pip install anime-downloader[cloudflare]
pip3 install anime-downloader

- Enjoy.
- You can download mpv with your package manager. You can follow `this
guide`_, to install it on ubuntu.


Windows via ``choco`` (Method 2)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Contributed by @1ijack

- Using the `Chocolatey`_ Package manager (via Windows Nuget) to

- Install these pre-reqs: `git`_ `python3`_ `aria2`_ `mpv`_
- Refresh/Update environment changes (choco ``refreshenv`` command)
- clone this repository to
``[C:]\Users\[USERNAME]\anime-downloader``
- setup/build anime-downloader from
``"%userprofile%\anime-downloader"``
- run ``anime watch`` command
- To install master branch::

- the ``choco install`` command SHOULD be run with an administrator
privledged ``cmd.exe`` console:

- ``[WindowsKey] > "cmd.exe" > [Shift]+[Ctrl]+[Enter] > (Click "Yes")``

::

choco install -y git python3 aria2 mpv

- the rest of the commands SHOULD be run in an un-privledged/normal
``cmd.exe`` console:
pip3 install -U git+https://github.com/vn-ki/anime-downloader.git
- Enjoy.

- ``[WindowsKey] > "cmd.exe" > [Enter]`` \``\` refreshenv git clone
https://github.com/vn-ki/anime-downloader
“%userprofile%:raw-latex:`\anime`-downloader” cd /d "

.. _downloads section: https://www.python.org/downloads/windows/
.. _here: https://mpv.srsfckn.biz/
.. _this guide: http://ubuntuhandbook.org/index.php/2017/12/install-mpv-0-28-0-in-ubuntu-18-04-16-04/
.. _Chocolatey: https://chocolatey.org/install
.. _git: https://chocolatey.org/packages/git
.. _python3: https://chocolatey.org/packages/python3
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/sites.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Site Configs
------------

The following are the sites with configs. These can be configured using the config file. The keys and corresponding possible values are mentioned below.

.. autoclass:: anime_downloader.sites.animeflv.Animeflv
.. autoclass:: anime_downloader.sites.anistream.Anistream
.. autoclass:: anime_downloader.sites.nineanime.NineAnime
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
'tabulate>=0.8.3',
'pycryptodome>=3.8.2',
],
tests_require=[
'pytest',
],
extras_require={
'dev': [
'pytest',
'httpretty',
],
},
long_description=long_description,
long_description_content_type='text/markdown',
entry_points='''
Expand Down

0 comments on commit b90781e

Please sign in to comment.