Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
Download bangumi by specified keywords (included and excluded).
Browse files Browse the repository at this point in the history
  • Loading branch information
RicterZ committed Oct 20, 2016
1 parent 4df1b6d commit f4487f2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ BGmi is a cli tool for subscribed bangumi.
====
TODO
====
+ Download bangumi by file format, file size, etc.
Empty as my wallet.

==========
Update Log
==========
+ Download bangumi by specified keywords (included and excluded).
+ **BGmi have supported Windows now**
+ `bgmi followed` is deprecated.
+ :code:`bgmi followed` is deprecated.
+ Update specified bangumi rather than update all bangumi.
+ Play bangumi online with danmaku

Expand Down
32 changes: 22 additions & 10 deletions bgmi/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import bgmi.config
from bgmi.config import FETCH_URL, DETAIL_URL, MAX_PAGE
from bgmi.models import Bangumi, Followed, STATUS_FOLLOWED, STATUS_UPDATED
from bgmi.models import Bangumi, Followed, Filter, STATUS_FOLLOWED, STATUS_UPDATED
from bgmi.utils.utils import print_error, print_warning, print_info, unicodeize, \
test_connection, bug_report, get_terminal_col, _, GREEN, YELLOW, COLOR_END
import bgmi.patches.bangumi
Expand Down Expand Up @@ -235,7 +235,7 @@ def parse_episode(data):
return 0


def fetch_episode(keyword, name='', subtitle_group=None):
def fetch_episode(keyword, name='', subtitle_group=None, include=None, exclude=None):
result = []
keyword = bgmi.patches.keyword.main(name, keyword)

Expand Down Expand Up @@ -273,28 +273,40 @@ def fetch_episode(keyword, name='', subtitle_group=None):
bangumi_update_info['download'] = detail.find('a').attrs.get('href')

# filter subtitle group
if subtitle_group and subtitle_group is not None:
if subtitle_group:
subtitle_group_list = map(lambda s: s.strip(), subtitle_group.split(','))
for s in subtitle_group_list:
if _(s) in _(bangumi_update_info['subtitle_group']):
result.append(bangumi_update_info)
else:
result.append(bangumi_update_info)

if include:
include_list = map(lambda s: s.strip(), include.split(','))
result = filter(lambda s: True if all(map(lambda t: _(t) in _(s['title']),
include_list)) else False, result)

if exclude:
exclude_list = map(lambda s: s.strip(), exclude.split(','))
result = filter(lambda s: True if all(map(lambda t: _(t) not in _(s['title']),
exclude_list)) else False, result)

result = bgmi.patches.bangumi.main(data=result)
return result


def get_maximum_episode(bangumi, subtitle=True, ignore_old_row=True):
subtitle_group = bangumi.subtitle_group
if subtitle:
followed_obj = Followed(bangumi_name=bangumi.name)
followed_obj.select_obj()
if followed_obj:
subtitle_group = followed_obj.subtitle_group

followed_filter_obj = Filter(bangumi_name=bangumi.name)
followed_filter_obj.select_obj()

subtitle_group = followed_filter_obj.subtitle if followed_filter_obj and subtitle else None
include = followed_filter_obj.include if followed_filter_obj and subtitle else None
exclude = followed_filter_obj.exclude if followed_filter_obj and subtitle else None

data = [i for i in fetch_episode(keyword=bangumi.keyword, name=bangumi.name,
subtitle_group=subtitle_group if subtitle else None)
subtitle_group=subtitle_group,
include=include, exclude=exclude)
if i['episode'] is not None]
if data:
if ignore_old_row:
Expand Down
80 changes: 45 additions & 35 deletions bgmi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import signal
import sqlite3
import time
import platform

import bgmi.config
from bgmi.command import CommandParser
from bgmi.config import BGMI_PATH, DB_PATH, write_config
from bgmi.download import download_prepare
from bgmi.fetch import fetch, bangumi_calendar, get_maximum_episode
from bgmi.models import Bangumi, Followed, Download, STATUS_FOLLOWED, STATUS_UPDATED,\
from bgmi.models import Bangumi, Followed, Download, Filter, STATUS_FOLLOWED, STATUS_UPDATED,\
STATUS_NORMAL, STATUS_NOT_DOWNLOAD
from bgmi.sql import CREATE_TABLE_BANGUMI, CREATE_TABLE_FOLLOWED, CREATE_TABLE_DOWNLOAD
from bgmi.sql import CREATE_TABLE_BANGUMI, CREATE_TABLE_FOLLOWED, CREATE_TABLE_DOWNLOAD, CREATE_TABLE_FOLLOWED_FILTER
from bgmi.utils.utils import print_warning, print_info, print_success, print_error, print_version
from bgmi.download import get_download_class

Expand Down Expand Up @@ -90,9 +91,13 @@ def main():

sub_parser_filter = action.add_sub_parser(ACTION_FILTER, help='Set bangumi fetch filter.')
sub_parser_filter.add_argument('name', required=True, help='Bangumi name to set the filter.')
sub_parser_filter.add_argument('subtitle_group', help='Subtitle group name.')
sub_parser_filter.add_argument('--remove', help='Remove subtitle group filter.')
sub_parser_filter.add_argument('--remove-all', help='Remove all the subtitle group filter.', mutex='--remove')
sub_parser_filter.add_argument('--subtitle', arg_type='1', help='Subtitle group name, split by ",".')
sub_parser_filter.add_argument('--include', arg_type='1',
help='Filter by keywords which in the title, split by ",".')
sub_parser_filter.add_argument('--exclude', arg_type='1',
help='Filter by keywords which not int the title, split by ",".')
# sub_parser_filter.add_argument('--remove', help='Remove subtitle group filter.')
# sub_parser_filter.add_argument('--remove-all', help='Remove all the subtitle group filter.', mutex='--remove')

sub_parser_del = action.add_sub_parser(ACTION_DELETE, help='Unsubscribe bangumi.')
sub_parser_del.add_argument('--name', arg_type='+', mutex='--clear-all', help='Bangumi name to unsubscribe.')
Expand Down Expand Up @@ -172,6 +177,8 @@ def main():
print_info('Fetch bangumi {0} ...'.format(bangumi_obj.name))
_, data = get_maximum_episode(bangumi_obj,
ignore_old_row=False if ret.action.fetch.not_ignore else True)
if not data:
print_warning('Nothing.')
for i in data:
print_success(i['title'])

Expand Down Expand Up @@ -250,33 +257,34 @@ def filter_(ret):
print_error('Bangumi {0} has not subscribed, try \'bgmi add "{1}"\'.'.format(bangumi_obj.name,
bangumi_obj.name))

subtitle = ret.action.filter.subtitle_group
if subtitle:
if not ret.action.filter.remove and not ret.action.filter.remove_all:
if not followed_obj.subtitle_group:
followed_obj.subtitle_group = subtitle
else:
group = followed_obj.subtitle_group.split(',')
for i in subtitle.split(','):
if i not in group:
group.append(i)
followed_obj.subtitle_group = ','.join(group)
elif ret.action.filter.remove:
if followed_obj.subtitle_group:
group = followed_obj.subtitle_group.split(',')
new_group = []
while group:
_ = group.pop()
if _ not in subtitle:
new_group.append(_)
followed_obj.subtitle_group = ','.join(new_group)

if ret.action.filter.remove_all:
followed_obj.subtitle_group = ''

followed_obj.save()
subtitle = ret.action.filter.subtitle
include = ret.action.filter.include
exclude = ret.action.filter.exclude

followed_filter_obj = Filter(bangumi_name=bangumi_obj.name)
followed_filter_obj.select_obj()

if not followed_filter_obj:
followed_filter_obj.save()

if subtitle is not None:
subtitle = map(lambda s: s.strip(), subtitle.split(','))
subtitle = filter(lambda s: True if s in bangumi_obj.subtitle_group.split(', ') else False, subtitle)
subtitle = ', '.join(subtitle)
followed_filter_obj.subtitle = subtitle

if include is not None:
followed_filter_obj.include = include

if exclude is not None:
followed_filter_obj.exclude = exclude

followed_filter_obj.save()

print_info('Usable subtitle group: {0}'.format(bangumi_obj.subtitle_group))
print_info('Added subtitle group: {0}'.format(followed_obj.subtitle_group))
print_success('Added subtitle group: {0}'.format(followed_filter_obj.subtitle))
print_success('Include keywords: {0}'.format(followed_filter_obj.include))
print_success('Exclude keywords: {0}'.format(followed_filter_obj.exclude))


def delete(ret):
Expand Down Expand Up @@ -314,7 +322,6 @@ def update(ret):
print_info('updating subscriptions ...')
download_queue = []


if ret.action.update.name is None:
updated_bangumi_obj = Followed.get_all_followed()
else:
Expand Down Expand Up @@ -428,6 +435,7 @@ def init_db(db_path):
conn.execute(CREATE_TABLE_BANGUMI)
conn.execute(CREATE_TABLE_FOLLOWED)
conn.execute(CREATE_TABLE_DOWNLOAD)
conn.execute(CREATE_TABLE_FOLLOWED_FILTER)
conn.commit()
conn.close()
except sqlite3.OperationalError:
Expand All @@ -439,10 +447,12 @@ def setup():
print_warning('BGMI_PATH %s does not exist, installing' % BGMI_PATH)
from bgmi.setup import create_dir, install_crontab
create_dir()
install_crontab()
if not platform.system() == 'Windows':
# if not input('Do you want to install a crontab to auto-download bangumi?(Y/n): ') == 'n':
install_crontab()

if not os.path.exists(DB_PATH):
init_db(DB_PATH)
# if not os.path.exists(DB_PATH):
init_db(DB_PATH)
main()


Expand Down
8 changes: 7 additions & 1 deletion bgmi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def get_all_bangumi(status=None, order=True):
class Followed(DB):
table = 'followed'
primary_key = ('bangumi_name', )
fields = ('bangumi_name', 'episode', 'status', 'subtitle_group', 'updated_time', )
fields = ('bangumi_name', 'episode', 'status', 'updated_time', )

@staticmethod
def delete_followed(condition=None, batch=True):
Expand Down Expand Up @@ -482,3 +482,9 @@ def get_all_downloads(status=None):
def delete(self, condition=None):
self.status = STATUS_DOWNLOADED
self.save()


class Filter(DB):
table = 'filter'
primary_key = ('bangumi_name', )
fields = ('bangumi_name', 'subtitle', 'include', 'exclude', )
16 changes: 12 additions & 4 deletions bgmi/sql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding=utf-8

CREATE_TABLE_BANGUMI = '''CREATE TABLE bangumi (
CREATE_TABLE_BANGUMI = '''CREATE TABLE IF NOT EXISTS bangumi (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
subtitle_group TEXT NOT NULL,
Expand All @@ -10,21 +10,29 @@
)'''


CREATE_TABLE_FOLLOWED = '''CREATE TABLE followed (
CREATE_TABLE_FOLLOWED = '''CREATE TABLE IF NOT EXISTS followed (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bangumi_name TEXT NOT NULL UNIQUE,
episode INTEGER DEFAULT 0,
status INTEGER DEFAULT 1,
subtitle_group TEXT,
updated_time INTEGER DEFAULT 0
)'''


CREATE_TABLE_DOWNLOAD = '''CREATE TABLE download (
CREATE_TABLE_DOWNLOAD = '''CREATE TABLE IF NOT EXISTS download (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
title TEXT NOT NULL,
episode INTEGER DEFAULT 0,
download TEXT,
status INTEGER DEFAULT 0
)'''


CREATE_TABLE_FOLLOWED_FILTER = '''CREATE TABLE IF NOT EXISTS filter (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bangumi_name TEXT UNIQUE NOT NULL,
subtitle TEXT,
include TEXT,
exclude TEXT
)'''

0 comments on commit f4487f2

Please sign in to comment.