rominf commited on
Commit
b681b53
0 Parent(s):

initial commit

Browse files
Files changed (3) hide show
  1. .gitattributes +54 -0
  2. README.md +68 -0
  3. flibusta.py +172 -0
.gitattributes ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.lz4 filter=lfs diff=lfs merge=lfs -text
12
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
13
+ *.model filter=lfs diff=lfs merge=lfs -text
14
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
15
+ *.npy filter=lfs diff=lfs merge=lfs -text
16
+ *.npz filter=lfs diff=lfs merge=lfs -text
17
+ *.onnx filter=lfs diff=lfs merge=lfs -text
18
+ *.ot filter=lfs diff=lfs merge=lfs -text
19
+ *.parquet filter=lfs diff=lfs merge=lfs -text
20
+ *.pb filter=lfs diff=lfs merge=lfs -text
21
+ *.pickle filter=lfs diff=lfs merge=lfs -text
22
+ *.pkl filter=lfs diff=lfs merge=lfs -text
23
+ *.pt filter=lfs diff=lfs merge=lfs -text
24
+ *.pth filter=lfs diff=lfs merge=lfs -text
25
+ *.rar filter=lfs diff=lfs merge=lfs -text
26
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
27
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ # Audio files - uncompressed
37
+ *.pcm filter=lfs diff=lfs merge=lfs -text
38
+ *.sam filter=lfs diff=lfs merge=lfs -text
39
+ *.raw filter=lfs diff=lfs merge=lfs -text
40
+ # Audio files - compressed
41
+ *.aac filter=lfs diff=lfs merge=lfs -text
42
+ *.flac filter=lfs diff=lfs merge=lfs -text
43
+ *.mp3 filter=lfs diff=lfs merge=lfs -text
44
+ *.ogg filter=lfs diff=lfs merge=lfs -text
45
+ *.wav filter=lfs diff=lfs merge=lfs -text
46
+ # Image files - uncompressed
47
+ *.bmp filter=lfs diff=lfs merge=lfs -text
48
+ *.gif filter=lfs diff=lfs merge=lfs -text
49
+ *.png filter=lfs diff=lfs merge=lfs -text
50
+ *.tiff filter=lfs diff=lfs merge=lfs -text
51
+ # Image files - compressed
52
+ *.jpg filter=lfs diff=lfs merge=lfs -text
53
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
54
+ *.webp filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ task_categories:
4
+ - text-generation
5
+ language:
6
+ - ru
7
+ tags:
8
+ - art
9
+ - not-for-all-audiences
10
+ size_categories:
11
+ - 100K<n<1M
12
+ ---
13
+ # Dataset Card for Dataset Name
14
+
15
+ ### Dataset Summary
16
+
17
+ Convenient access to books in Russian hosted on Flibusta (https://flibusta.is/).
18
+
19
+ Authors of the dataset do not endorse the usage of Flibusta for illegal purposes: please read "Licensing Information" before use.
20
+
21
+ You can load the Flibusta subset by searching by book title like this:
22
+
23
+ ```
24
+ from datasets import load_dataset
25
+
26
+ war_and_peace_flibusta = load_dataset("rominf/flibusta", books_query="Война и мир")
27
+ ```
28
+
29
+ ### Languages
30
+
31
+ Russian.
32
+
33
+ ## Dataset Structure
34
+
35
+ ### Data Instances
36
+
37
+ An example looks as follows:
38
+
39
+ ```
40
+ {
41
+ 'author': 'Толстой Лев Николаевич',
42
+ 'id': '169984',
43
+ 'text': 'Том первый...',
44
+ 'title': 'Война и мир. Книга 1',
45
+ 'url': 'https://flibusta.is/b/169984',
46
+ 'url_txt': 'https://flibusta.is/b/169984/txt',
47
+ }
48
+ ```
49
+
50
+ ## Additional Information
51
+
52
+ ### Licensing Information
53
+
54
+ Books are stored on https://flibusta.is/ and may not be accessible from your location because of legal reasons.
55
+
56
+ Please check with your local law if you can use this dataset.
57
+
58
+ The license Apache 2.0 applies only to the code.
59
+
60
+ ### Citation Information
61
+
62
+ ```
63
+ @ONLINE{flibusta,
64
+ author = "Флибуста",
65
+ title = "Флибуста",
66
+ url = "https://flibusta.is"
67
+ }
68
+ ```
flibusta.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dataclasses
2
+ import re
3
+ import urllib.parse
4
+ import xml.etree.ElementTree
5
+ import zipfile
6
+ from dataclasses import dataclass
7
+ from textwrap import dedent
8
+
9
+ import datasets
10
+
11
+
12
+ logger = datasets.logging.get_logger(__name__)
13
+
14
+
15
+ FLIBUSTA_URL = "https://flibusta.is"
16
+
17
+
18
+ _CITATION = """\
19
+ @ONLINE{flibusta,
20
+ author = "Флибуста",
21
+ title = "Флибуста",
22
+ url = "https://flibusta.is"
23
+ }
24
+ """
25
+
26
+
27
+ _DESCRIPTION = """\
28
+ Convenient access to books in Russian hosted on Flibusta (https://flibusta.is/).
29
+
30
+ Authors of the dataset do not endorse the usage of Flibusta for illegal
31
+ purposes: please read "Licensing Information" before use.
32
+ """
33
+
34
+
35
+ _LICENSE = """\
36
+ Books are stored on https://flibusta.is/ and may not be accessible from your
37
+ location because of legal reasons.
38
+
39
+ Please check with your local law if you can use this dataset.
40
+
41
+ The license Apache 2.0 applies only to the code.
42
+ """
43
+
44
+
45
+ @dataclass
46
+ class BookInfo:
47
+ author: str
48
+ id: str
49
+ title: str
50
+ url: str
51
+ url_txt: str
52
+
53
+
54
+ @dataclass
55
+ class DownloadedBookInfo:
56
+ book_info: BookInfo
57
+ file_path: str
58
+
59
+
60
+ class FlibustaConfig(datasets.BuilderConfig):
61
+ def __init__(self, *args, books_query, **kwargs):
62
+ super().__init__(*args, version=datasets.utils.Version("0.1.0"), **kwargs)
63
+ self.books_query = books_query
64
+
65
+
66
+ class Flibusta(datasets.GeneratorBasedBuilder):
67
+ BUILDER_CONFIG_CLASS = FlibustaConfig
68
+
69
+ def _info(self):
70
+ return datasets.DatasetInfo(
71
+ citation=_CITATION,
72
+ description=_DESCRIPTION,
73
+ features=datasets.Features(
74
+ {
75
+ "author": datasets.Value("string"),
76
+ "id": datasets.Value("string"),
77
+ "text": datasets.Value("string"),
78
+ "title": datasets.Value("string"),
79
+ "url": datasets.Value("string"),
80
+ "url_txt": datasets.Value("string"),
81
+ }
82
+ ),
83
+ homepage=FLIBUSTA_URL,
84
+ version=datasets.Version("0.1.0"),
85
+ )
86
+
87
+ @staticmethod
88
+ def _get_books(dl_manager, query):
89
+ flibusta_search_url = urllib.parse.urljoin(FLIBUSTA_URL, "opds/search")
90
+ continue_traverse = True
91
+ page = 0
92
+ while continue_traverse:
93
+ params_str = urllib.parse.urlencode(
94
+ {
95
+ "searchType": "books",
96
+ "searchTerm": query,
97
+ "pageNumber": page,
98
+ }
99
+ )
100
+ url = f"{flibusta_search_url}?{params_str}"
101
+ logger.info("Extracting book infos from: %s", url)
102
+ search_xml_file_path = dl_manager.download(url)
103
+ search_xml_str = open(search_xml_file_path, encoding="utf8").read()
104
+ search_xml_tree = xml.etree.ElementTree.fromstring(search_xml_str)
105
+ ns = "{http://www.w3.org/2005/Atom}"
106
+ for entry in search_xml_tree.findall(f"./{ns}entry"):
107
+ author = entry.findtext(f"./{ns}author/{ns}name")
108
+ title = entry.findtext(f"./{ns}title")
109
+ url_path_entry = entry.find(f'./{ns}link[@title="Книга на сайте"]')
110
+ url_txt_path_entry = entry.find(
111
+ f'./{ns}link[@type="application/txt+zip"]'
112
+ )
113
+ if (
114
+ author
115
+ and title
116
+ and url_path_entry is not None
117
+ and url_txt_path_entry is not None
118
+ ):
119
+ url_path = url_path_entry.get("href")
120
+ url = urllib.parse.urljoin(FLIBUSTA_URL, url_path)
121
+ url_txt_path = url_txt_path_entry.get("href")
122
+ url_txt = urllib.parse.urljoin(FLIBUSTA_URL, url_txt_path)
123
+ id_ = url_path.rpartition("/")[2]
124
+ yield BookInfo(
125
+ author=author,
126
+ id=id_,
127
+ title=title,
128
+ url=url,
129
+ url_txt=url_txt,
130
+ )
131
+ if search_xml_tree.find(f'./{ns}link[@rel="next"]') is not None:
132
+ page += 1
133
+ else:
134
+ continue_traverse = False
135
+
136
+ def _split_generators(self, dl_manager):
137
+ book_infos = list(
138
+ self._get_books(dl_manager=dl_manager, query=self.config.books_query)
139
+ )
140
+ book_urls_txt = {book_info.id: book_info.url_txt for book_info in book_infos}
141
+ book_file_paths = dl_manager.download(book_urls_txt)
142
+ downloaded_book_infos = [
143
+ DownloadedBookInfo(
144
+ book_info=book_info, file_path=book_file_paths[book_info.id]
145
+ )
146
+ for book_info in book_infos
147
+ ]
148
+ return [
149
+ datasets.SplitGenerator(
150
+ name=datasets.Split.TRAIN,
151
+ gen_kwargs={"downloaded_book_infos": downloaded_book_infos},
152
+ )
153
+ ]
154
+
155
+ def _generate_examples(self, downloaded_book_infos):
156
+ for downloaded_book_info in downloaded_book_infos:
157
+ archive = zipfile.ZipFile(open(downloaded_book_info.file_path, "rb"))
158
+ for content_name in archive.namelist():
159
+ if content_name.endswith(".txt"):
160
+ txt_path = zipfile.Path(archive, at=content_name)
161
+ text = txt_path.read_text(encoding="utf8")
162
+
163
+ text = re.sub(r"Взято из Флибусты,.*", "", text)
164
+ text = text.strip("\n")
165
+ text = dedent(text)
166
+
167
+ example = {
168
+ "text": text,
169
+ **dataclasses.asdict(downloaded_book_info.book_info),
170
+ }
171
+ yield downloaded_book_info.book_info.id, example
172
+ break