forked from myxdvz/booktree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyx_mam.py
130 lines (109 loc) · 4.38 KB
/
myx_mam.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import requests
import json
import os
import pickle
from pprint import pprint
import myx_classes
import myx_utilities
#MAM Functions
def searchMAM(cfg, titleFilename, authors, extension):
#Config
session = cfg.get("Config/session")
log_path = cfg.get("Config/log_path")
ebook = bool(cfg.get("Config/flags/ebooks"))
audiobook = not (ebook)
#put paren around authors and titleFilename
if len(authors):
authors = f"({authors})"
if len(titleFilename):
titleFilename = f"({titleFilename})"
search = f'{authors} {titleFilename} {extension} @dummy mamDummy'
#cache results for this search string
cacheKey=myx_utilities.getHash(search)
if myx_utilities.isCached(cacheKey, "mam", cfg):
#this search has been done before, load results from cache
results = myx_utilities.loadFromCache(cacheKey, "mam")
return (results["data"])
else:
#save cookie for future use
cookies_filepath = os.path.join(log_path, 'cookies.pkl')
sess = requests.Session()
#a cookie file exists, use that
if os.path.exists(cookies_filepath):
cookies = pickle.load(open(cookies_filepath, 'rb'))
sess.cookies = cookies
else:
#assume a session ID is passed as a parameter
sess.headers.update({"cookie": f"mam_id={session}"})
#test session and cookie
r = sess.get('https://www.myanonamouse.net/jsonLoad.php', timeout=5) # test cookie
if r.status_code != 200:
raise Exception(f'Error communicating with API. status code {r.status_code} {r.text}')
else:
# save cookies for later
with open(cookies_filepath, 'wb') as f:
pickle.dump(sess.cookies, f)
mam_categories = []
if audiobook:
mam_categories.append(13) #audiobooks
mam_categories.append(16) #radio
if ebook:
mam_categories.append(14)
if not mam_categories:
return None
params = {
"tor": {
"text": search, # The search string.
"srchIn": {
"title": "true",
"author": "true",
"fileTypes": "true",
"filenames": "true"
},
"main_cat": mam_categories
},
"perpage":50
}
try:
r = sess.post('https://www.myanonamouse.net/tor/js/loadSearchJSONbasic.php', json=params)
if r.text == '{"error":"Nothing returned, out of 0"}':
return None
results = r.json()
#cache this result before returning it
myx_utilities.cacheMe(cacheKey, "mam", results, cfg)
return (results["data"])
except Exception as e:
print(f'error searching MAM {e}')
return None
def getMAMBook(cfg, titleFilename="", authors="", extension=""):
books=[]
mamBook=searchMAM(cfg, titleFilename, authors, extension)
if (mamBook is not None):
for b in mamBook:
#pprint(b)
book=myx_classes.Book()
book.init()
if 'asin' in b:
book.asin=str(b["asin"])
if 'title' in b:
book.title=str(b["title"])
if 'author_info'in b:
#format {id:author, id:author}
if len(b["author_info"]):
authors = json.loads(b["author_info"])
for author in authors.values():
book.authors.append(myx_classes.Contributor(str(author)))
if 'series_info'in b:
#format {"35598": ["Kat Dubois", "5"]}
if len(b["series_info"]):
series_info = json.loads(b["series_info"])
for series in series_info.values():
s=list(series)
book.series.append(myx_classes.Series(str(s[0]), s[1]))
if 'lang_code' in b:
book.language=myx_utilities.getLanguage((b["lang_code"]))
if 'my_snatched' in b:
book.snatched=bool((b["my_snatched"]))
if book.snatched:
books.append(book)
return books