コード例 #1
0
ファイル: Fotos.cs プロジェクト: darknut/OMIstats
        private void updateAlbum()
        {
            string fallbackPortada = "";

            try
            {
                currentCalls = 1; // Bloqueamos a otros procesos con esta bandera

                // Sacamos el metadata del album
                Dictionary <string, object> response = call(String.Format(BASE_FACEBOOK_URL, ACCESS_TOKEN, id, "", ALBUM_METADATA));
                nombre = (string)response[NOMBRE_ALBUM];
                fotos  = (int)response[COUNT_ALBUM];
                string coverId = (string)((Dictionary <string, object>)response[PORTADA_ALBUM])[ID];

                // Luego sacamos el metadata de la portada
                response = call(String.Format(BASE_FACEBOOK_URL, ACCESS_TOKEN, coverId, "", FOTOS_METADATA));
                portada  = obtenerFoto(response, ignorarVerticales: true);

                // Borramos las fotos del album
                Foto.borrarDeAlbum(id);

                string after     = "";
                int    ordenFoto = 0;
                // Finalmente, obtenemos las fotos del album
                while (true)
                {
                    string url = String.Format(BASE_FACEBOOK_URL, ACCESS_TOKEN, id, ALBUM_FOTOS, FOTOS_METADATA);
                    if (after.Length > 0)
                    {
                        url += String.Format(AFTER_METADATA, after);
                    }
                    response = call(url);

                    ArrayList data = (ArrayList)response[DATA];
                    if (data.Count == 0)
                    {
                        break;
                    }

                    // Se agregan las fotos
                    foreach (Dictionary <string, object> foto in data)
                    {
                        Foto f = new Foto();
                        f.album  = id;
                        f.id     = (string)foto[ID];
                        f.orden  = ++ordenFoto;
                        f.url    = (string)foto[LINK];
                        f.imagen = obtenerFoto(foto);

                        if (fallbackPortada.Length == 0)
                        {
                            fallbackPortada = f.imagen;
                        }

                        if (portada.Length == 0)
                        {
                            portada = obtenerFoto(foto, ignorarVerticales: true);
                        }

                        f.guardar();
                    }

                    Dictionary <string, object> paging  = (Dictionary <string, object>)response[PAGING];
                    Dictionary <string, object> cursors = (Dictionary <string, object>)paging[CURSORS];
                    after = (string)cursors[AFTER];
                }
            }
            catch (Exception e)
            {
                Log.add(Log.TipoLog.FACEBOOK, e.ToString());
            }
            finally
            {
                if (portada.Length == 0)
                {
                    portada = fallbackPortada;
                }

                // Actualizamos las actualizaciones en general
                Album al = Album.obtenerAlbum(ALBUM_GRAL);

                if (al.lastUpdated < DateTime.Today)
                {
                    al.lastUpdated = DateTime.Today;
                    al.orden       = currentCalls;
                }
                else
                {
                    al.orden += currentCalls;
                }

                // Le quitamos 1 del extra que le pusimos al principio
                al.orden--;

                // Actualizamos los calls en el sistema
                al.guardarDatos();

                // También actualizamos la fecha del album que estamos actualizando
                lastUpdated  = DateTime.Today;
                currentCalls = 0;
            }
        }