-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-missing-chapters.py
147 lines (96 loc) · 3.38 KB
/
find-missing-chapters.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import requests
import json
import argparse
########################################################
#
# Change These Settings, as desired.
#
########################################################
#Kavita ODPS url (requierd)
odps_url = 'https://example.com/api/opds/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxc'
########################################################
#
# Stop changing here
#
########################################################
# Calculated from odps_url
base_url = odps_url.split('/api')[0]
api_key = odps_url.split('/opds/')[1]
def kauth():
auth_url = base_url+'/api/Plugin/authenticate/?apiKey='+api_key+'&pluginName=Kavita_List'
response = requests.post(auth_url)
token = response.json()['token']
return token
def myuserid(kavita_token):
headers = {'Authorization': f'Bearer {kavita_token}'}
myself_url = base_url+'/api/Users/myself'
response = requests.get(myself_url, headers=headers)
user_id = response.json()['id']
return user_id
def librarySeriesInfo(library_id, kavita_token):
headers = {
'Authorization': f'Bearer {kavita_token}',
'accept': "text/plain",
'Content-Type': "application/json"
}
library_series_url = base_url+'/api/Series/all-v2/?PageNumber=1&PageSize=0'
data = {
"id": 0,
"name": None,
"statements": [
{
"comparison": 0,
"field": 19,
"value": str(library_id),
}
],
"combination": 0,
"sortOptions": {
"sortField": 1,
"isAscending": True,
},
"limitTo": 0
}
response = requests.post(library_series_url, headers=headers, data=json.dumps(data))
library_data = response.json()
return library_data
def seriesVols(series_id, kavita_token):
headers = {'Authorization': f'Bearer {kavita_token}'}
series_vol_url = base_url+'/api/Series/volumes?seriesId='+str(+series_id)
response = requests.get(series_vol_url, headers=headers)
series_vols = response.json()
return series_vols
def findMissingChapters(chapter_list):
sorted_list = sorted(chapter_list, key=float)
integers = [int(float(val)) for val in sorted_list if float(val).is_integer()]
# Create the list of missing integers if there are at least two integers to form a range
if integers:
integers = sorted(set(integers)) # Sort and remove duplicates
missing_integers = set(range(integers[0], integers[-1] + 1)) - set(integers)
else:
missing_integers = set()
return missing_integers
def main():
parser = argparse.ArgumentParser(description="Check Kavita Library for missing chapters")
parser.add_argument("library_id", help="The Kavita Library ID to check")
args = parser.parse_args()
kavita_token = kauth()
print('Missing Chapters: \n\n--------------------------\n')
library_series = librarySeriesInfo(args.library_id, kavita_token)
for series in library_series:
series_id = series['id']
series_name = series['name']
series_vols = seriesVols(int(series_id),kavita_token)
for volume in series_vols:
chapters = volume['chapters']
vol_chapter_numbers = []
for chapter in chapters:
chapter_number = chapter['number']
vol_chapter_numbers.append(chapter_number)
missing_chapters = findMissingChapters(vol_chapter_numbers)
if len(missing_chapters) > 0:
print(series_name)
print(missing_chapters)
print("\n")
if __name__ == "__main__":
main()