add server code

This commit is contained in:
geoffrey45
2021-12-14 09:02:02 +03:00
parent 951d58623b
commit 803c813786
14 changed files with 1680 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
from flask import Flask
from flask_cors import CORS
def create_app():
app = Flask(__name__)
CORS(app)
from . import api
app.register_blueprint(api.bp, url_prefix='/')
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)
Binary file not shown.
+335
View File
@@ -0,0 +1,335 @@
import os
from re import sub
import requests
import urllib
import time
from progress.bar import Bar
from mutagen.flac import MutagenError
from flask import Blueprint, request, send_from_directory
from app.models import AllSongs, Folders, Artists
from app.configs import default_configs
from app.helpers import (
all_songs_instance,
convert_one_to_json,
getTags,
convert_to_json,
remove_duplicates,
save_image,
isValidFile,
create_config_dir,
extract_thumb,
home_dir, app_dir,
run_fast_scandir
)
bp = Blueprint('api', __name__, url_prefix='')
artist_instance = Artists()
folder_instance = Folders()
def main_whatever():
create_config_dir()
main_whatever()
@bp.route('/search')
def search_by_title():
if not request.args.get('q'):
query = "mexican girl"
else:
query = request.args.get('q')
songs = all_songs_instance.find_song_by_title(query)
all_songs = convert_to_json(songs)
albums = all_songs_instance.find_songs_by_album(query)
all_songs.append(convert_to_json(albums))
artists = all_songs_instance.find_songs_by_artist(query)
all_songs.append(convert_to_json(artists))
songs = remove_duplicates(all_songs)
return {'songs': songs}
@bp.route('/populate')
def populate():
sub_dirs, files = run_fast_scandir(home_dir, [".flac", ".mp3"])
bar = Bar('Processing', max=len(files))
for file in files:
file_in_db_obj = all_songs_instance.find_song_by_path(file)
song_obj = convert_one_to_json(file_in_db_obj)
try:
image = song_obj['image']
except:
image = None
if image is None:
try:
getTags(file)
except MutagenError:
pass
if image is not None and not os.path.exists(image):
extract_thumb(file)
bar.next()
bar.finish()
# dirs = []
# files = []
# for dir in sub_dirs:
# files_in_dir = run_fast_scandir(dir, [".flac", ".mp3"])[1]
# if len(files_in_dir) != 0:
# dir_content = os.scandir(dir)
# for entry in dir_content:
# dirs = []
# files = []
# if entry.is_dir() and not entry.name.startswith('.'):
# print(dir)
# files_in_dir = run_fast_scandir(entry.path, [".flac", ".mp3"])[1]
# if len(files_in_dir) != 0:
# dir_data = {
# "name": entry.name,
# "count": len(files_in_dir),
# "path": entry.path.replace(home_dir, "")
# }
# dirs.append(dir_data)
# if entry.is_file():
# if isValidFile(entry.name) == True:
# files.append(entry.path)
# print(dirs)
# return {"info": ''}
@bp.route('/file/<file_id>')
def send_audio(file_id):
song_obj = all_songs_instance.get_song_by_id(file_id)
loaded_song = convert_one_to_json(song_obj)
filepath = loaded_song['filepath'].split('/')[-1]
print(loaded_song['folder'] + filepath)
return send_from_directory(home_dir + loaded_song['folder'], filepath)
@bp.route("/folder/artists")
def get_folder_artists():
dir = request.args.get('dir')
songs = all_songs_instance.find_songs_by_folder(dir)
songs_array = convert_to_json(songs)
without_duplicates = remove_duplicates(songs_array)
artists = []
for song in without_duplicates:
this_artists = song['artists'].split(', ')
for artist in this_artists:
if artist not in artists:
artists.append(artist)
final_artists = []
for artist in artists[:15]:
artist_obj = artist_instance.find_artists_by_name(artist)
if artist_obj != []:
final_artists.append(convert_to_json(artist_obj))
return {'artists': final_artists}
@bp.route("/populate/images")
def populate_images():
all_songs = all_songs_instance.get_all_songs()
songs_array = convert_to_json(all_songs)
remove_duplicates(songs_array)
artists = []
for song in songs_array:
this_artists = song['artists'].split(', ')
for artist in this_artists:
if artist not in artists:
artists.append(artist)
bar = Bar('Processing images', max=len(artists))
for artist in artists:
file_path = app_dir + '/images/artists/' + artist + '.jpg'
if not os.path.exists(file_path):
url = 'https://api.deezer.com/search/artist?q={}'.format(artist)
response = requests.get(url)
data = response.json()
try:
image_path = data['data'][0]['picture_xl']
except:
image_path = None
if image_path is not None:
try:
save_image(image_path, file_path)
artist_obj = {
'name': artist
}
artist_instance.insert_artist(artist_obj)
except:
pass
else:
pass
bar.next()
bar.finish()
artists_in_db = artist_instance.get_all_artists()
artists_in_db_array = convert_to_json(artists_in_db)
return {'sample': artists_in_db_array[:25]}
@bp.route("/artist")
def getArtistData():
artist = urllib.parse.unquote(request.args.get('q'))
artist_obj = artist_instance.find_artists_by_name(artist)
artist_obj_json = convert_to_json(artist_obj)
def getArtistSongs():
songs = all_songs_instance.find_songs_by_artist(artist)
songs_array = convert_to_json(songs)
return songs_array
artist_songs = getArtistSongs()
songs = remove_duplicates(artist_songs)
def getArtistAlbums():
artist_albums = []
albums_with_count = []
albums = all_songs_instance.find_songs_by_album_artist(artist)
albums_array = convert_to_json(albums)
for song in songs:
song['artists'] = song['artists'].split(', ')
for song in albums_array:
if song['album'] not in artist_albums:
artist_albums.append(song['album'])
for album in artist_albums:
count = 0
length = 0
for song in artist_songs:
if song['album'] == album:
count = count + 1
length = length + song['length']
album_ = {
"title": album,
"count": count,
"length": length
}
albums_with_count.append(album_)
return albums_with_count
return {'artist': artist_obj_json, 'songs': songs, 'albums': getArtistAlbums()}
@bp.route("/")
def getFolderTree():
start = time.time()
req_dir = request.args.get('f')
if req_dir is not None:
requested_dir = home_dir + req_dir
else:
requested_dir = home_dir
dir_content = os.scandir(requested_dir)
folders = []
files = []
for entry in dir_content:
if entry.is_dir() and not entry.name.startswith('.'):
files_in_dir = run_fast_scandir(entry.path, [".flac", ".mp3"])[1]
if len(files_in_dir) != 0:
dir = {
"name": entry.name,
"count": len(files_in_dir),
"path": entry.path.replace(home_dir, "")
}
folders.append(dir)
if entry.is_file():
if isValidFile(entry.name) == True:
songs_array = all_songs_instance.find_songs_by_folder(req_dir)
songs = convert_to_json(songs_array)
for song in songs:
song['artists'] = song['artists'].split(', ')
files = songs
for file in files:
del file['filepath']
dir_content.close()
end = time.time()
print(end - start)
return {"requested": req_dir, "files": files[:25], "folders": folders}
@bp.route('/image/<img_type>/<image_id>')
def send_image(img_type, image_id):
if img_type == "thumbnail":
song_obj = all_songs_instance.get_song_by_id(image_id)
loaded_song = convert_one_to_json(song_obj)
img_dir = app_dir + "/images/thumbnails"
image = loaded_song['image']
if img_type == "artist":
artist_obj = artist_instance.get_artist_by_id(image_id)
artist = convert_one_to_json(artist_obj)
img_dir = app_dir + "/images/artists"
image = artist['name'] + ".jpg"
print(img_dir + image)
return send_from_directory(img_dir, image)
+6
View File
@@ -0,0 +1,6 @@
default_configs = {
"dirs": [
"/home/cwilvx/Music/",
"/home/cwilvx/FreezerMusic"
]
}
+262
View File
@@ -0,0 +1,262 @@
from genericpath import exists
import os
import json
import requests
import urllib
from mutagen.mp3 import MP3
from mutagen.id3 import ID3
from mutagen.flac import FLAC
from bson import json_util
from io import BytesIO
from PIL import Image
from app.models import AllSongs
from app.configs import default_configs
all_songs_instance = AllSongs()
music_dir = os.environ.get("music_dir")
music_dirs = os.environ.get("music_dirs")
home_dir = os.path.expanduser('~')
app_dir = home_dir + '/.shit'
PORT = os.environ.get("PORT")
def run_fast_scandir(dir, ext):
subfolders = []
files = []
for f in os.scandir(dir):
if f.is_dir() and not f.name.startswith('.'):
subfolders.append(f.path)
if f.is_file():
if os.path.splitext(f.name)[1].lower() in ext:
files.append(f.path)
for dir in list(subfolders):
sf, f = run_fast_scandir(dir, ext)
subfolders.extend(sf)
files.extend(f)
return subfolders, files
def extract_thumb(path):
img_path = app_dir + "/images/thumbnails/" + path.split('/')[-1] + '.jpg'
if os.path.exists(img_path):
return path.split('/')[-1] + '.jpg'
if path.endswith('.flac'):
audio = FLAC(path)
try:
album_art = audio.pictures[0].data
except IndexError:
album_art = None
elif path.endswith('.mp3'):
audio = ID3(path)
try:
album_art = audio.getall('APIC')[0].data
except IndexError:
album_art = None
if album_art is not None:
img = Image.open(BytesIO(album_art))
try:
img.save(img_path, 'JPEG')
except OSError:
try:
img.convert('RGB'.save(img_path, 'JPEG'))
except:
img_path = None
return path.split('/')[-1] + '.jpg'
def getTags(full_path):
if full_path.endswith('.flac'):
audio = FLAC(full_path)
elif full_path.endswith('.mp3'):
audio = MP3(full_path)
try:
artists = audio['artist'][0]
except KeyError:
try:
artists = audio['TPE1'][0]
except:
artists = 'Unknown'
except IndexError:
artists = 'Unknown'
try:
album_artist = audio['albumartist'][0]
except KeyError:
try:
album_artist = audio['TPE2'][0]
except:
album_artist = 'Unknown'
except IndexError:
album_artist = 'Unknown'
try:
title = audio['title'][0]
except KeyError:
try:
title = audio['TIT2'][0]
except:
title = 'Unknown'
except IndexError:
title = 'Unknown'
try:
album = audio['album'][0]
except KeyError:
try:
album = audio['TALB'][0]
except:
album = "Unknown"
except IndexError:
album = "Unknown"
try:
genre = audio['genre'][0]
except KeyError:
try:
genre = audio['TCON'][0]
except:
genre = "Unknown"
except IndexError:
genre = "Unknown"
img_path = extract_thumb(full_path)
tags = {
"filepath": full_path,
"folder": os.path.dirname(full_path).replace(home_dir, ""),
"title": title,
"artists": artists,
"album_artist": album_artist,
"album": album,
"genre": genre,
"length": round(audio.info.length),
"bitrate": audio.info.bitrate,
"image": img_path
}
all_songs_instance.insert_song(tags)
return tags
def convert_one_to_json(song):
json_song = json.dumps(song, default=json_util.default)
loaded_song = json.loads(json_song)
return loaded_song
def convert_to_json(array):
songs = []
for song in array:
json_song = json.dumps(song, default=json_util.default)
loaded_song = json.loads(json_song)
songs.append(loaded_song)
return songs
def get_folders():
folders = []
for dir in default_configs['dirs']:
entry = os.scandir(dir)
folders.append(entry)
def remove_duplicates(array):
return array
def save_image(url, path):
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.save(path, 'JPEG')
def isValidFile(filename):
if filename.endswith('.flac') or filename.endswith('.mp3'):
return True
else:
return False
def isValidAudioFrom(folder):
folder_content = os.scandir(folder)
files = []
for entry in folder_content:
if isValidFile(entry.name) == True:
file = {
"path": entry.path,
"name": entry.name
}
files.append(file)
return files
def getFolderContents(filepath, folder):
folder_name = urllib.parse.unquote(folder)
path = filepath
name = filepath.split('/')[-1]
tags = {}
if name.endswith('.flac'):
image_path = folder_name + '/.thumbnails/' + \
name.replace('.flac', '.jpg')
audio = FLAC(path)
if name.endswith('.mp3'):
image_path = folder_name + '/.thumbnails/' + \
name.replace('.mp3', '.jpg')
audio = MP3(path)
abslt_path = urllib.parse.quote(path.replace(music_dir, ''))
if os.path.exists(image_path):
img_url = 'http://localhost:{}/{}'.format(
PORT,
urllib.parse.quote(image_path.replace(music_dir, ''))
)
try:
audio_url = 'http://localhost:{}/{}'.format(
PORT, abslt_path
)
tags = getTags(audio_url, audio, img_url, folder_name)
except:
pass
return tags
def create_config_dir():
home_dir = os.path.expanduser('~')
config_folder = home_dir + "/.shit"
dirs = ["", "/images", "/images/artists", "/images/thumbnails"]
for dir in dirs:
if not os.path.exists(config_folder + dir):
os.makedirs(config_folder + dir)
+82
View File
@@ -0,0 +1,82 @@
import pymongo
from bson import ObjectId
class Mongo:
def __init__(self, database):
mongo_uri = pymongo.MongoClient()
self.db = mongo_uri[database]
class Folders(Mongo):
def __init__(self):
super(Folders, self).__init__('LOCAL_FOLDERS')
self.collection = self.db['LOCAL_FOLDERS']
def insert_folder(self, folder):
self.collection.insert_one(folder)
def find_folder(self, folder_id):
return self.collection.find_one({'_id': ObjectId(folder_id)})
class Artists(Mongo):
def __init__(self):
super(Artists, self).__init__('ALL_ARTISTS')
self.collection = self.db['THEM_ARTISTS']
def insert_artist(self, artist_obj):
self.collection.update(artist_obj, artist_obj, upsert=True)
def get_all_artists(self):
return self.collection.find()
def get_artist_by_id(self, artist_id):
return self.collection.find_one({'_id': ObjectId(artist_id)})
def find_artists_by_name(self, query):
return self.collection.find({'name': {'$regex': query, '$options': 'i'}})
class AllSongs(Mongo):
def __init__(self):
super(AllSongs, self).__init__('ALL_SONGS')
self.collection = self.db['ALL_SONGS']
# def drop_db(self):
# self.collection.drop()
def get_song_by_id(self, file_id):
return self.collection.find_one({'_id': ObjectId(file_id)})
def insert_song(self, song_obj):
self.collection.update({'filepath': song_obj['filepath']}, song_obj, upsert=True)
def find_song_by_title(self, query):
self.collection.create_index([('title', pymongo.TEXT)])
return self.collection.find({'title': {'$regex': query, '$options': 'i'}})
def find_songs_by_album(self, query):
return self.collection.find({'album': {'$regex': query, '$options': 'i'}})
def get_all_songs(self):
return self.collection.find()
def find_songs_by_folder(self, query):
return self.collection.find({'folder': query})
def find_songs_by_artist(self, query):
return self.collection.find({'artists': {'$regex': query, '$options': 'i'}})
def find_songs_by_album_artist(self, query):
return self.collection.find({'album_artist': {'$regex': query, '$options': 'i'}})
def find_song_by_path(self, path):
return self.collection.find_one({'filepath': path})
def remove_song_by_filepath(self, filepath):
try:
self.collection.remove({'filepath': filepath})
return True
except:
return False
+58
View File
@@ -0,0 +1,58 @@
import time
import os
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class OnMyWatch:
watchDirectory = "/home/cwilvx/Music"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(
event_handler, self.watchDirectory, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
def create_thumb_dir(filepath):
f_name = filepath.split('/')[-1]
parent_dir = filepath.replace(f_name, '')
thumb_dir = parent_dir + ".thumbnails"
if not os.path.exists(thumb_dir):
os.makedirs(thumb_dir)
class Handler(PatternMatchingEventHandler):
def __init__(self):
PatternMatchingEventHandler.__init__(
self, patterns=['*.flac', '*.mp3'], ignore_directories=True, case_sensitive=False)
def on_created(self, event):
print(event.src_path)
create_thumb_dir(event.src_path)
def on_deleted(self, event):
print(event.src_path)
def on_moved(self, event):
print(event.src_path)
print(event.dest_path)
if __name__ == '__main__':
watch = OnMyWatch()
watch.run()