- Partner Engineer @
@johnliu55tw
不要客氣,請隨時發問
已安裝 Python ≥ 3
已安裝 pip
安裝 pipenv
$ pip install pipenv
git clone https://github.com/johnliu55tw/InnovationChat-20
投影片連結:https://goo.gl/GtAc8o
Wifi: 'K_Square_guest', '12345678'
Client ID
Client Secret
from kkbox_developer_sdk.auth_flow import KKBOXOAuth
# Replace CLIENT_ID and CLIENT_SECRET with yours
auth = KKBOXOAuth(CLIENT_ID, CLIENT_SECRET)
token = auth.fetch_access_token_by_client_credentials()
print(token.access_token)
BQfUdNXcFWiTliaSiTovbQ==
$ pip install kkbox_developer_sdk
import pickle
def get_token(token_file, client_id, client_secret):
"""Helper function for getting the token.
If the specified file exists, try to load it with pickle.
Else use the given client ID and Secret to request a token.
"""
if path.exists(token_file):
with open(token_file, 'rb') as f:
return pickle.load(f)
else:
auth = KKBOXOAuth(client_id, client_secret)
token = auth.fetch_access_token_by_client_credentials()
with open(token_file, 'wb') as f:
pickle.dump(token, f)
return token
from kkbox_developer_sdk.api import KKBOXAPI
kkboxapi = KKBOXAPI(token)
search_results = kkboxapi.search_fetcher.search(
'運動',
types=['playlist'],
terr='TW')
playlists = search_results['playlists']['data']
first = playlists[0]
from pprint import pprint
pprint(first, depth=2)
{'description': '在做有氧運動時...'
'id': 'KnqLLVliEedzFEen54',
'images': [{...}, {...}, {...}],
'owner': {'description': 'House是現今流行樂壇中最重要的樂種...',
'id': 'DZHpWKlqBsC81LL8oy',
'images': [...],
'name': 'DJ Rainbowchild',
'url': 'https://www.kkbox.com/tw/profile/DZHpWKlqBsC81LL8oy'},
'title': '世大運動一動:有氧運動專用勸世舞曲(8.11更新)',
'updated_at': '2017-08-11T12:51:22+00:00',
'url': 'https://event.kkbox.com/content/playlist/KnqLLVliEedzFEen54'}
def search_playlists(token, keyword):
"""Search playlists using the given keyword.
This function returns a list of playlist object directly.
"""
kkboxapi = KKBOXAPI(token)
data = kkboxapi.search_fetcher.search(
keyword,
types=['playlist'],
terr='TW')
return data['playlists']['data']
id
type
terr
lang
autoplay
loop
https://widget.kkbox.com/v1/?param1=value¶m2=value
<iframe width="320" height="470"
src="https://widget.kkbox.com/v1/?id=KnqLLVliEedzFEen54&type=playlist"
</iframe>
播放清單搜尋器
Python + Flask + Jinja2 HTML template engine
No JavaScript!
<div class="w-100 mb-3">
<form action="/" method="get">
<div class="input-group">
<input type="text" name="question" class="form-control"
placeholder="Ask me something about music...">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="submit">
Search
</button>
</div>
</div>
</form>
</div>
<div class="w-100 mb-3 border border-info rounded"
style="height: 80px; overflow: scroll;">
<ul>
{% for record in search_history|reverse %}
<li><b>{{ record.q }}</b>:
{% if record.id %}
<a target="_blank"
href="https://www.kkbox.com/tw/tc/playlist/{{ record.id }}">
{{ record.title }}
</a>
{% else %}
<span>Found nothing...</span>
{% endif %}
</li>
{% endfor %}
</ul>
</div>
<div class="d-flex justify-content-center">
{% if playlist_id %}
<iframe width="320" height="470" frameborder="0" scrolling="no"
src="https://widget.kkbox.com/v1/?id={{playlist_id}}&type=playlist&terr=tw&lang=tc&autoplay=true&loop=true">
</iframe>
{% endif %}
</div>
Configurations
SECRET_KEY
TOKEN_FILE
KKBOX_CLIENT_ID
KKBOX_CLIENT_SECRET
GET /
GET /?question=<value>
app.config.update(SECRET_KEY=urandom(24),
KKBOX_CLIENT_ID='The Client ID',
KKBOX_CLIENT_SECRET='The Client Secret',
TOKEN_FILE='./token.pkl')
@app.route('/', methods=['GET'])
def index():
history = session.setdefault('history', list())
question = request.args.get('question')
if question:
token = get_token(app.config['TOKEN_FILE'],
app.config['KKBOX_CLIENT_ID'],
app.config['KKBOX_CLIENT_SECRET'])
results = search_playlists(token, question)
record = {'q': question,
'title': results[0]['title'] if results else None,
'id': results[0]['id'] if results else None}
history.append(record)
# Manually set to True since list.append won't be a update.
session.modified = True
return render_template('index.html',
search_history=history,
playlist_id=record['id'])
else:
return render_template('index.html',
search_history=history)