public void SaveDetails(Torrent torrent, bool isRemoved)
        {
            using (var db = new DbTorronto())
            {
                var filter = db.Torrent
                             .Where(x => x.ID == torrent.ID)
                             .Set(f => f.Updated, DateTime.UtcNow)
                             .Set(f => f.IsDetailed, true);

                if (isRemoved)
                {
                    filter = filter.Set(f => f.IsRemoved, true);
                }
                else
                {
                    filter = filter
                             .Set(f => f.ImdbID, torrent.ImdbID)
                             .Set(f => f.KinopoiskID, torrent.KinopoiskID)
                             .Set(f => f.VideoQuality, torrent.VideoQuality)
                             .Set(f => f.AudioQuality, torrent.AudioQuality)
                             .Set(f => f.Translation, torrent.Translation)
                             .Set(f => f.IsRemoved, false);
                }

                filter.Update();
            }
        }
Beispiel #2
0
        public List <Movie> CreateMoviesFromTorrents()
        {
            List <Movie> movies;

            using (var db = new DbTorronto())
            {
                var ids = db.Torrent
                          .Where(t => t.MovieID == null && t.KinopoiskID != null)
                          .Select(t => t.KinopoiskID)
                          .Distinct()
                          .Take(100)
                          .ToList();

                movies = ids
                         .Where(id => db.Movie.FirstOrDefault(m => m.KinopoiskID == id) == null)
                         .Select(kinopoiskID => new Movie
                {
                    Title       = "Unknown yet",
                    KinopoiskID = kinopoiskID
                })
                         .ToList();
            }

            SaveMovies(movies, null);

            return(movies);
        }
 public UserProfile GetProfile(int?userId)
 {
     using (var db = new DbTorronto())
     {
         return(db.User
                .Where(u => u.ID == userId)
                .Select(u => new UserProfile
         {
             ID = u.ID,
             DisplayName = u.DisplayName,
             Email = u.Email,
             FilterAudio = u.FilterAudio,
             FilterTraslation = u.FilterTraslation,
             FilterVideo = u.FilterVideo,
             FilterSizes = u.FilterSizes,
             Identities = db.UserIdentity
                          .Where(i => i.UserID == u.ID)
                          .Select(i => new UserProfile.Identity
             {
                 Email = i.Email,
                 AuthProviderName = i.AuthProviderName,
                 AuthProviderID = i.AuthProviderID,
                 DisplayName = i.DisplayName
             }),
             RssHash = Sql2.MD5(Sql.ConvertTo <string> .From(u.Identifier))
         })
                .FirstOrDefault());
     }
 }
Beispiel #4
0
        public void RemoveMovieUserLink(int movieId, int?userId, bool?waitlist = null, bool?watched = null, bool?dontwant = null)
        {
            using (var db = new DbTorronto())
            {
                var update = db.MovieUser
                             .Where(mu => mu.UserID == userId && mu.MovieID == movieId)
                             .Set(f => f.MovieID, movieId);

                if (waitlist == true)
                {
                    update = update.Set(f => f.IsWaitlist, false);
                }

                if (watched == true)
                {
                    update = update
                             .Set(f => f.IsWatched, false)
                             .Set(f => f.Mark, (int?)null);
                }

                if (dontwant == true)
                {
                    update = update
                             .Set(f => f.IsDontWant, false);
                }

                update.Update();
            }
        }
Beispiel #5
0
        private static void PgMigrate()
        {
            Console.WriteLine("Migration started");

            using (var db = new DbTorronto())
                using (var postgres = new DbTorronto("torrontopg"))
                {
                    postgres.Execute("select truncate_tables('postgres')");

                    TransferItemsWithId <Movie>(db, postgres, "movies", true);
                    TransferItemsWithId <Torrent>(db, postgres, "torrents", true);
                    TransferItemsWithId <Genre>(db, postgres, "genres", true);
                    TransferItemsWithId <Person>(db, postgres, "persons", true);
                    TransferItemsWithId <User>(db, postgres, "users", true);

                    TransferItemsWithId <MovieGenre>(db, postgres, "movies_genres", false);
                    TransferItemsWithId <MoviePerson>(db, postgres, "movies_persons", false);
                    TransferItemsWithId <MovieRecommendation>(db, postgres, "movies_recommendations", false);
                    TransferItemsWithId <MovieUser>(db, postgres, "movies_users", true);
                    TransferItemsWithId <TorrentUser>(db, postgres, "torrents_users", false);
                    TransferItemsWithId <UserIdentity>(db, postgres, "user_identities", true);
                }

            Console.WriteLine("Migration finished");
        }
Beispiel #6
0
        private static void TransferItemsWithId <T>(DbTorronto db, DbTorronto postgres, string tableName, bool resetSeq) where T : class
        {
            List <T> items;
            var      offset = 0;
            var      count  = 100;

            var options = new BulkCopyOptions
            {
                KeepIdentity = true
            };

            Console.WriteLine("TABLE {0}", tableName);
            do
            {
                items = db.GetTable <T>()
                        .Skip(offset)
                        .Take(count)
                        .ToList();

                postgres.BulkCopy(options, items);

                offset += count;

                Console.WriteLine("processed: {0}", offset);
            } while (items.Count > 0);

            if (resetSeq)
            {
                postgres.Execute(string.Format(@"
                    SELECT  
                        setval(pg_get_serial_sequence('{0}', 'id'), 
                        (SELECT MAX(id) FROM {0}));
                    ", tableName));
            }
        }
 public User GetByCredentials(string email, string password)
 {
     using (var db = new DbTorronto())
     {
         return(db.User
                .FirstOrDefault(x => x.Email == email && x.Password == password));
     }
 }
 public Torrent GetById(int torrentId)
 {
     using (var db = new DbTorronto())
     {
         return(db.Torrent
                .FirstOrDefault(m => m.ID == torrentId));
     }
 }
Beispiel #9
0
        public void ProcessDetailsSingle(int torrentId)
        {
            _logger.Info("Detailing torrent #{0}", torrentId);

            Torrent torrent;

            using (var db = new DbTorronto())
            {
                torrent = db.Torrent.First(t => t.ID == torrentId);
            }

            if (torrent == null)
            {
                _logger.Warn("No such torrent #{0}", torrentId);
                return;
            }

            using (var client = GetClient())
            {
                var response = client.GetAsync("torrent/" + torrent.SiteID).Result;

                if (response.IsSuccessStatusCode)
                {
                    var content = response.Content.ReadAsStringAsync().Result;
                    var dom     = CQ.Create(content, Encoding.UTF8);

                    var links = dom["#details a"];
                    var title = (dom["title"].Select(x => x.InnerText).FirstOrDefault() ?? string.Empty).ToLower();

                    int?imdbId, kinopoiskId;

                    FillExternalMovieIds(links, out imdbId, out kinopoiskId);

                    var quality = _qualityDetector.Detect(dom);

                    if (torrent.Category == TorrentCategory.Russian)
                    {
                        quality.TranslationQuality = Translation.Dub;
                    }

                    torrent.ImdbID       = imdbId;
                    torrent.KinopoiskID  = kinopoiskId;
                    torrent.VideoQuality = quality.VideoQuality;
                    torrent.AudioQuality = quality.AudioQuality;
                    torrent.Translation  = quality.TranslationQuality;

                    var isRemoved = title.Contains("не существует");

                    _torrentService.SaveDetails(torrent, isRemoved);

                    QueueService.AddTorrentForReindex(torrentId);
                    QueueService.AddTorrentForMatch(torrentId);

                    File.WriteAllText(Path.Combine("html", "torrents", torrentId + ".html"), content);
                }
            }
        }
 public static void InsertMovieUser(this DbTorronto db, int movieId, int?userId)
 {
     db.Insert(new MovieUser
     {
         Created = DateTime.UtcNow,
         MovieID = movieId,
         UserID  = userId.GetValueOrDefault()
     });
 }
Beispiel #11
0
 public Movie GetById(int movieId)
 {
     using (var db = new DbTorronto())
     {
         return(db.Movie
                .NoCopyrighted()
                .FirstOrDefault(m => m.ID == movieId));
     }
 }
Beispiel #12
0
        public User GetByIdentity(string providerName, string providerId)
        {
            using (var db = new DbTorronto())
            {
                var identity = db.UserIdentity
                               .LoadWith(i => i.User)
                               .FirstOrDefault(i => i.AuthProviderName == providerName &&
                                               i.AuthProviderID == providerId);

                return(identity?.User);
            }
        }
        public static void UpdateBestVideoQuality(DbTorronto db, Movie movie)
        {
            var videoQuality = db.Torrent
                               .Where(t => t.MovieID == movie.ID)
                               .Select(t => (VideoQuality?)t.VideoQuality)
                               .Max() ?? VideoQuality.Unknown;

            db.Movie
            .Where(m => m.ID == movie.ID)
            .Set(f => f.BestVideoQuality, videoQuality)
            .Update();
        }
Beispiel #14
0
 public void UpdateProfile(int?userId, UserProfile profile)
 {
     using (var db = new DbTorronto())
     {
         db.User
         .Where(u => u.ID == userId)
         .Set(f => f.FilterVideo, profile.FilterVideo)
         .Set(f => f.FilterAudio, profile.FilterAudio)
         .Set(f => f.FilterTraslation, profile.FilterTraslation)
         .Set(f => f.FilterSizes, profile.FilterSizes)
         .Update();
     }
 }
        public IEnumerable <RssItem> GetPersonalFeed(string userHash)
        {
            using (var db = new DbTorronto())
            {
                var user = db.User
                           .FirstOrDefault(u => Sql.Like(Sql2.MD5(Sql.ConvertTo <string> .From(u.Identifier)), userHash));

                if (user == null)
                {
                    return(Enumerable.Empty <RssItem>());
                }

                var filter = db.TorrentUser
                             .Where(tu => tu.UserID == user.ID && tu.AddedRss != null && Sql2.IsNullOrFalse(tu.Torrent.Movie.IsCopyrighted));

                var orderedByAdded = filter
                                     .OrderByDescending(x => x.AddedRss)
                                     .Select(x => new RssItem
                {
                    Title       = TorrentTitle(x.Torrent.Movie.Title, x.Torrent.Title),
                    Link        = TorrentUrl(x.Torrent.InfoHash),
                    Description = x.Torrent.Movie.Description,
                    PubDate     = x.AddedRss.GetValueOrDefault()
                })
                                     .Take(20)
                                     .ToList();

                var orderedByUpdated = filter
                                       .OrderByDescending(x => x.Torrent.Updated)
                                       .Select(x => new RssItem
                {
                    Title       = TorrentTitle(x.Torrent.Movie.Title, x.Torrent.Title),
                    Link        = TorrentUrl(x.Torrent.InfoHash),
                    Description = x.Torrent.Movie.Description,
                    PubDate     = x.Torrent.Updated
                })
                                       .Take(20)
                                       .ToList();

                var items = orderedByAdded
                            .Union(orderedByUpdated)
                            .GroupBy(x => x.Link)
                            .Select(g => g.OrderByDescending(x => x.PubDate).First())
                            .OrderByDescending(x => x.PubDate)
                            .Take(20)
                            .ToList();

                return(items);
            }
        }
Beispiel #16
0
        private static void FillExternalMovieIds(CQ links, out int?imdbId, out int?kinopoiskId)
        {
            imdbId      = null;
            kinopoiskId = null;

            foreach (var a in links)
            {
                var href           = a.GetAttribute("href");
                var imdbMatch      = _imdbRegex.Match(href);
                var kinopoiskMatch = _kinopoiskRegex.Match(href);

                if (imdbMatch.Success)
                {
                    imdbId = Convert.ToInt32(imdbMatch.Groups[1].Value);
                }

                if (kinopoiskMatch.Success)
                {
                    kinopoiskId = Convert.ToInt32(kinopoiskMatch.Groups[1].Value);
                }
            }

            if (imdbId != null && kinopoiskId != null)
            {
                return;
            }

            using (var db = new DbTorronto())
            {
                if (kinopoiskId == null && imdbId != null)
                {
                    var tmpImdb      = imdbId.Value;
                    var kinopoiskIds = db.Torrent
                                       .Where(t => t.ImdbID == tmpImdb && t.KinopoiskID != null)
                                       .Select(t => t.KinopoiskID)
                                       .Distinct()
                                       .ToList();

                    if (kinopoiskIds.Count == 1)
                    {
                        kinopoiskId = kinopoiskIds.First();
                        _logger.Info("KPRESTORED {0} -> {1}", imdbId, kinopoiskId);
                    }
                    else
                    {
                        _logger.Info("KPNOTRESTORED {0} -> {1}", imdbId, string.Join(", ", kinopoiskIds));
                    }
                }
            }
        }
        public void AddTorrentUserLink(int torrentID, int?userId, bool subscribe, bool rss)
        {
            if (userId == null)
            {
                return;
            }

            using (var db = new DbTorronto())
            {
                var update = db.TorrentUser
                             .Where(tu => tu.UserID == userId && tu.TorrentID == torrentID)
                             .AsUpdatable();

                if (subscribe)
                {
                    update = update.Set(f => f.IsSubscribed, true);
                }

                if (rss)
                {
                    update = update.Set(f => f.AddedRss, DateTime.UtcNow);
                }

                var affected = update.Update();

                if (affected == 0)
                {
                    var newTu = new TorrentUser
                    {
                        EmailSent = null,
                        UserID    = userId.GetValueOrDefault(),
                        TorrentID = torrentID
                    };

                    if (subscribe)
                    {
                        newTu.IsSubscribed = true;
                    }
                    if (rss)
                    {
                        newTu.AddedRss = DateTime.UtcNow;
                    }

                    db.Insert(newTu);
                }
            }
        }
        private void MatchTorrent(int torrentId)
        {
            using (var db = new DbTorronto())
            {
                _logger.Info("Matching torrent #{0}", torrentId);

                var torrent = db.Torrent.FirstOrDefault(t => t.ID == torrentId);

                if (torrent != null)
                {
                    var predicate = PredicateBuilder.False <Movie>();

                    if (torrent.KinopoiskID != null)
                    {
                        predicate = predicate.Or(m => m.KinopoiskID == torrent.KinopoiskID);
                    }
                    if (torrent.ImdbID != null)
                    {
                        predicate = predicate.Or(m => m.ImdbID == torrent.ImdbID);
                    }

                    var movies = db.Movie.Where(predicate).ToList();

                    if (movies.Count > 1)
                    {
                        _logger.Warn("torrent #{0} matches to multiple movies", torrentId);
                        return;
                    }

                    var movie = movies.FirstOrDefault();

                    if (movie != null)
                    {
                        db.Torrent
                        .Where(x => x.ID == torrent.ID)
                        .Set(f => f.MovieID, movie.ID)
                        .Update();

                        UpdateBestVideoQuality(db, movie);

                        SendNotifications(db, movie, torrent);
                    }
                }
            }
        }
        public void UpdateRatings()
        {
            List <int> unprocessed;

            using (var db = new DbTorronto())
            {
                unprocessed = db.Movie
                              .Where(x => x.RatingLastGather == null || x.RatingLastGather < DateTime.UtcNow.AddDays(-14))
                              .Take(500)
                              .Select(x => x.ID.GetValueOrDefault())
                              .ToList();
            }

            foreach (var movieId in unprocessed)
            {
                QueueService.AddMovieForRating(movieId);
            }
        }
        public void UpdateRecommendations()
        {
            List <int> unprocessed;

            using (var db = new DbTorronto())
            {
                unprocessed = db.Movie
                              .Where(x => x.LastRecommendation == null || x.LastRecommendation < DateTime.UtcNow.AddMonths(-6))
                              .Take(_undetailedMoviesLimit)
                              .Select(x => x.ID.GetValueOrDefault())
                              .ToList();
            }

            foreach (var movieId in unprocessed)
            {
                QueueService.AddMovieForRecommendation(movieId);
            }
        }
Beispiel #21
0
        public MovieItem GetMovieSingle(int?userId, int movieId)
        {
            using (var db = new DbTorronto())
            {
                var item = db.Movie
                           .NoCopyrighted()
                           .SelectMany(m => db.MovieUser.Where(x => x.MovieID == m.ID && x.UserID == userId).DefaultIfEmpty(),
                                       (m, movieUser) => new { movie = m, muUser = movieUser })
                           .FirstOrDefault(x => x.movie.ID == movieId);

                if (item != null)
                {
                    var persons = db.MoviePerson
                                  .Where(mp => mp.MovieID == movieId)
                                  .OrderBy(mp => mp.Position)
                                  .Select(mp => mp.Person);

                    var genres = db.MovieGenre
                                 .Where(mg => mg.MovieID == movieId)
                                 .OrderBy(mg => mg.Position)
                                 .Select(mg => mg.Genre);

                    Movie.AddRelated(item.movie, persons.Take(6));
                    Movie.AddRelated(item.movie, genres);

                    var movieItem = new MovieItem
                    {
                        Self = item.movie
                    };

                    if (item.muUser != null)
                    {
                        movieItem.InWaitList = item.muUser.IsWaitlist;
                        movieItem.IsWatched  = item.muUser.IsWatched;
                        movieItem.Mark       = item.muUser.Mark;
                        movieItem.IsDontWant = item.muUser.IsDontWant;
                    }

                    return(movieItem);
                }
            }

            return(null);
        }
        public TorrentItem GetSingleTorrent(int torrentID, int?userId)
        {
            using (var db = new DbTorronto())
            {
                var item = db.Torrent
                           .LoadWith(t => t.Movie)
                           .FirstOrDefault(torrent => torrent.ID == torrentID && Sql2.IsNullOrFalse(torrent.Movie.IsCopyrighted));

                var subscription = db.TorrentUser
                                   .FirstOrDefault(tu => tu.TorrentID == torrentID && tu.UserID == userId);

                return(new TorrentItem
                {
                    Self = item,
                    IsSubscribed = subscription?.IsSubscribed == true,
                    IsRss = subscription?.AddedRss != null
                });
            }
        }
Beispiel #23
0
        public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
        {
            using (var db = new DbTorronto())
            {
                var user = db.User.FirstOrDefault(x => x.Identifier == identifier);

                if (user != null)
                {
                    return(new TorrontoUserIdentity
                    {
                        UserName = user.Identifier.ToString(),
                        Claims = new[] { "user" },
                        User = user
                    });
                }
            }

            return(null);
        }
Beispiel #24
0
        public IndexModule(
            SearchService searchService,
            TorrentService torrentService
            )
        {
            _torrentService = torrentService;

            Get["/"] = parameters =>
            {
                var user = this.GetUser() ?? new User();

                var model = new
                {
                    User             = user,
                    UserPresent      = user.ID != null,
                    FilterVideo      = (int)user.FilterVideo,
                    FilterAudio      = (int)user.FilterAudio,
                    FilterTraslation = (int)user.FilterTraslation,
                    FilterSizes      = user.FilterSizes,

                    LibScript = Bundle.JavaScript().RenderCachedAssetTag("lib"),
                    AppScript = Bundle.JavaScript().RenderCachedAssetTag("app"),
                    LibCss    = Bundle.Css().RenderCachedAssetTag("css")
                };

                return(View["index", model]);
            };

            Get["/test"] = parameters =>
            {
                using (var db = new DbTorronto())
                {
                    var user    = db.User.FirstOrDefault(x => x.ID == 3);
                    var message = new StringBuilder()
                                  .AppendLine("оПХБЕР");

                    EmailService.SendMail(user, "test", message);
                }
                return(200);
            };

            Get["/rss/{userHash}"] = GetUserRss;
        }
        private void MatchMovie(int movieId)
        {
            using (var db = new DbTorronto())
            {
                _logger.Info("Matching movie #{0}", movieId);

                var movie = db.Movie.FirstOrDefault(t => t.ID == movieId);

                if (movie?.KinopoiskID != null)
                {
                    db.Torrent
                    .Where(t => t.MovieID == null && t.KinopoiskID == movie.KinopoiskID)
                    .Set(f => f.MovieID, movieId)
                    .Update();

                    UpdateBestVideoQuality(db, movie);
                }
            }
        }
Beispiel #26
0
        public void SaveMovies(List <Movie> movies, int?userId)
        {
            using (var db = new DbTorronto())
            {
                foreach (var movie in movies.Where(m => m.KinopoiskID != null))
                {
                    var existing = db.Movie
                                   .FirstOrDefault(x => x.KinopoiskID == movie.KinopoiskID);

                    int movieId;

                    if (existing == null)
                    {
                        movieId = CreateMovie(db, movie);

                        if (movieId == 0)
                        {
                            continue;
                        }

                        QueueService.AddMovieForMatch(movieId);
                    }
                    else
                    {
                        movieId = existing.ID.GetValueOrDefault();

                        db.Movie
                        .Where(x => x.ID == existing.ID)
                        .Set(f => f.Status, movie.Status)
                        .Set(f => f.Updated, DateTime.UtcNow)
                        .Update();
                    }

                    movie.ID = movieId;

                    if (userId != null && !db.MovieUser.Any(x => x.MovieID == movieId && x.UserID == userId))
                    {
                        db.InsertMovieUser(movieId, userId.GetValueOrDefault());
                    }
                }
            }
        }
        public void SaveToDb(List <Torrent> torrents)
        {
            using (var db = new DbTorronto())
            {
                foreach (var torrent in torrents)
                {
                    var existing = db.Torrent
                                   .FirstOrDefault(x => x.SiteID == torrent.SiteID);

                    if (existing == null)
                    {
                        torrent.Updated = DateTime.UtcNow;
                        db.Insert(torrent);
                    }
                    else
                    {
                        db.Torrent
                        .Where(x => x.ID == existing.ID)
                        .Set(f => f.Title, torrent.Title)
                        .Set(f => f.InfoHash, torrent.InfoHash)
                        .Set(f => f.Size, torrent.Size)
                        .Set(f => f.Updated, DateTime.UtcNow)
                        .Update();

                        if (existing.Size != torrent.Size)
                        {
                            var subscribers = from tu in db.TorrentUser
                                              join u in db.User on tu.UserID equals u.ID
                                              where tu.IsSubscribed && tu.TorrentID == existing.ID
                                              select u;

                            existing.Title = torrent.Title;

                            foreach (var subscriber in subscribers)
                            {
                                _emailService.NotifyUserAboutTorrent(subscriber, existing);
                            }
                        }
                    }
                }
            }
        }
        public void ProcessDetails()
        {
            List <int> unprocessed;

            using (var db = new DbTorronto())
            {
                unprocessed = db.Movie
                              .Where(x => !x.IsDetailed)
                              .OrderByDescending(x => x.Created)
                              .Take(_undetailedMoviesLimit)
                              .Select(x => x.ID.GetValueOrDefault())
                              .ToList();
            }

            foreach (var movieId in unprocessed)
            {
                QueueService.AddMovieForDetails(movieId);
                QueueService.AddMovieForRating(movieId);
            }
        }
Beispiel #29
0
        public int CreateMovie(DbTorronto db, Movie movie)
        {
            int movieId = 0;

            movie.Created = DateTime.UtcNow;
            movie.Updated = DateTime.UtcNow;

            try
            {
                movieId = Convert.ToInt32(
                    db.InsertWithIdentity(movie)
                    );
            }
            catch (Exception ex)
            {
                _logger.Warn("MovieService.CreateMovie", ex);
            }

            return(movieId);
        }
        public void RemoveTorrentUserLink(int torrentID, int?userId, bool subscribe, bool rss)
        {
            using (var db = new DbTorronto())
            {
                var update = db.TorrentUser
                             .Where(tu => tu.UserID == userId && tu.TorrentID == torrentID)
                             .Set(f => f.TorrentID, torrentID);

                if (subscribe)
                {
                    update = update.Set(f => f.IsSubscribed, false);
                }

                if (rss)
                {
                    update = update.Set(f => f.AddedRss, (DateTime?)null);
                }

                update.Update();
            }
        }