Example #1
0
def songs_post():
    # Add a new song to the database 
    db_song = models.Song(file_id=file.id)
    session.add(db_song)
    session.commit()
    data = db_song.as_dictionary()
    return Response(json.dumps(data), 201, mimetype="application/json")
Example #2
0
def songs_post():
    """ Add a new song - after file is uploaded """
    # Get JSON from the request
    data = request.json

    # Check that the JSON supplied is valid
    # If not you return a 422 Unprocessable Entity
    try:
        validate(data, file_schema)
    except ValidationError as error:
        data = {"message": error.message}
        return Response(json.dumps(data), 422, mimetype="application/json")

    # Extract the file data from the request
    file = data["file"]
    # Verify that this file exists in the database
    db_file = session.query(models.File).get(file["id"])

    # Create the song object, linking the file_id
    song = models.Song(file_id=file["id"])
    # Add the song to the session and commit it
    session.add(song)
    session.commit()

    # Return a 201 Created, containing the post as JSON and with the
    # Location header set to the location of the post
    data = json.dumps(song.as_dictionary())
    headers = {"Location": url_for("songs_get")}
    return Response(data, 201, headers=headers, mimetype="application/json")
Example #3
0
def songs_post():
    """ Post a new song to database """

    data = request.json

    # Check for valid JSON. If not available, return 422, Unprocessable Entity
    try:
        validate(data, post_schema)
    except ValidationError as error:
        data = json.dumps({"message": error.message})
        return Response(data, 422, mimetype="application/json")

    file_id = data["file"]["id"]
    song_file = session.query(models.File).get(file_id)

    if not song_file:  # File with id = file_id is not in database
        message = "Could not find song file with file id {}".format(file_id)
        data = json.dumps({"message": message})
        return Response(data, 404, mimetype="application/json")

    song = models.Song(file_id=file_id)
    session.add(song)
    session.commit()
    data = json.dumps(song.as_dictionary())
    return Response(data, 201, mimetype="application/json")
Example #4
0
def addbar():
    # TODO: ADD data verification, ensure everything is there...
    request_data = request.args

    line = request_data.get('line')
    artist_name = request_data.get('correct_artist')
    title = request_data.get('song')

    # Create artist if not in the database
    correct_artist = models.Artist.query.filter_by(Name=artist_name).first()
    if(correct_artist == None):
        genius = apimanager.Genius()
        img_url = genius.search(artist_name)[
            'response']['hits'][0]['result']['primary_artist']['image_url']
        correct_artist = models.Artist(Name=artist_name, Image=img_url)
        db.session.add(correct_artist)
        db.session.commit()  # may not be necessary to commit new artist here

    # Create song if not in the database
    s = models.Song.query.filter_by(Name=title).first()
    if(s == None):
        genius = apimanager.Genius()
        img_url = genius.search(title + ", " + correct_artist.Name)[
            'response']['hits'][0]['result']['song_art_image_url']
        s = models.Song(Name=title, artist=correct_artist, Image=img_url)
        db.session.add(s)
        db.session.commit()  # may not be necessary to commit new song here

    db.session.add(models.Bar(Line=line, CorrectArtist=correct_artist.ArtistID, song=s))
    db.session.commit()
    return jsonify({'response': 'Success'})
Example #5
0
def putSong(title, artist, album=None):
  song = models.Song(parent=album,
                     title=title, artist=artist)
  if album is not None:
    song.album = album
  song.put()
  return mcset(song, SONG_ENTRY %song.key())
Example #6
0
 def update_global_song(self, key_name, viewer_song, opinion=False, viewcount=False, old_opinion=None):
   global_song = models.Song.get_by_key_name(key_name)
   if global_song is None:
     global_song = models.Song(key_name=key_name)
     global_song.title = viewer_song.title
     global_song.artist = viewer_song.artist
   
   if opinion:
     # Need old opinion!
     if viewer_song.opinion == 'Yay':
       global_song.opinioncount_yay += 1
     elif viewer_song.opinion == 'Meh':
       global_song.opinioncount_meh += 1
     else:
       global_song.opinioncount_nay += 1
       
     if old_opinion:
       if old_opinion == 'Yay':
         global_song.opinioncount_yay -= 1
       elif old_opinion == 'Meh':
         global_song.opinioncount_meh -= 1
       else:
         global_song.opinioncount_nay -= 1
         
   elif viewcount:
     models.Song.incr_viewcount(key_name)
   
   global_song.put()
Example #7
0
def create_playlist(user_id, playlist={}, songs=[{}]):
    """ Funcion para agregar una playlist """
    p = m.s.query(m.Playlist).filter(m.Playlist.url == playlist['url']).first()

    # Si la playlists no esta en la DB, se añaden las canciones y luego al usuario
    if p is None:
        p = m.Playlist(**playlist)
        for song in songs:
            if '/channel/' in song.get('youtube_id',
                                       '') or '/user/' in song.get(
                                           'youtube_id', ''):
                continue
            # s = m.s.query(m.Song).filter_by(youtube_id=song['youtube_id']).first()
            s = m.s.query(m.Song).filter(
                or_(m.Song.youtube_id == song.get('youtube_id', ''),
                    m.Song.sc_permalink == song.get('sc_permalink',
                                                    ''))).first()
            s = s if s else m.Song(**song)
            p.songs.append(s)
        m.s.add(p)

    user = m.s.query(m.User).filter_by(id=user_id).first()
    user.playlists.append(p)
    m.s.commit()
    m.s.flush()
    m.s.refresh(p)
    return {
        'id': p.id,
        'url': p.url,
        'name': p.name,
        'source': p.source,
        'total': len(p.songs),
        'missing': len(p.songs)
    }
Example #8
0
def song_post():
    """ post a song """
    headers = {
        "Location": url_for("song_post"),
        "Content-Type": "application/json"
    }

    ######### get the data from the form
    data = request.json

    post_song = models.Song(
        song_name=data["song_name"],
        id=data["song_id"])  ## ask sam if we really need to post seperately.
    post_file = models.File(song_id=data["song_id"],
                            file_name=data["file_name"],
                            id=data["file_id"])

    if not session.query(models.Song).get(
            post_song.id
    ):  #consider adding a check here for duplicate fileID too
        session.add_all([post_song, post_file])
        session.commit()
    else:
        print "************* ELSE ***************"
        session.rollback()
        session.flush()
        return Response(json.dumps(
            {"status": "failed - that song already exists"}),
                        500,
                        mimetype="application/json")

    return Response(stripUnicode(data),
                    200,
                    headers=headers,
                    mimetype="application/json")
def create_song(db: Session, song):

    db_song = models.Song(name=song["name"], duration=song["duration"])

    db.add(db_song)
    db.commit()
    db.refresh(db_song)

    return db_song
Example #10
0
def songs_post():
    """ Add a new song """
    data = request.json

    # Add the post to the database
    song = models.Song(file=data["file"])
    session.add(song)
    session.commit()

    # Return a 201 Created, containing the post as JSON and with the
    # Location header set to the location of the post
    data = json.dumps(song.as_dictionary())
    headers = {"Location": url_for("post_get", id=song.id)}
    return Response(data, 201, headers=headers, mimetype="application/json")
Example #11
0
def create_file(db: Session, audio_type: int, audio: dict):
	if audio_type==1:
	    db_user = models.Song(**audio)
	elif audio_type==2:
	    db_user = models.Podcast(**audio)
	else:
	    db_user = models.Audiobook(**audio)
	try:
	    db.add(db_user)
	    db.commit()
	    db.refresh(db_user)
	    return True
	except Exception as e:
		return False
Example #12
0
def update_playlist(playlist_id, user_id=None, songs=[{}]):
    """ Funcion para añadir nuevas canciones a una playlist ya existente """
    p = m.s.query(m.Playlist).filter_by(id=playlist_id).first()
    old_yt_ids = [s.youtube_id for s in p.songs]
    new_yt_ids = [s['youtube_id'] for s in songs]
    songs_to_add = [s for s in songs if s['youtube_id'] not in old_yt_ids]
    songs_to_remove = [s for s in p.songs if s.youtube_id not in new_yt_ids]
    for song in songs_to_remove:
        p.songs.remove(song)
    for song in songs_to_add:
        p.songs.append(m.Song(**song))
    m.s.commit()
    return {
        'total': len(p.songs),
        'missing': len(old_yt_ids) + len(songs_to_add) - len(songs_to_remove)
    }
Example #13
0
def saveSong(song, path):
    song.path = path
    pickle = Pickler.savePickle(song)
    dbsong = models.Song()
    dbsong.seconds = song.seconds
    dbsong.path = path
    dbsong.name = song.name
    dbsong.file = pickle
    if not checkForSong(dbsong):
        db.session.add(dbsong)
    savedSong = db.session.query(models.Song).filter(
        models.Song.name == dbsong.name,
        models.Song.seconds == dbsong.seconds).first()
    if g.user:
        if g.user is not None and g.user.is_authenticated:
            g.user.songs.append(savedSong)
    for item in db.session.query(models.Song):
        print(item)
    db.session.commit()
def save_songs(song_json):
    for e in song_json:
        song = models.Song()
        song.song_name = e["name"]
        song.song_id = e["id"]
        song.duration = e["duration"]
        song.comment_thread = e["commentThreadId"]
        artists = e["artists"]
        album = e["album"]
        song.album_id = album["id"]
        song.album_name = album["name"]
        artist_id ='/'.join([str(i["id"]) for i in artists])
        artist_name = '/'.join([i['name'] for i in artists])
        song.artist_name = artist_name
        song.artist_id = artist_id
        try:
            song.save()
        except Exception,e:
            models.db.rollback()
            sys.stderr.write('song.save: ' +str(e))
Example #15
0
def post_song():
    """ Add a new song """
    data = request.json
    print data

    # check that the JSON supplied is valid
    # if not return a 422 Unprocessable Entity
    try:
        validate(data, song_schema)
    except ValidationError as error:
        data = {"message": error.message}
        return Response(json.dumps(data), 422, mimetype="application/json")

    # add the song to the database
    song = models.Song(song_file_id=data["file"]["id"])
    session.add(song)
    session.commit()

    # return a 201 Created, containing the post as JSON and with the
    # location header set to the location of the post
    data = json.dumps(song.as_dictionary())
    headers = {"Location": url_for("get_songs")}
    return Response(data, 201, headers=headers, mimetype="application/json")
Example #16
0
def getSonglist(url):
    page = getPage(url)
    title_path = ".//ul[@class='f-hide']/li/a"
    description_path = ".//meta[@name='description']"
    song_path = './/div[@id="song-list-pre-cache"]/textarea'
    tree = etree.HTML(page)
    des = tree.xpath(description_path)
    #return des[0].get('content')
    res = tree.xpath(title_path)
    songs = tree.xpath(song_path)
    song_json = json.loads(songs[0].text)
    count = 0
    for e in song_json:
        song = models.Song()
        song.song_name = e["name"]
        song.song_id = e["id"]
        song.duration = e["duration"]
        song.comment_thread = e["commentThreadId"]
        artists = e["artists"]
        album = e["album"]
        song.album_id = album["id"]
        song.album_name = album["name"]
        artist_id = ''
        artist_name = ''
        artist_id ='/'.join([str(i["id"]) for i in artists])
        artist_name = '/'.join([i['name'] for i in artists])
        song.artist_name = artist_name
        song.artist_id = artist_id
        models.db.rollback()
        if count%2==0:
            song.save()
        count +=1
        print artist_id + '\t' + artist_name
    for e in res:
        #print e.text +' ' + e.get('href')
        pass
    return res
Example #17
0
def song_delete():
    """ delete a song """
    headers = {"Location": url_for("song_delete")}
    # get the data from the form

    data = request.json

    post_song = models.Song(
        song_name=data["song_name"],
        id=data["song_id"])  ## ask sam if we really need to post seperately.
    post_file = models.File(song_id=data["song_id"],
                            file_name=data["file_name"],
                            id=data["file_id"])

    if session.query(models.Song).get(
            post_song.id
    ):  #consider adding a check here for duplicate fileID too
        del_song = session.query(models.Song).get(post_song.id)
        session.delete(del_song)

        del_file = session.query(models.File).get(post_file.id)
        session.delete(del_file)

        session.commit()
    else:
        print "************* ELSE ***************"
        session.rollback()
        session.flush()
        return Response(json.dumps(
            {"status": "failed - that song doesnt exists"}),
                        500,
                        mimetype="application/json")

    return Response(json.dumps({"status": "deleted"}),
                    200,
                    headers=headers,
                    mimetype="application/json")
def song_post():
    data = request.json
    song = models.Song(file_id=data['file']['id'])
    session.add(song)
    session.commit()
    """Print outs to view how data looks during execution above
	print("print outs from song_post()")
	print("data from request.json in song_post() is {}".format(data))
	print ("data from ['file']['id'] is {}".format(data['file']['id']))
	print("session.query(models.File).get(data['file']['id']) is {}".format(session.query(models.File).get(data['file']['id'])))
	"""

    data = json.dumps(song.as_dictionary())
    headers = {"Location": url_for("song_get", id=song.id)}

    response_info = Response(data,
                             201,
                             headers=headers,
                             mimetype="application/json")

    #Print out to view how info above looks
    print("final song_post response inf {}".format(response_info.data))

    return Response(data, 201, headers=headers, mimetype="application/json")
Example #19
0
def parse():
    print("\nJob started\n")

    data = {
        "song": [],
        "movie": [],
        "app": [],
    }
    files_name = []

    data_file = requests.get(f"{URL}/files_list.data")
    files_list = set(data_file.text.split("\n"))

    engine = sa.create_engine(DB_PATH)
    # print('!!! Dropping all tables !!!')
    # models.Base.metadata.drop_all(engine, models.Base.metadata.tables.values())
    # print('!!! All tables dropped !!!')
    models.Base.metadata.create_all(engine,
                                    models.Base.metadata.tables.values())

    Session = sessionmaker(bind=engine)
    with Session.begin() as session:
        processed_df = {df.name for df in session.query(models.DataFile).all()}

    for f in files_list - processed_df:
        print("-" * 42)
        print(f"New file ::: {f}")
        content = requests.get(f"{URL}/{f}").json()
        print(f"{len(content)} elements")
        for obj in content:
            t = obj["type"]
            if t == "song":
                data["song"].append(
                    obj["data"] | {"ingestion_time": datetime.datetime.now()})
            if t == "movie":
                data["movie"].append(
                    obj["data"]
                    | {
                        "original_title_normalized":
                        func_title(obj["data"]["original_title"])
                    })
            if t == "app":
                data["app"].append(obj["data"]
                                   | {"is_awesome": func_rate(obj["data"])})
        files_name.append(f)
        print("*" * 42)

    print(f"Data files parsed: {len(files_name)}")
    print(f"New song objects: {len(data['song'])}")
    print(f"New movie objects: {len(data['movie'])}")
    print(f"New app objects: {len(data['app'])}")
    if len(files_name):
        print("Inserting into db......")

    Session = sessionmaker(bind=engine)
    with Session() as session:
        session.add_all([models.Song(**song) for song in data["song"]])
        session.add_all([models.Movie(**movie) for movie in data["movie"]])
        session.add_all([models.App(**app) for app in data["app"]])
        session.add_all(
            [models.DataFile(name=file_name) for file_name in files_name])
        session.commit()

        print("^" * 42)
        print(
            f"Total song objects in db: {session.query(models.Song).count()}")
        print(
            f"Total movie objects in db: {session.query(models.Movie).count()}"
        )
        print(f"Total app objects in db: {session.query(models.App).count()}")
        print(
            f"Total data files parsed: {session.query(models.DataFile).count()}"
        )
        print("-" * 42)
Example #20
0
args = parser.parse_args()

provider = ["azlyrics", "elyrics"]
saveflag = False

artist = args.artist
title = args.lyric
if args.provider:
    provider = args.provider
if args.save:
    saveflag = args.save

if __name__ == '__main__':

    filename = mo.create_filename(artist, title)
    song = mo.Song(artist, title)

    objAZ = mo.Azlyrics(artist, title)
    objEL = mo.Elyrics(artist, title)

    if song.search_in_repo() == False:

        if provider == "azlyrics":
            try:
                print("Searching in AZlyrics...")
                print(objAZ.get_lyric())
                if saveflag:
                    objAZ.save()
            except:
                print("no results in AZlyrics")