File size: 8,499 Bytes
e26fba6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MovieTube.Web.Models;
using MovieFinder.Data;
using System.Data.Entity;
using MovieTube.Client.Scraper;
using System.IO;
using MovieTube.Web.Services;
using MovieTube.Web.Repository;
using System.Threading.Tasks;
namespace MovieTube.Web.Repository
{
public class MovieRepository : IMovieRepository
{
private readonly int ThumbsPerPage = 20;
private readonly int ThumbsPerLang = 6;
private readonly IConfigProvider confProvider;
private readonly IImageUrlBuilder imgUrlBuilder;
public MovieRepository()
{
this.confProvider = new ConfigProvider();
this.imgUrlBuilder = new ImageUrlBuilder();
}
public ThumbNailVm List(string term, string language, int? year, int? page)
{
year = !year.HasValue ? 0 : year;
page = (!page.HasValue || page < 0) ? 0 : page;
List<MovieThumbnailVm> movies = null;
if (String.IsNullOrWhiteSpace(term) &&
String.IsNullOrWhiteSpace(language))
{
movies = QueryMovies(term, "ml", year, page, ThumbsPerLang);
movies.AddRange(QueryMovies(term, "hi", year, page, ThumbsPerLang));
movies.AddRange(QueryMovies(term, "ta", year, page, ThumbsPerLang));
movies.AddRange(QueryMovies(term, "te", year, page, ThumbsPerLang));
}
else
movies = QueryMovies(term, language, year.Value, page, ThumbsPerPage);
return new ThumbNailVm
{
Thumbnails = movies,
NextPage = movies.Count < ThumbsPerPage ? -1 : page.Value + 1
};
}
public List<MovieThumbnailVm> QueryMovies(string term, string langCode, int? year, int? page, int count)
{
using (var db = new MovieFinderEntities())
{
try
{
IQueryable<Movie> movies = null;
if (!String.IsNullOrWhiteSpace(langCode))
movies = db.Movies.Where(x => x.LanguageCode == langCode);
if (!String.IsNullOrWhiteSpace(term))
movies = (movies == null ? db.Movies : movies).Where(x => x.Name.StartsWith(term));
if (year != 0)
movies = (movies == null ? db.Movies : movies).Where(x => x.ReleaseDate.Year == year);
if (movies == null)
movies = db.Movies;
var m = movies.OrderByDescending(x => x.ReleaseDate)
.ThenByDescending(x => x.CreateDate)
.Skip(page.Value * count)
.Take(count)
.ToList()
.Select(x => new MovieThumbnailVm
{
ImageUrl = String.IsNullOrWhiteSpace(x.ImageLocalUrl) ? x.ImageUrl : imgUrlBuilder.Build(x.ImageLocalUrl),
PostedBy = "Admin",
PostedDate = x.CreateDate.ToShortDateString(),
Title = x.Name,
Language = GetLanguage(x.LanguageCode),
ReleasedYear = x.ReleaseDate.Year,
Id = x.UniqueID,
Url = String.Format("{0}/Watch/{1}/{2}/{3}/{4}",confProvider.RootUrl,
GetLanguage(x.LanguageCode), x.ReleaseDate.Year, x.UniqueID, x.Name),
ViewCount = x.ViewCount,
LikeCount = x.LikeCount
}).ToList();
return m;
}
catch
{
return null;
}
}
}
public MovieVm QueryMovie(string id, bool updateStat = false)
{
using (var db = new MovieFinderEntities())
{
try
{
var movie = db.Movies
.Include(x => x.MovieLinks)
.Where(x => x.UniqueID == id &&
x.MovieLinks.Any(y => y.FailedAttempts < 5))
.ToList()
.Select(x => new MovieVm{
ImageUrl = String.IsNullOrWhiteSpace(x.ImageLocalUrl) ? x.ImageUrl : imgUrlBuilder.Build(x.ImageLocalUrl),
PostedBy = "Admin",
Description = x.Description,
PostedDate = x.CreateDate.ToShortDateString(),
Title = x.Name,
Language = GetLanguage(x.LanguageCode),
ReleasedYear = x.ReleaseDate.Year,
Id = x.UniqueID,
Url = String.Format("{0}/Watch/{1}/{2}/{3}/{4}", confProvider.RootUrl,
GetLanguage(x.LanguageCode), x.ReleaseDate.Year, x.UniqueID, x.Name),
ViewCount = x.ViewCount,
LikeCount = x.LikeCount,
Links = x.MovieLinks.Where(z => z.IsWebSupported).Select(y => new VideoLinkVm{
HostSite = y.DownloadSiteID,
Title = ShortenLinkTitle(y.LinkTitle),
Url = VideoScraperBase.GetScraper(y.DowloadUrl).GetFlashUrl(y.DowloadUrl),
ID = y.ID,
PartID = y.PartID,
PartIndex = y.PartIndex
}).ToList()
}).FirstOrDefault();
movie.Links.Sort();
if (updateStat)
{
var m = db.Movies.Single(x => x.UniqueID == id);
m.ViewCount++;
db.SaveChanges();
}
return movie;
}
catch
{
return null;
}
}
}
private string ShortenLinkTitle(string title)
{
if (String.IsNullOrWhiteSpace(title))
return title;
title = title.ToLower();
var text = ScraperBase.Substring(title, "part");
if (String.IsNullOrWhiteSpace(text))
return " Full Movie";
return "Part - " + text;
}
private string GetLanguage(string langCode)
{
switch (langCode)
{
case "hi":
return "Hindi";
case "ml":
return "Malayalam";
case "te":
return "Telugu";
case "ta":
return "Tamil";
default:
return "Unknown";
}
}
public void RemoveLink(string link)
{
Task.Factory.StartNew(() =>
{
try
{
using (var db = new MovieFinderEntities())
{
var ml = db.MovieLinks.FirstOrDefault(x => x.DowloadUrl == link && x.FailedAttempts <= 3);
if (ml != null)
{
if (VideoScraperBase.ValidateUrl(ml.DowloadUrl) == MovieTube.Client.Scraper.ScraperResult.VideoDoesNotExist)
{
ml.FailedAttempts = 5;
ml.LastValidatedBy = new Guid();
db.SaveChanges();
}
}
}
}
catch { }
});
}
}
} |