Beispiel #1
0
    IEnumerator DownloadAlbumImage(SpotifyImage[] images)
    {
        print("Downloading image");
        /* Prioritize the highest resolution image */
        SpotifyImage selected = images[0];

        if (selected == null)
        {
            selected = images[1];
        }
        if (selected == null)
        {
            selected = images[2];
        }

        /* Download image */
        string url = selected.url;

        using (WWW www = new WWW(url))
        {
            /*  Wait for download to complete */
            yield return(www);

            /* Create a texture in DXT1 format */
            Texture2D texture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);

            /* Assign the downloaded image to sprite */
            www.LoadImageIntoTexture(texture);
            Rect   rec         = new Rect(0, 0, texture.width, texture.height);
            Sprite spriteToUse = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 100);
            image.sprite = spriteToUse;

            animator.SetBool("loading", false);
        }
    }
Beispiel #2
0
 public static ImageInfo CreateImageInfo(SpotifyImage inImage)
 {
     return(new ImageInfo
     {
         Height = inImage.Height ?? 0,
         Width = inImage.Width ?? 0,
         Url = inImage.Url
     });
 }
        public async Task <ActionResult> Post([FromBody] DiscographyPostData bodyObj)
        {
            try
            {
                // Obtener Albums
                List <Album> albumsList = ds.getAlbumsFromBase64File(bodyObj.File);

                // Ordenar los datos
                List <Album> orderedAlbums = albumsList.OrderBy(album => album.Year).ThenBy(album => album.Title).ToList();

                // Preparar los datos (Obtener décadas de discos)
                var groupedByDecade = orderedAlbums.GroupBy(album => Utils.Utils.GetDecade(album.Year)).OrderByDescending(x => Utils.Utils.GetDecade(x.Key));

                // Crear tablero de Trello
                CrearTableroResult tablero = ts.CrearTablero(bodyObj.DashboardTitle);
                if (tablero != null)
                {
                    // Crear listas de décadas en tablero de Trello
                    foreach (var decade in groupedByDecade)
                    {
                        CrearListaResult lista = ts.CrearLista(tablero.Id, String.Format("{0}s'", decade.Key.ToString()));
                        foreach (var album in decade)
                        {
                            // Crear tarjeta para el album
                            string tarjetaName = album.Year + " - " + album.Title;

                            CrearTarjetaResult tarjeta = ts.CrearTarjeta(lista.Id, tarjetaName);

                            // Obtener imagen del album de Spotify
                            SpotifyAlbumSearch albumsResult = ss.searchAlbumsByTitle(album.Title);

                            if (albumsResult != null)
                            {
                                SpotifyAlbum albumFound = albumsResult.albums.Items
                                                          .Where(x => x.Artists.FirstOrDefault().Name.ToLower().Equals("Bob Dylan".ToLower()))
                                                          .Where(x => Utils.Utils.homologarComilllas(x.Name).ToLower().Equals(Utils.Utils.homologarComilllas(album.Title).ToLower()))
                                                          .FirstOrDefault();
                                if (albumFound != null)
                                {
                                    SpotifyImage albumImage = albumFound.images.Where(x => x.Width == 300).FirstOrDefault();

                                    // Adjuntar imagen a la portada
                                    SpotifyImage attachedImage = ts.AdjuntarPortada(tarjeta.Id, albumImage.Url, "img_" + tarjetaName.Replace(" ", ""));
                                }
                                else
                                {
                                    // Registrar de que albums no se encontraron portadas
                                    var t = album.Title;
                                }
                            }
                        }
                    }
                }

                return(Ok());
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        public async Task <ProcessAlbumsResponse> ProcessAlbums([FromForm][Required] string accessToken,
                                                                [FromForm][Required] string state)
        {
            if (!await AuthenticateUser(accessToken, state))
            {
                Response.StatusCode = StatusCodes.Status401Unauthorized;
                return(new ProcessAlbumsResponse());
            }

            logger.LogInformation($"Started processing {state}");

            // Used for timing information
            DateTime before             = DateTime.UtcNow;
            bool     hadToProcessImages = false;

            int total  = 0;
            int offset = 0;
            List <SpotifySavedAlbum> albums = new List <SpotifySavedAlbum>();

            do
            {
                int limit       = 50;
                var savedAlbums = await spotifyClient.GetUserSavedAlbums(limit, offset);

                offset += limit;
                albums.AddRange(savedAlbums.Items);
                total = savedAlbums.Total;
            }while (albums.Count < total);

            ProcessAlbumsResponse response = new ProcessAlbumsResponse();

            foreach (var album in albums)
            {
                SpotifyImage largestImage = album.Album.GetLargestImage();
                if (largestImage == null || largestImage.Url == null)
                {
                    continue;
                }

                AlbumInfo?albumInfo = await cosmosAPI.GetAlbumInfo(album.Album.Uri);

                if (albumInfo == null)
                {
                    hadToProcessImages = true;
                    ImageAnalysis results;
                    try
                    {
                        results = await computerVisionAPI.AnalyzeImage(largestImage.Url);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Failed to process image. Spotify URI: {album.Album.Uri}");
                        continue;
                    }

                    string?artist = null;
                    if (album.Album.Artists.Count > 0)
                    {
                        artist = album.Album.Artists[0].Name;
                    }
                    albumInfo = new AlbumInfo()
                    {
                        AlbumId       = album.Album.Uri,
                        AlbumName     = album.Album.Name,
                        ArtistName    = artist,
                        VisionResults = new VisionResults()
                        {
                            Color       = results.Color,
                            Description = results.Description,
                            ImageType   = results.ImageType,
                            Tags        = results.Tags
                        }
                    };

                    try
                    {
                        await cosmosAPI.SaveAlbumInfo(albumInfo);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Failed to save album info. Spotify URI: {album.Album.Uri}");
                        continue;
                    }

                    AlbumInfoSearchObject searchObject = albumInfo.ToSearchObject();

                    try
                    {
                        await searchAPI.UploadDocument(searchObject);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Failed to save search document. Spotify URI: {album.Album.Uri}");
                    }
                }

                response.Albums.Add(album.ToResponseAlbum());
            }

            DateTime after          = DateTime.UtcNow;
            TimeSpan processingTime = after - before;

            logger.LogInformation($"{{\"processingTime\": {processingTime.TotalSeconds}, \"hadToProcessImages\": {hadToProcessImages}}}");

            return(response);
        }