예제 #1
0
        //static async Task Sample(CancellationToken cancellationToken)
        //{
        //    using (var client = new ServiceClient("1a85f5aeaf961ae4ee8a30df575d2baa"))
        //    {
        //        for (int i = 1, count = 1000; i <= count; i++)
        //        {
        //            var movies = await client.Movies.GetTopRatedAsync(null, i, cancellationToken);
        //            count = movies.PageCount; // keep track of the actual page count

        //            foreach (Movie m in movies.Results)
        //            {
        //                var movie = await client.Movies.GetAsync(m.Id, null, true, cancellationToken);



        //                var personIds = movie.Credits.Cast.Select(s => s.Id)
        //                    .Union(movie.Credits.Crew.Select(s => s.Id));

        //                foreach (var id in personIds)
        //                {
        //                    var person = await client.People.GetAsync(id, true, cancellationToken);

        //                    //foreach (var img in person.Images.Results)
        //                    //{
        //                    //    //string filepath = Path.Combine("People", img.FilePath.TrimStart('/'));
        //                    //    //await DownloadImage(img.FilePath, filepath, cancellationToken);
        //                    //}



        //                }
        //                //saveMovie(movie);
        //            }
        //        }
        //    }
        //}


        //static void GetAllMovies(CancellationToken cancellationToken)
        //{
        //    using (var client = new ServiceClient("1a85f5aeaf961ae4ee8a30df575d2baa"))
        //    {
        //        for (int i = 1, count = 40; i < count; i++)
        //        {
        //            var movies = client.Movies.GetNowPlayingAsync(null, i, cancellationToken).Result;
        //            // count = movies.PageCount;
        //            foreach (Movie m in movies.Results)
        //            {
        //                E.Movie Dbm = new E.Movie();
        //                Dbm.IdMovie = m.Id;
        //                Dbm.Title = m.Title;
        //                Dbm.Age = m.Adult ? "Adult" : "All Age";
        //                Dbm.Duration = 0;
        //                Dbm.ReleaseDate = m.ReleaseDate;
        //                if (m.Images != null && m.Images.Posters != null && m.Images.Posters.Count() > 0)
        //                {
        //                    Dbm.Picture = m.Images.Posters.FirstOrDefault().FilePath;
        //                }
        //                else
        //                {
        //                    Dbm.Picture = "NoPicture.jpg";
        //                }
        //                Dbm.Genre = GetGenre(m);
        //                Dbm.Cast = GetCast(m, client);
        //                Dbm.Rating = GetRating(m, client);
        //                lm.Add(Dbm);
        //            }


        //        }
        //    }
        //}

        //private static IEnumerable<E.Rating> GetRating(Movie m, ServiceClient client)
        //{
        //    client.Genres.GetAsync();
        //}

        //private static IEnumerable<E.Genre> GetGenre(Movie m)
        //{
        //    if (m.Genres == null) return new List<E.Genre>();
        //    return m.Genres.Select(g => new E.Genre() { IdGenre = g.Id, Label = g.Name });
        //}

        //private static IEnumerable<E.Cast> GetCast(Movie movie, ServiceClient client)
        //{
        //    if (movie.Credits == null) return new List<E.Cast>();
        //    List<E.Cast> lc = new List<E.Cast>();
        //    var personIds = movie.Credits.Cast.Select(s => s.Id)
        //                   .Union(movie.Credits.Crew.Select(s => s.Id));

        //    foreach (var id in personIds)
        //    {
        //        var person = client.People.GetAsync(id, true, new CancellationToken()).Result;
        //        lc.Add(new E.Cast() { IdCast = person.Id, FirstName = "", LastName = person.Name });
        //        foreach (var img in person.Images.Results)
        //        {
        //            string filepath = Path.Combine("People", img.FilePath.TrimStart('/'));

        //            DownloadImage(img.FilePath, filepath, new CancellationToken()).Wait();
        //        }



        //    }
        //    return lc;
        //}

        #endregion
        static async Task DownloadImage(string filename, string localpath, CancellationToken cancellationToken)
        {
            if (!File.Exists(localpath))
            {
                string folder = Path.GetDirectoryName(localpath);
                Directory.CreateDirectory(folder);

                var storage = new StorageClient();
                using (var fileStream = new FileStream(localpath, FileMode.Create,
                                                       FileAccess.Write, FileShare.None, short.MaxValue, true))
                {
                    try { await storage.DownloadAsync(filename, fileStream, cancellationToken); }
                    catch (Exception ex) { System.Diagnostics.Trace.TraceError(ex.ToString()); }
                }
            }
        }