-
Notifications
You must be signed in to change notification settings - Fork 1
/
title_metadata.py
105 lines (84 loc) · 2.67 KB
/
title_metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import logging
class TitleMetadata():
def __init__(self):
"""
title: str
Title
title_url: str
Title URLl
description: str
Description
categories: Set[str]
Categories
chapter_numbers: List[str]
Chapter numbers, uses a list because
we care about the order they appear on the site
rather than simple sorting
chapter_urls: List[str]
Urls corresponding to the chapter_numbers
"""
self.title = None
self.title_url = None
self.description = None
self.categories = set()
self.chapter_numbers = []
self.chapter_urls = []
def set_title(self, title):
self.title = title
assert(self.title != "")
def get_title(self):
return self.title
def set_title_url(self, title_url):
self.title_url = title_url
def get_title_url(self):
return self.title_url
def set_description(self, description):
self.description = description
def get_description(self):
return self.description
def add_category(self, category):
self.categories.add(category.lower())
def get_categories(self):
return sorted(list(self.categories))
def add_chapter_number(self, chapter_number, url):
self.chapter_numbers.append(chapter_number)
self.chapter_urls.append(url)
def get_chapter_numbers(self):
return self.chapter_numbers, self.chapter_urls
def get_chapter_number_from_url(self, in_url):
for (ch, url) in zip(self.chapter_numbers, self.chapter_urls):
if url == in_url:
return ch
raise Exception("Unknown url: " + in_url)
def dump(self, log=False):
ret = ""
ret += "Title: " + str(self.title) + "\n"
ret += "==========\n"
if log:
logging.info("Title: " + str(self.title))
logging.info("==========")
ret += "Title URL: " + str(self.title_url) + "\n"
ret += "==========\n"
if log:
logging.info("Title URL: " + str(self.title_url))
logging.info("==========")
ret += "Description: " + str(self.description) + "\n"
ret += "==========\n"
if log:
logging.info("Description: " + str(self.description))
logging.info("==========")
categories_str = ""
for c in sorted(list(self.categories)):
categories_str += "\t" + c + "\n"
ret += "Categories:\n" + categories_str
ret += "==========\n"
if log:
logging.info("Categories:\n" + categories_str)
logging.info("==========")
chapters_str = ""
for n, u in zip(self.chapter_numbers, self.chapter_urls):
chapters_str += "\t" + n + ":" + u + "\n"
ret += "Chapter Numbers:\n" + chapters_str
if log:
logging.info("Chapter Numbers:\n" + chapters_str)
return ret