1
- from flask import Blueprint , session , request
1
+ from pathlib import Path
2
2
from file_read_backwards import FileReadBackwards
3
+ from flask import Blueprint , session , request , current_app , send_file
3
4
4
5
from library_db .utils .db_utils import (
5
6
get_media ,
10
11
user_query_mini ,
11
12
get_borrower ,
12
13
)
13
- from library_db .utils .utils import is_admin
14
+ from library_db .utils .utils import is_admin , goodreads_search , scrape_goodreads_cover , imdb_search
14
15
from library_db .logger import (
15
16
get_info_logfile ,
16
17
get_error_logfile ,
21
22
22
23
@other_bluep .route ("/media/<int:media_id>" , methods = ["GET" ])
23
24
def media (media_id ):
24
- media_info = get_media (media_id ).__dict__
25
+ media_info = get_media (media_id )
26
+ if not media_info :
27
+ return {"error" : "No media with this ID found" }
28
+
29
+ media_info = media_info .__dict__
25
30
if is_media_borrowed (media_id ):
26
31
media_info .update ({"is_borrowed" : True })
27
32
media_info .update (
@@ -33,10 +38,22 @@ def media(media_id):
33
38
34
39
return media_info
35
40
41
+ @other_bluep .route ("/media/image/<int:media_id>" , methods = ["GET" ])
42
+ def media_image (media_id ):
43
+ media_info = get_media (media_id )
44
+ if not media_info :
45
+ return {"error" : "No media with this ID found" }
46
+
47
+ if media_info .image :
48
+ path = Path (current_app .config ["UPLOAD_FOLDER" ]).joinpath (media_info .image )
49
+ if path .exists ():
50
+ return send_file (path , mimetype = "image/jpeg" )
51
+
52
+ return {"error" : "Image does not exsist for this media." }
36
53
37
54
@other_bluep .route ("/mini_search/author" , methods = ["POST" ])
38
55
def query_authors ():
39
- if not request .is_json and "query" in request .get_json ():
56
+ if not ( request .is_json and "query" in request .get_json () ):
40
57
return {"error" : "Unprocessable data" }, 400
41
58
42
59
query = request .get_json ()["query" ]
@@ -47,7 +64,7 @@ def query_authors():
47
64
48
65
@other_bluep .route ("/mini_search/media" , methods = ["POST" ])
49
66
def query_media ():
50
- if not request .is_json and "query" in request .get_json ():
67
+ if not ( request .is_json and "query" in request .get_json () ):
51
68
return {"error" : "Unprocessable data" }, 400
52
69
53
70
query = request .get_json ()["query" ]
@@ -99,3 +116,37 @@ def get_log():
99
116
response .update ({"log_lines" : lines })
100
117
101
118
return response
119
+
120
+
121
+ @other_bluep .route ("/scraper/search_covers" , methods = ["POST" ])
122
+ def search_covers ():
123
+ if not (request .is_json and "query" in request .get_json () and "media_type" in request .get_json ()):
124
+ return {"error" : "Unprocessable data" }, 400
125
+
126
+ if request .get_json ().get ("media_type" ) == "Book" :
127
+ try :
128
+ urls = goodreads_search (request .get_json ().get ("query" ))
129
+ except :
130
+ return {"error" : "Search failed" }
131
+
132
+ with_images = False
133
+ elif request .get_json ().get ("media_type" ) == "Blu-Ray" or request .get_json ().get ("media_type" ) == "DVD" :
134
+ urls = imdb_search (request .get_json ().get ("query" ))
135
+ with_images = True
136
+ elif request .get_json ().get ("media_type" ) == "CD" :
137
+ return {"error" : "CD-Cover Scraper not Implementet" }
138
+ else :
139
+ return {"error" : "Invalid media type" }
140
+
141
+ return {"with_images" : with_images , "urls" : urls }
142
+
143
+ @other_bluep .route ("/scraper/book/scrape_cover" , methods = ["POST" ])
144
+ def scrape_coverurl ():
145
+ if not (request .is_json and "url" in request .get_json ()):
146
+ return {"error" : "Unprocessable data" }, 400
147
+
148
+ url = scrape_goodreads_cover (request .get_json ().get ("url" ))
149
+ if not url :
150
+ return {"error" : "Could not scrape image url" }
151
+
152
+ return {"image_url" : url }
0 commit comments