-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.py
66 lines (59 loc) · 2.27 KB
/
example.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
import asyncio
from os import getenv
from time import time
from typing import List, Union, cast
from FB2 import FictionBook2
from FB2.Author import Author
from httpx import AsyncClient, Timeout
from Classes.Book import Book
from Classes.Dataclasses import Pages
from Classes.Functions import Authorize, Logoff, SetSessionHeaders
async def main():
async with AsyncClient(base_url=Pages.main) as client:
client._timeout = Timeout(3)
SetSessionHeaders(client)
t = time()
password = getenv('AUTHOR_TODAY_PASSWORD')
email = getenv('AUTHOR_TODAY_EMAIL')
if await Authorize(client, email, password):
print(f"Authorized as {email}")
book: Book = Book(client)
await book.GetBookFromUrl("work/40323")
print(book.header)
print(
"-----------------\nTable of Contents\n-----------------",
end="\n",
)
for chapterHeader in book.header.tableOfContents:
print(chapterHeader)
fb2 = FictionBook2()
fb2.titleInfo.title = book.header.title
fb2.titleInfo.authors = cast(
List[Union[str, Author]],
book.header.authors,
)
fb2.titleInfo.annotation = book.header.annotation
fb2.titleInfo.genres = book.header.genres
fb2.titleInfo.lang = "ru"
fb2.titleInfo.sequences = (
[(book.header.sequence.name, book.header.sequence.number)]
if book.header.sequence
else None
)
fb2.titleInfo.keywords = book.header.tags
fb2.titleInfo.coverPageImages = (
[book.header.coverImageData]
if book.header.coverImageData
else None
)
fb2.titleInfo.date = (book.header.publicationDate, None)
fb2.chapters = list(
map(
lambda chapter: (chapter.header.title, chapter.paragraphs),
book.chapters,
)
)
fb2.write(f"./Output/{fb2.titleInfo.title}.fb2")
await Logoff(client)
print(f"All requests took {time() - t} seconds.")
asyncio.get_event_loop().run_until_complete(main())