-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (70 loc) · 2.32 KB
/
main.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
from flask import Flask, request
from threading import Thread
from flask_restful import reqparse, abort, Api, Resource
from ytmusicapi import YTMusic
import os, json
app = Flask('')
api = Api(app)
parser = reqparse.RequestParser()
# Look only in the POST body form data
parser.add_argument('name', type=str, location='form')
# Look only in the querystring
parser.add_argument('task', type=str, help='Please provide a valid task id')
# From the request headers
parser.add_argument('X-API-KEY', type=str, location='headers', required=True)
# Look in multiple locations
parser.add_argument('text', location=['headers', 'values', 'json'])
def authenticated(api_key):
if api_key == os.getenv("THIS_API_KEY_1"):
return True
class Index(Resource):
def get(self):
if next(x for x in ytmusic.get_library_playlists() if x["playlistId"] == 'LM' )['playlistId'] == 'LM':
return {"alive": "yes"}
else:
return {"alive": "no"}
class Likes(Resource):
def get(self):
args = parser.parse_args()
if not authenticated(api_key=args['X-API-KEY']):
abort(401)
return ytmusic.get_playlist(playlistId="LM")
def post(self):
args = parser.parse_args()
if not authenticated(api_key=args['X-API-KEY']):
abort(401)
json_message = json.loads(request.data)
try:
track = json_message['track']
except:
abort(400)
try:
artist = json_message['artist']
except:
abort(400)
if type(track) == str and type(artist) == str:
search_results = ytmusic.search(track + " - " + artist)
try:
like = ytmusic.rate_song(videoId=search_results[0]['videoId'], rating='LIKE')
except:
abort(500)
if like['actions'][0]['addToToastAction']['item']['notificationActionRenderer']['responseText']['runs'][0]['text'] == 'Added to your likes':
return {"status": "added to likes"}
else:
abort(400)
class Poster(Resource):
def post(self):
args = parser.parse_args()
if not authenticated(api_key=args['X-API-KEY']):
abort(401)
return args
#creating api endpoint
api.add_resource(Index, '/')
api.add_resource(Likes, '/likes')
api.add_resource(Poster, '/poster')
def run():
app.run(host='0.0.0.0',port=7210)
t = Thread(target=run)
# other code
ytmusic = YTMusic(os.getenv("YTMUSIC_COOKIE_JSON"))
t.start()