<commit_before># encoding: utf-8
from __future__ import print_function, unicode_literals
import os
import json
import datetime
import hashlib
import tornado.ioloop
import tornado.options
import tornado.httpserver
import tornado.web
import tornado.template
from tornado.options import options, define
from icalendar import Calendar, Event, vText

from collections import OrderedDict, defaultdict
from bgmi import __version__
from bgmi.config import BGMI_SAVE_PATH, DB_PATH, DANMAKU_API_URL, COVER_URL
from bgmi.models import Download, Bangumi, Followed, STATUS_NORMAL, STATUS_UPDATING, STATUS_END


WEEK = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
define('port', default=8888, help='listen on the port', type=int)
define('address', default='0.0.0.0', help='binding at given address', type=str)


def md5(_, string):
    return hashlib.md5(string.encode('utf-8')).hexdigest()


def make_dicts(cursor, row):
    return dict((cursor.description[idx][0], value)
                for idx, value in enumerate(row))


class BangumiHandler(tornado.web.RequestHandler):
    def get(self, _):
        self.set_header('Content-Type', 'text/html')
        self.write('<h1>BGmi HTTP Service</h1>')
        self.write('<pre>Please modify your web server configure file\n'
                   'to server this path to \'%s\'.\n'
                   'e.g.\n\n'
                   '...\n'
                   'autoindex on;\n'
                   'location /bangumi {\n'
                   '    alias %s;\n'
                   '}\n'
                   '...\n</pre>' % (BGMI_SAVE_PATH, BGMI_SAVE_PATH)
                   )
        self.finish()


class BangumiPlayerHandler(tornado.web.RequestHandler):
    def get(self, bangumi_name):
        data = Followed(bangumi_name=bangumi_name)
        data.select_obj()

        bangumi = Bangumi(name=bangumi_name)
        bangumi.select_obj()

        if not data:
            return self.write_error(404)
        episode_list = {}
        bangumi_path = os.path.join(BGMI_SAVE_PATH, bangumi_name)
        for root, _, files in os.walk(bangumi_path):
            if not _ and files:
                _ = root.replace(bangumi_path, '').split('/')
                base_path = root.replace(BGMI_SAVE_PATH, '')
                if len(_) >= 2:
                    episode_path = root.replace(os.path.join(BGMI_SAVE_PATH, bangumi_name), '')
                    episode = int(episode_path.split('/')[1])
                else:
                    episode = -1

                for bangumi in files:
                    if bangumi.lower().endswith('.mp4'):
                        episode_list[episode] = {'path': os.path.join(base_path, bangumi)}
                        break

        if not episode_list:
            self.write('_(:3 There are nothing to play, please try again later.')
            self.finish()
        else:
            self.render('templates/dplayer.html', bangumi=episode_list, bangumi_name=bangumi_name,
                        bangumi_cover=bangumi['cover'], DANMAKU_URL=DANMAKU_API_URL)


class ImageCSSHandler(tornado.web.RequestHandler):
    def get(self):
        data = Followed.get_all_followed(status=None, bangumi_status=None)
        self.set_header('Content-Type', 'text/css; charset=utf-8')
        self.render('templates/image.css', data=data, image_url=COVER_URL)


class RssHandler(tornado.web.RequestHandler):
    def get(self):
        data = Download.get_all_downloads()
        self.set_header('Content-Type', 'text/xml')
        self.render('templates/download.xml', data=data)


class CalendarHandler(tornado.web.RequestHandler):
    def get(self):
        type_ = self.get_argument('type', 0)

        cal = Calendar()
        cal.add('prodid', '-//BGmi Followed Bangumi Calendar//bangumi.ricterz.me//')
        cal.add('version', '2.0')<commit_msg>fix bug<commit_after># encoding: utf-8
from __future__ import print_function, unicode_literals
import os
import json
import datetime
import hashlib
import tornado.ioloop
import tornado.options
import tornado.httpserver
import tornado.web
import tornado.template
from tornado.options import options, define
from icalendar import Calendar, Event, vText

from collections import OrderedDict, defaultdict
from bgmi import __version__
from bgmi.config import BGMI_SAVE_PATH, DB_PATH, DANMAKU_API_URL, COVER_URL
from bgmi.models import Download, Bangumi, Followed, STATUS_NORMAL, STATUS_UPDATING, STATUS_END


WEEK = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')
define('port', default=8888, help='listen on the port', type=int)
define('address', default='0.0.0.0', help='binding at given address', type=str)


def md5(_, string):
    return hashlib.md5(string.encode('utf-8')).hexdigest()


def make_dicts(cursor, row):
    return dict((cursor.description[idx][0], value)
                for idx, value in enumerate(row))


class BangumiHandler(tornado.web.RequestHandler):
    def get(self, _):
        self.set_header('Content-Type', 'text/html')
        self.write('<h1>BGmi HTTP Service</h1>')
        self.write('<pre>Please modify your web server configure file\n'
                   'to server this path to \'%s\'.\n'
                   'e.g.\n\n'
                   '...\n'
                   'autoindex on;\n'
                   'location /bangumi {\n'
                   '    alias %s;\n'
                   '}\n'
                   '...\n</pre>' % (BGMI_SAVE_PATH, BGMI_SAVE_PATH)
                   )
        self.finish()


class BangumiPlayerHandler(tornado.web.RequestHandler):
    def get(self, bangumi_name):
        data = Followed(bangumi_name=bangumi_name)
        data.select_obj()

        bangumi_obj = Bangumi(name=bangumi_name)
        bangumi_obj.select_obj()

        if not data:
            return self.write_error(404)
        episode_list = {}
        bangumi_path = os.path.join(BGMI_SAVE_PATH, bangumi_name)
        for root, _, files in os.walk(bangumi_path):
            if not _ and files:
                _ = root.replace(bangumi_path, '').split('/')
                base_path = root.replace(BGMI_SAVE_PATH, '')
                if len(_) >= 2:
                    episode_path = root.replace(os.path.join(BGMI_SAVE_PATH, bangumi_name), '')
                    episode = int(episode_path.split('/')[1])
                else:
                    episode = -1

                for bangumi in files:
                    if bangumi.lower().endswith('.mp4'):
                        episode_list[episode] = {'path': os.path.join(base_path, bangumi)}
                        break

        if not episode_list:
            self.write('_(:3 There are nothing to play, please try again later.')
            self.finish()
        else:
            self.render('templates/dplayer.html', bangumi=episode_list, bangumi_name=bangumi_name,
                        bangumi_cover=bangumi_obj['cover'], DANMAKU_URL=DANMAKU_API_URL)


class ImageCSSHandler(tornado.web.RequestHandler):
    def get(self):
        data = Followed.get_all_followed(status=None, bangumi_status=None)
        self.set_header('Content-Type', 'text/css; charset=utf-8')
        self.render('templates/image.css', data=data, image_url=COVER_URL)


class RssHandler(tornado.web.RequestHandler):
    def get(self):
        data = Download.get_all_downloads()
        self.set_header('Content-Type', 'text/xml')
        self.render('templates/download.xml', data=data)


class CalendarHandler(tornado.web.RequestHandler):
    def get(self):
        type_ = self.get_argument('type', 0)

        cal = Calendar()
        cal.add('prodid', '-//BGmi Followed Bangumi Calendar//bangumi.ricterz.me//')
        cal.add('version', '2.0')
{'fraction_non_alphanumeric': 0.1228713649462929, 'fraction_numerical': 0.00707361802462667, 'mean_word_length': 2.433003597122302, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1310111', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2343, 'n_words': 500}