from librb import radiobrowser
from database import SettingsDatabase, MusicDatabase
from media.item import item_builders, item_loaders, item_id_generators, dict_to_item, dicts_to_items
-from media.playlist import get_item_wrapper, get_item_wrapper_by_id, get_item_wrappers_by_tags
+from media.playlist import get_item_wrapper_from_scrap, get_item_wrapper_by_id, get_item_wrappers_by_tags
from media.file import FileItem
from media.url_from_playlist import PlaylistURLItem, get_playlist_info
from media.url import URLItem
# return
if parameter in var.cache.files:
- music_wrapper = get_item_wrapper(bot, type='file', path=parameter, user=user)
+ music_wrapper = get_item_wrapper_from_scrap(bot, type='file', path=parameter, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
url = util.get_url_from_input(parameter)
if url:
- music_wrapper = get_item_wrapper(bot, type='url', url=url, user=user)
+ music_wrapper = get_item_wrapper_from_scrap(bot, type='url', url=url, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
items = get_playlist_info(url=url, start_index=offset, user=user)
if len(items) > 0:
items = var.playlist.extend(list(map(
- lambda item: get_item_wrapper(bot, **item), items)))
+ lambda item: get_item_wrapper_from_scrap(bot, **item), items)))
for music in items:
log.info("cmd: add to playlist: " + music.format_debug_string())
else:
parameter = parameter.split()[0]
url = util.get_url_from_input(parameter)
if url:
- music_wrapper = get_item_wrapper(bot, type='radio', url=url)
+ music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
url = radiobrowser.geturl_byid(parameter)
if url != "-1":
log.info('cmd: Found url: ' + url)
- music_wrapper = get_item_wrapper(bot, type='radio', url=url, name=stationname, user=user)
+ music_wrapper = get_item_wrapper_from_scrap(bot, type='radio', url=url, name=stationname, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.async_download_next()
if 1 <= index <= len(song_shortlist):
kwargs = song_shortlist[index - 1]
kwargs['user'] = user
- music_wrapper = get_item_wrapper(bot, **kwargs)
+ music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
msgs.append("<li><b>{}</b></li>".format(music_wrapper.item().title))
if 1 <= index <= len(song_shortlist):
kwargs = song_shortlist[index - 1]
kwargs['user'] = user
- music_wrapper = get_item_wrapper(bot, **kwargs)
+ music_wrapper = get_item_wrapper_from_scrap(bot, **kwargs)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
bot.send_msg(constants.strings('file_added', item=music_wrapper.format_song_string()), text)
volume = 0.1
# playback mode should be one of "one-shot", "loop", "random", "autoplay"
playback_mode = one-shot
+autoplay_length = 5
# target version, stable or testing (testing need to bot installed with git)
target_version = stable
# it should be one of "one-shot" (remove item once played), "repeat" (looping through the playlist),
# or "random" (randomize the playlist), "autoplay" (randomly grab something from the music library).
# This option will be overridden by value in the database.
+# 'autoplay_length': how many songs the autoplay mode fills the playlist
#playback_mode = one-shot
+#autoplay_length = 5
# target version, stable or testing (testing need to bot installed with git)
# stable will use simple bash with curl command to get releases, testing will follow github master branch with git commands
"WHERE %s" % condition_str, filler).fetchall()
conn.close()
- if len(results) > 0:
- music_dicts = []
- for result in results:
- music_dict = json.loads(result[3])
- music_dict['type'] = result[1]
- music_dict['title'] = result[2]
- music_dict['tags'] = result[4].strip(",").split(",")
- music_dict['id'] = result[0]
- music_dicts.append(music_dict)
-
- return music_dicts
- else:
- return None
+ return self._result_to_dict(results)
def query_music_by_keywords(self, keywords):
condition = []
filler = []
for keyword in keywords:
- condition.append('title LIKE ?')
- filler.append("%{:s}%".format(keyword))
+ condition.append('LOWER(title) LIKE ?')
+ filler.append("%{:s}%".format(keyword.lower()))
condition_str = " AND ".join(condition)
"WHERE %s" % condition_str, filler).fetchall()
conn.close()
- if len(results) > 0:
- music_dicts = []
- for result in results:
- music_dict = json.loads(result[3])
- music_dict['type'] = result[1]
- music_dict['title'] = result[2]
- music_dict['id'] = result[0]
- music_dict['tags'] = result[4].strip(",").split(",")
- if not music_dict['tags'][0]:
- music_dict['tags'] = []
-
- music_dicts.append(music_dict)
-
- return music_dicts
- else:
- return None
+ return self._result_to_dict(results)
def query_music_by_tags(self, tags):
condition = []
filler = []
for tag in tags:
- condition.append('tags LIKE ?')
- filler.append("%{:s},%".format(tag))
+ condition.append('LOWER(tags) LIKE ?')
+ filler.append("%{:s},%".format(tag.lower()))
condition_str = " AND ".join(condition)
"WHERE %s" % condition_str, filler).fetchall()
conn.close()
+ return self._result_to_dict(results)
+
+ def query_tags_by_id(self, id):
+ conn = sqlite3.connect(self.db_path)
+ cursor = conn.cursor()
+ results = cursor.execute("SELECT tags FROM music "
+ "WHERE id=?", (id, )).fetchall()
+ conn.close()
+
+ if len(results) > 0:
+ tags = results[0][0].strip(",").split(",")
+
+ return tags if tags[0] else []
+ else:
+ return None
+
+ def query_random_music(self, count):
+ conn = sqlite3.connect(self.db_path)
+ cursor = conn.cursor()
+ results = cursor.execute("SELECT id, type, title, metadata, tags FROM music "
+ "WHERE id IN (SELECT id FROM music ORDER BY RANDOM() LIMIT ?)", (count,)).fetchall()
+ conn.close()
+
+ return self._result_to_dict(results)
+
+
+ def _result_to_dict(self, results):
if len(results) > 0:
music_dicts = []
for result in results:
else:
return None
- def query_tags_by_id(self, id):
- conn = sqlite3.connect(self.db_path)
- cursor = conn.cursor()
- results = cursor.execute("SELECT tags FROM music "
- "WHERE id=?", (id, )).fetchall()
- conn.close()
-
- if len(results) > 0:
- tags = results[0][0].strip(",").split(",")
-
- return tags if tags[0] else []
- else:
- return None
-
def delete_music(self, **kwargs):
condition = []
filler = []
from werkzeug.utils import secure_filename
import errno
import media
-from media.playlist import get_item_wrapper, get_item_wrapper_by_id, get_item_wrappers_by_tags
+from media.playlist import get_item_wrapper_from_scrap, get_item_wrapper_by_id, get_item_wrappers_by_tags
import logging
import time
elif 'add_url' in request.form:
- music_wrapper = get_item_wrapper(var.bot, type='url', url=request.form['add_url'], user=user)
+ music_wrapper = get_item_wrapper_from_scrap(var.bot, type='url', url=request.form['add_url'], user=user)
var.playlist.append(music_wrapper)
log.info("web: add to playlist: " + music_wrapper.format_debug_string())
elif 'add_radio' in request.form:
url = request.form['add_radio']
- music_wrapper = get_item_wrapper(var.bot, type='radio', url=url, user=user)
+ music_wrapper = get_item_wrapper_from_scrap(var.bot, type='radio', url=url, user=user)
var.playlist.append(music_wrapper)
log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
import variables as var
from media.file import FileItem
+from media.item import dict_to_item
from media.url import URLItem
from media.url_from_playlist import PlaylistURLItem
from media.radio import RadioItem
# Remember!!! Using these three get wrapper functions will automatically add items into the cache!
-def get_item_wrapper(bot, **kwargs):
+def get_item_wrapper_from_scrap(bot, **kwargs):
item = var.cache.get_item(bot, **kwargs)
if 'user' not in kwargs:
raise KeyError("Which user added this song?")
return PlaylistItemWrapper(var.cache, item.id, kwargs['type'], kwargs['user'])
+def get_item_wrapper_from_dict(bot, dict_from_db, user):
+ item = dict_to_item(bot, dict_from_db)
+ var.cache[dict_from_db['id']] = item
+ return PlaylistItemWrapper(var.cache, item.id, item.type, user)
+
def get_item_wrapper_by_id(bot, id, user):
item = var.cache.get_item_by_id(bot, id)
if item:
return self[0]
-class AutoPlaylist(BasePlaylist):
+class AutoPlaylist(OneshotPlaylist):
def __init__(self):
super().__init__()
self.mode = "autoplay"
def refresh(self):
- _list = []
- ids = var.music_db.query_all_ids()
- for _ in range(20):
- _list.append(get_item_wrapper_by_id(var.bot, ids[random.randint(0, len(ids)-1)], 'AutoPlay'))
+ dicts = var.music_db.query_random_music(var.config.getint("bot", "autoplay_length", fallback=5))
+ _list = [get_item_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
self.from_list(_list, -1)
# def from_list(self, _list, current_index):
def next(self):
if len(self) == 0:
self.refresh()
- return False
-
- self.version += 1
+ return super().next()
- if self.current_index < len(self) - 1:
- self.current_index += 1
- return self[self.current_index]
- else:
- self.refresh()
- self.current_index = 0
- return self[0]