-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
171 lines (163 loc) · 6.72 KB
/
index.py
File metadata and controls
171 lines (163 loc) · 6.72 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from flask import Flask, jsonify, request
import requests
import json
import os
from collections import OrderedDict
app = Flask(__name__)
USER_AGENT = os.environ.get('FooBarApp/3.0', 'NamaAplikasiAnda/VersiAnda')
def get_discogs_data(release_id):
headers = {'User-Agent': USER_AGENT}
api_url = f'https://api.discogs.com/releases/{release_id}'
try:
response = requests.get(api_url, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"Terjadi kesalahan saat menghubungi API: {e}")
return None
#@app.errorhandler(404)
def not_found(error):
"""Custom handler untuk error 404, mengembalikan HTML atau JSON."""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Bhang Records - Get Data from Discogs</title>
<style>
body {
background-color: black;
color: white;
display: flex;
justify-content: center; /* Center content horizontally */
align-items: center; /* Center content vertically */
min-height: 100vh; /* Ensure full viewport height */
margin: 0; /* Remove default body margin */
padding: 20px; /* Add some padding around the content */
text-align: center; /* Center text within elements */
font-family: sans-serif; /* Optional: Choose a font */
}
.container {
padding: 40px; /* Add more padding to the container */
border: 1px solid #333; /* Optional: Add a subtle border */
border-radius: 5px; /* Optional: Add rounded corners */
}
a {
color: #aaa; /* Light gray color for links */
text-decoration: none; /* Remove underlines from links */
}
a:hover {
color: white; /* Change link color on hover */
}
pre {
background-color: #222;
padding: 10px;
border-radius: 3px;
overflow: auto; /* Handle long code lines */
}
</style>
</head>
<body>
<div class="container">
<h1>Bhang Records - Get Data from Discogs</h1>
<p>Halaman yang Anda cari tidak ditemukan, namun Anda dapat menggunakan endpoint berikut:</p>
<pre><code>/discogs/release_info/<release_id></code></pre>
<p>Contoh penggunaan:</p>
<ul>
<li>Untuk mendapatkan informasi rilis dengan ID 247822, kunjungi:
<a href="/discogs/release_info/247822">/discogs/release_info/247822</a>
</li>
<li>Contoh lain untuk ID 12345:
<a href="/discogs/release_info/12345">/discogs/release_info/12345</a>
</li>
</ul>
</div>
</body>
</html>
"""
if request.accept_mimetypes.accept_json and not request.accept_mimetypes.accept_html:
response = jsonify({'error': 'Not Found', 'status_code': 404})
response.status_code = 404
return response
return html_content, 404
@app.route('/')
def index():
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Bhang Records - Get Data from Discogs</title>
<style>
body {
background-color: black;
color: white;
display: flex;
justify-content: center; /* Center content horizontally */
align-items: center; /* Center content vertically */
min-height: 100vh; /* Ensure full viewport height */
margin: 0; /* Remove default body margin */
padding: 20px; /* Add some padding around the content */
text-align: center; /* Center text within elements */
font-family: sans-serif; /* Optional: Choose a font */
}
.container {
padding: 40px; /* Add more padding to the container */
border: 1px solid #333; /* Optional: Add a subtle border */
border-radius: 5px; /* Optional: Add rounded corners */
}
a {
color: #aaa; /* Light gray color for links */
text-decoration: none; /* Remove underlines from links */
}
a:hover {
color: white; /* Change link color on hover */
}
pre {
background-color: #222;
padding: 10px;
border-radius: 3px;
overflow: auto; /* Handle long code lines */
}
</style>
</head>
<body>
<div class="container">
<h1>Bhang Records - Get Data from Discogs</h1>
<p>Anda dapat mengambil informasi rilis dari Discogs menggunakan endpoint berikut:</p>
<pre><code>/discogs/release_info/<release_id></code></pre>
<p>Contoh penggunaan:</p>
<ul>
<li>Untuk mendapatkan informasi rilis dengan ID 247822, kunjungi:
<a href="/discogs/release_info/247822">/discogs/release_info/247822</a>
</li>
<li>Contoh lain untuk ID 12345:
<a href="/discogs/release_info/12345">/discogs/release_info/12345</a>
</li>
</ul>
</div>
</body>
</html>
"""
return html_content
@app.route('/discogs/release_info/<int:release_id>', methods=['GET'])
def discogs_release_info(release_id):
release_data = get_discogs_data(release_id)
if release_data:
formatted_data = OrderedDict([
("catalog_number", release_data.get('labels', [{}])[0].get('catno') if release_data.get('labels') else None),
("artist", release_data.get('artists', [{}])[0].get('name') if release_data.get('artists') else None),
("title", release_data.get('title')),
("genre", release_data.get('genres', [])),
("format", [item for format_obj in release_data.get('formats', [])
for item in format_obj.get('descriptions', [])]),
("label", [label.get('name') for label in release_data.get('labels', [])]),
("released_year", release_data.get('year')),
])
response = jsonify(formatted_data)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
else:
response = jsonify({'error': f'Tidak dapat mengambil data rilis dengan ID {release_id}'}), 404
response.headers.add('Access-Control-Allow-Origin', '*')
return response
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)