Esempio n. 1
0
def addSpotify(request):
    try:
        account = Account.objects.get(user_id = request.user.id)
    except:
        return HttpResponseForbidden()
    if len(account.spotify_username) == 0 or len(account.spotify_password) == 0:
        return HttpResponseForbidden()

    if 'ok' in request.POST and 'uri' in request.POST and request.POST['uri'].startswith('spotify:track:'):
            
        query = {'action':'meta', 'uri':request.POST['uri'], 'username':account.spotify_username, 'password':account.spotify_password}
        try:
            result = json.loads(requests.get("http://localhost:8080/", params=query).text)
            
            song = Song(account_id=account.id,
                        source=Song.SOURCE_SPOTIFY,
                        hash=request.POST['uri'].replace('spotify:track:', ''),
                        title=result['title'],
                        artist=result['artist'],
                        length=result['length'],
                        album=result['title'],
                        track=int(result['track']) if 'track' in result and result['track'] else None,
                        year=int(result['year']) if 'year' in result and result['year'] else None)
            
            song.full_clean()
            song.save()
                
        except:
            pass

        return HttpResponse(status=201)

    return render_to_response('establishment/library/addSpotify.html', {}, context_instance=RequestContext(request))
Esempio n. 2
0
def add(request):
    try:
        account = Account.objects.get(user_id = request.user.id)
    except:
        return HttpResponseForbidden()

    if 'ok' in request.POST:
        uploadPath = os.path.join(settings.MEDIA_ROOT, "upload/" + str(account.id) + "/")
        libraryPath = os.path.join(settings.MEDIA_ROOT, "library/" + str(account.id) + "/")

        for file in os.listdir(uploadPath):
            name = file.split('_', 1)
            
            audio = MP3(uploadPath + file)
            comments = audio.tags.getall('COMM:')
            song = Song(account_id=account.id,
                        source=Song.SOURCE_FILE,
                        hash=hashlib.md5(open(uploadPath + file, 'rb').read()).hexdigest(),
                        title=audio.tags['TIT2'].text[0] if 'TIT2' in audio.tags and audio.tags['TIT2'].text[0] else name[1],
                        artist=audio.tags['TPE1'].text[0] if 'TPE1' in audio.tags and audio.tags['TPE1'].text[0] else "Uknown artist",
                        length=audio.info.length,
                        album=audio.tags['TALB'].text[0] if 'TALB' in audio.tags else "",
                        track=int(audio.tags['TRCK'].text[0]) if 'TRCK' in audio.tags and isinstance(audio.tags['TRCK'].text[0], (int, long, float, complex)) else None,
                        year=int(audio.tags['TDRC'].text[0].get_text()) if 'TDRC' in audio.tags and isinstance(audio.tags['TDRC'].text[0].get_text(), (int, long, float, complex)) else None,
                        genre=audio.tags['TCON'].text[0] if 'TCON' in audio.tags else "",
                        comment=comments[0].text[0] if comments else "")
            
            try:
                song.full_clean()
                song.save()
                image = None
                if 'APIC:' in audio.tags:
                    image = audio.tags['APIC:'].data
                else:
                    if 'PIC:' in audio.tags:
                        image = audio.tags['PIC:'].data
                if image:
                    with open(libraryPath + str(song.id) + ".jpg", 'wb') as img:
                        img.write(image)
                os.rename(uploadPath + file, libraryPath + str(song.id) + ".mp3")
            except ValidationError:
                pass

        return HttpResponse(status=201)

    return render_to_response('establishment/library/add.html', {}, context_instance=RequestContext(request))