forked from LikeToAccess/TMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful.py
110 lines (88 loc) · 2.92 KB
/
restful.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
# -*- coding: utf-8 -*-
# filename : restful.py
# description : API to grab movie links
# author : Ian Ault
# email : liketoaccess@protonmail.com
# date : 05-24-2022
# version : v1.0
# usage : python restful.py
# notes :
# license : MIT
# py version : 3.10.2 (must run on 3.6 or higher)
#==============================================================================
import base64
# import threading
from flask_restful import Resource, Api, reqparse
from flask_cors import CORS
from waitress import serve
from flask import Flask
from download import Download
from settings import *
from scraper import Scraper
from format import Format
app = Flask(__name__)
api = Api(app)
CORS(app, resources={r"*": {"origins": "*"}})
scraper = Scraper()
threads = []
# download = Download()
class Plex(Resource):
def get(self):
# Gets search results from a search term
parser = reqparse.RequestParser()
parser.add_argument("search_term", required=True, type=str, location="args")
args = parser.parse_args()
if not args:
return {"message": "Bad request"}, 400
data = scraper.search(
args["search_term"]
)
if data == 404 or not data:
return {"message": "Page not found"}, 404
return {"message": data}, 200
def post(self):
# Adds media to the server
parser = reqparse.RequestParser()
parser.add_argument("search_term", required=True, type=str, location="args")
parser.add_argument("result_data", required=False, type=str, location="args")
args = parser.parse_args()
if not args:
return {"message": "Bad request"}, 400
# TODO: Make TV Shows work
queue, status = scraper.get_video_url_from_page_link(
args["search_term"]
)
data = args["result_data"] # if the media is a TV Show than the data is useless
# print(queue) # URL (list)
# print(status) # 200
# print(data) # DATA
if status == 404:
return {"message": "Page not found"}, 404
if status == 225:
image_data = base64.b64encode(open("captcha.png", "rb").read()).decode("utf-8")
image_data = f"data:image/png;base64,{image_data}"
return {"message": "Captcha", "data": image_data}, 225
# download = Download(status, data)
for url in queue:
download = Download(url, Format(data).format_file_name())
download.run()
if download.verify(): return {"message": "Created", "data": data}, 201
return {"message": "Gone (failure to verify)"}, 410
class Sample(Resource):
def get(self):
return {"message": "Not implemented"}, 501
def post(self):
return {"message": "Not implemented"}, 501
class Captcha(Resource):
def get(self):
return {"message": "Not implemented"}, 501
def post(self):
return {"message": "Not implemented"}, 501
def main():
api.add_resource(Plex, "/plex")
api.add_resource(Sample, "/sample")
api.add_resource(Captcha, "/captcha")
serve(app, host=HOST, port=PORT)
# app.run()
if __name__ == "__main__":
main()