Beispiel #1
0
        private static TvShowImportRequestModel BuildTvImportModel(string filename, int episodeNumber)
        {
            FileInfo fi = new FileInfo(filename);
            TvShowImportRequestModel model = new TvShowImportRequestModel
                {
                    Episode = episodeNumber,
                    FileHash = FileHash.ComputeMovieHash(filename),
                    Format = DetectType.FindVideoSource(filename),
                    Filename = Path.GetFileName(filename),
                    FullPath = filename,
                    Season = TvNaming.ExtractSeasonNumber(filename),
                    SeriesName = TvNaming.ExtractSeriesName(filename),
                    FileSize = fi.Length
                };

            return model;
        }
        public HttpResponseMessage PostTv(TvShowImportRequestModel requestModel)
        {
            string username = User.Identity.Name;
            var user = _membershipService.GetUser(username);

            Guid tvShowKey = Guid.Empty;
            if (!string.IsNullOrEmpty(requestModel.SeriesName))
            {
                Guid showId = Guid.Empty;
                var tvShow = _tvService.GetTvShow(requestModel.SeriesName);
                if (tvShow == null)
                {
                    TvShow show = new TvShow { ShowName = requestModel.SeriesName };
                    var showResult = _tvService.AddShow(show);
                    showId = showResult.Entity.Key;
                }
                else
                {
                    showId = tvShow.Key;
                }
                tvShowKey = showId;
            }

            TvEpisode dbEp = _tvService.GetTvEpisode(requestModel.FileHash, requestModel.Season, requestModel.Episode);
            if (dbEp == null)
            {
                var tvEpisode = Mapper.Map<TvEpisode>(requestModel);
                tvEpisode.TvShowKey = tvShowKey;
                var createdEp = _tvService.AddTvEpisode(tvEpisode);
                dbEp = createdEp.Entity;
                if (!createdEp.IsSuccess)
                {
                    Log.ErrorFormat("Could not create episode {0} [{1}].  Hashcode was {2}.", string.Format("{0}.S{1}E{2}", requestModel.SeriesName, requestModel.Season, requestModel.Episode), requestModel.FullPath, requestModel.FileHash);
                    return new HttpResponseMessage(HttpStatusCode.Conflict);
                }
            }

            var existingUserEpisode = _tvService.GetUserTvEpisode(dbEp.Key, user.User.Key);

            if (existingUserEpisode == null)
            {
                var userEpisode = Mapper.Map<UserTvEpisode>(requestModel);
                userEpisode.TvEpisodeKey = dbEp.Key;
                userEpisode.OwnerKey = user.User.Key;

                var createdUserEpisode = _tvService.AddUserTvEpisode(userEpisode);
                if (!createdUserEpisode.IsSuccess)
                {
                    Log.ErrorFormat("Could not add episode {0} to user {1}.  Episode ID: {2}",
                                    string.Format("{0}.S{1}E{2}", requestModel.SeriesName, requestModel.Season,
                                                  requestModel.Episode), user.User.Key, userEpisode.TvEpisodeKey);

                    Log.ErrorFormat("Imported File Path: {0}", requestModel.FullPath);
                    Log.ErrorFormat("Imported File Hash: {0}", requestModel.FileHash);

                    var existingUserMovie = dbEp.UserTvEpisodes.FirstOrDefault(x => x.OwnerKey == user.User.Key);
                    if (existingUserMovie != null)
                    {
                        Log.ErrorFormat("Existing File Path: {0}", existingUserMovie.FullPath);
                        Log.ErrorFormat("Existing File Hash: {0}", dbEp.FileHash);
                    }
                    else
                    {
                        Log.Error("No existing episode. BOGUS ERROR!!!!");
                    }

                    return new HttpResponseMessage(HttpStatusCode.Conflict);
                }

                existingUserEpisode = createdUserEpisode.Entity;
            }
            else if(requestModel.UpdateExisting)
            {
                existingUserEpisode.Filename = requestModel.Filename;
                existingUserEpisode.FullPath = requestModel.FullPath;

                _tvService.UpdateUserTvEpisode(existingUserEpisode);
            }

            var response = Request.CreateResponse(HttpStatusCode.Created, Mapper.Map<TvEpisodeDto>(existingUserEpisode));

            return response;
        }
Beispiel #3
0
        private static void PostTvEpisode(TvShowImportRequestModel model)
        {
            var request = HttpRequestMessageHelper.ConstructRequest(HttpMethod.Post, _tvEndpoint, "application/json",
                                                                    _username, _password);

            request.Content = new ObjectContent<TvShowImportRequestModel>(model, new JsonMediaTypeFormatter());
            var response = GetResponse(request);
            if (!response.IsSuccessStatusCode)
            {
                LogMessageFormat("Received status {0} for TV Episode {1} Season {2} Episode {3}", response.StatusCode, model.SeriesName, model.Season, model.Episode);
            }
            else
            {
                LogMessageFormat("Posted TV Episode {0} Season {1} Episode {2} successfully!", model.SeriesName, model.Season, model.Episode);
            }
        }