-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (88 loc) · 3.77 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import requests
from http import HTTPStatus
import json
host = "localhost"
port = "8080"
def show_entity (entity):
req = requests.get(f"http://{host}:{port}/api/v1/{entity}")
res = json.loads(req.content)
format_res = json.dumps(res, indent=2, ensure_ascii=False)
print(f" -=-=-=--==-=-=-=-=- Lista de {entity} -=-=-=--==-=-=-=-=- ")
print(format_res)
def get_entity_by_id (entity, id):
req = requests.get(f"http://{host}:{port}/api/v1/{entity}/{id}")
return json.loads(req.content)
def show_entity_by_id (entity, id):
res = get_entity_by_id(entity, id)
format_res = json.dumps(res, indent=2, ensure_ascii=False)
print(f" -=-=-=--==-=-=-=-=- Lista de {entity} -=-=-=--==-=-=-=-=- ")
print(format_res)
def insert_data(entity, data):
req = requests.post(f"http://{host}:{port}/api/v1/{entity}", json=data)
return req.status_code == HTTPStatus.CREATED
def remove_data(entity, data):
req = requests.delete(f"http://{host}:{port}/api/v1/{entity}", json=data)
return req.status_code == HTTPStatus.OK
def insert_faixa_in_playlist (id_album, id_play):
while True:
id_faixa = int(input("Informe o número da faixa (-1 voltar): "))
if id_faixa == -1:
return True
meio = input("Informe o meio físico da faixa: ")
if not (insert_data(f"playlists/{id_play}/faixas", {"nroFaixa": id_faixa, "codAlbum": id_album, "meio": meio, "codPlay": id_play})):
return False
def remove_faixa_from_playlist(id_album, id_play):
while True:
id_faixa = int(input("Informe o id da faixa a ser removida (-1 voltar): "))
if id_faixa == -1:
return True
meio = input("Informe o meio físico da faixa: ")
if not (remove_data(f"playlists/{id_play}/faixas", {"nroFaixa": id_faixa, "codAlbum": id_album, "meio": meio, "codPlay": id_play})):
return False
def show_albuns_faixas():
show_entity("albuns")
id_album = int(input("informe o id do álbum: "))
show_entity(f"albuns/{id_album}/faixas")
return id_album
def red_str(str):
return '\033[31m'+str+'\033[0;0m'
def create_playlist ():
id_play = int(input('Informe o identificador da playlist: '))
name = input('Informe o nome da playlist: ')
if not insert_data("playlists", {"id": id_play, "nome": name}):
print(red_str("Não foi possível criar a playlist"))
return
if (input("Adicionar faixa? [S/n]") in ('Ss', '')):
id_album = show_albuns_faixas()
insert_faixa_in_playlist(id_album, id_play)
def delete_playlist ():
id_play = int(input('Informe o identificador da playlist: '))
if not remove_data("playlists", {"id": id_play}):
print(red_str("Não foi possível remover a playlist, verifique se ela ainda possui faixas"))
def manage_playlists():
id_play = int(input("Informe o id da playlist: "))
show_entity(f"playlists/{id_play}/faixas")
op = input("[r]emover ou [i]nserir faixas? (outro p/ voltar)")
if op == 'r':
id_album = int(input("Informe o id do álbum: "))
if not remove_faixa_from_playlist(id_album, id_play):
print(red_str("Não foi possível remover faixa."))
if op == 'i':
id_album = show_albuns_faixas()
if not insert_faixa_in_playlist(id_album, id_play):
print(red_str("Não foi possível inserir faixa."))
else:
return
if __name__ == '__main__':
while True:
show_entity("playlists")
op = input("[c]riar, [a]pagar ou [g]erenciar playlists ([s]air)? ")
if op == 'c':
create_playlist()
elif op == 'a':
delete_playlist()
elif op == 'g':
manage_playlists()
elif op == 's':
print(red_str("Saindo..."))
break