예제 #1
0
        public static async Task SeedLiveStreams()
        {
            IEnumerable <StationDirbleDto> stationDtos;
            var page = 1;

            do
            {
                stationDtos = await _dirble.GetStations(page);

                foreach (var stationDto in stationDtos)
                {
                    var existingLiveStream = _liveStreamService.GetLiveStream(stationDto.Id);

                    if (existingLiveStream != null)
                    {
                        string coverImageName = null;

                        if (stationDto.Image.Url != null)
                        {
                            using (var webClient = new WebClient())
                            {
                                try
                                {
                                    using (var imageStream = webClient.OpenRead(new Uri(stationDto.Image.Url)))
                                    {
                                        using (var memoryStream = new MemoryStream())
                                        {
                                            await imageStream.CopyToAsync(memoryStream);

                                            memoryStream.Position = 0;

                                            new FileExtensionContentTypeProvider().TryGetContentType(stationDto.Image.Url, out string contentType);

                                            try
                                            {
                                                coverImageName = await _uploadService.UploadCoverImage(stationDto.Image.Url, memoryStream, contentType);
                                            }
                                            catch (Exception) { }
                                        }
                                    }
                                }
                                catch (WebException) { }
                            }
                        }

                        var liveStream = new LiveStream
                        {
                            Id             = stationDto.Id,
                            Name           = stationDto.Name,
                            Country        = stationDto.Country,
                            Description    = String.IsNullOrWhiteSpace(stationDto.Description) ? null : stationDto.Description,
                            CoverImageName = coverImageName,
                            WebsiteUrl     = String.IsNullOrWhiteSpace(stationDto.Website) ? null : stationDto.Website,
                            TwitterUrl     = String.IsNullOrWhiteSpace(stationDto.Twitter) ? null : stationDto.Twitter,
                            FacebookUrl    = String.IsNullOrWhiteSpace(stationDto.Facebook) ? null : stationDto.Facebook,
                            DateAdded      = DateTimeOffset.Parse(stationDto.CreatedAt),
                            DateUpdated    = DateTimeOffset.Parse(stationDto.UpdatedAt),
                            Slug           = stationDto.Slug,
                        };

                        foreach (var category in stationDto.Categories)
                        {
                            liveStream.AudioGenres.Add(new AudioGenre
                            {
                                GenreId = category.Id,
                            });
                        }

                        foreach (var streamDto in stationDto.Streams)
                        {
                            liveStream.StreamDatas.Add(new StreamData
                            {
                                LiveStreamUrl = streamDto.Stream,
                                Bitrate       = streamDto.Bitrate == 0 ? null : streamDto.Bitrate,
                                ContentType   = streamDto.ContentType == "?" ? null : streamDto.ContentType,
                            });
                        }

                        _liveStreamService.Add(liveStream);
                    }
                }

                page++;
            } while (stationDtos.Any());
        }
예제 #2
0
 public override Models.LiveStream GetById(string id)
 {
     return(_liveStreamService.GetLiveStream(int.Parse(id)));
 }