1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-25 22:46:31 -04:00

Fixed: Replaced trakt with tvdb as data source

This commit is contained in:
Keivan Beigi
2014-12-30 14:08:08 -08:00
parent 0f56bfac2f
commit 19f5427dbf
25 changed files with 1589 additions and 4 deletions
+279
View File
@@ -0,0 +1,279 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml.Linq;
using TVDBSharp.Models.DAO;
using TVDBSharp.Models.Enums;
using TVDBSharp.Utilities;
namespace TVDBSharp.Models
{
/// <summary>
/// Provides builder classes for complex entities.
/// </summary>
public class Builder
{
private const string UriPrefix = "http://thetvdb.com/banners/";
private readonly IDataProvider _dataProvider;
/// <summary>
/// Initializes a new Builder object with the given <see cref="IDataProvider" />.
/// </summary>
/// <param name="dataProvider">The DataProvider used to retrieve XML responses.</param>
public Builder(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
}
/// <summary>
/// Builds a show object from the given show ID.
/// </summary>
/// <param name="showID">ID of the show to serialize into a <see cref="Show" /> object.</param>
/// <returns>Returns the Show object.</returns>
public Show BuildShow(int showID)
{
var builder = new ShowBuilder(_dataProvider.GetShow(showID));
return builder.GetResult();
}
public Episode BuildEpisode(int episodeId, string lang)
{
var builder = new EpisodeBuilder(_dataProvider.GetEpisode(episodeId, lang).Descendants("Episode").First());
return builder.GetResult();
}
public Updates BuildUpdates(Interval interval)
{
var builder = new UpdatesBuilder(_dataProvider.GetUpdates(interval));
return builder.GetResult();
}
/// <summary>
/// Returns a list of <see cref="Show" /> objects that match the given query.
/// </summary>
/// <param name="query">Query the search is performed with.</param>
/// <param name="results">Maximal amount of shows the resultset should return.</param>
/// <returns>Returns a list of show objects.</returns>
public List<Show> Search(string query, int results)
{
var shows = new List<Show>();
var doc = _dataProvider.Search(query);
foreach (var element in doc.Descendants("Series").Take(results))
{
var id = int.Parse(element.GetXmlData("seriesid"));
try
{
var response = _dataProvider.GetShow(id);
shows.Add(new ShowBuilder(response).GetResult());
}
catch (WebException ex)
{
}
}
return shows;
}
private static Uri GetBannerUri(string uriSuffix)
{
return new Uri(UriPrefix + uriSuffix, UriKind.Absolute);
}
private class ShowBuilder
{
private readonly Show _show;
public ShowBuilder(XDocument doc)
{
_show = new Show();
_show.Id = int.Parse(doc.GetSeriesData("id"));
_show.ImdbId = doc.GetSeriesData("IMDB_ID");
_show.Name = doc.GetSeriesData("SeriesName");
_show.Language = doc.GetSeriesData("Language");
_show.Network = doc.GetSeriesData("Network");
_show.Description = doc.GetSeriesData("Overview");
_show.Rating = string.IsNullOrWhiteSpace(doc.GetSeriesData("Rating"))
? (double?) null
: Convert.ToDouble(doc.GetSeriesData("Rating"),
System.Globalization.CultureInfo.InvariantCulture);
_show.RatingCount = string.IsNullOrWhiteSpace(doc.GetSeriesData("RatingCount"))
? 0
: Convert.ToInt32(doc.GetSeriesData("RatingCount"));
_show.Runtime = string.IsNullOrWhiteSpace(doc.GetSeriesData("Runtime"))
? (int?) null
: Convert.ToInt32(doc.GetSeriesData("Runtime"));
_show.Banner = GetBannerUri(doc.GetSeriesData("banner"));
_show.Fanart = GetBannerUri(doc.GetSeriesData("fanart"));
_show.LastUpdated = string.IsNullOrWhiteSpace(doc.GetSeriesData("lastupdated"))
? (long?) null
: Convert.ToInt64(doc.GetSeriesData("lastupdated"));
_show.Poster = GetBannerUri(doc.GetSeriesData("poster"));
_show.Zap2ItID = doc.GetSeriesData("zap2it_id");
_show.FirstAired = string.IsNullOrWhiteSpace(doc.GetSeriesData("FirstAired"))
? (DateTime?) null
: Utils.ParseDate(doc.GetSeriesData("FirstAired"));
_show.AirTime = string.IsNullOrWhiteSpace(doc.GetSeriesData("Airs_Time"))
? (TimeSpan?) null
: Utils.ParseTime(doc.GetSeriesData("Airs_Time"));
_show.AirDay = string.IsNullOrWhiteSpace(doc.GetSeriesData("Airs_DayOfWeek"))
? (Frequency?) null
: (Frequency) Enum.Parse(typeof (Frequency), doc.GetSeriesData("Airs_DayOfWeek"));
_show.Status = string.IsNullOrWhiteSpace(doc.GetSeriesData("Status"))
? Status.Unknown
: (Status) Enum.Parse(typeof (Status), doc.GetSeriesData("Status"));
_show.ContentRating = Utils.GetContentRating(doc.GetSeriesData("ContentRating"));
_show.Genres =
new List<string>(doc.GetSeriesData("Genre")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries));
_show.Actors =
new List<string>(doc.GetSeriesData("Actors")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries));
_show.Episodes = new EpisodesBuilder(doc).BuildEpisodes();
}
public Show GetResult()
{
return _show;
}
}
public class EpisodeBuilder
{
private readonly Episode _episode;
public EpisodeBuilder(XElement episodeNode)
{
_episode = new Episode
{
Id = int.Parse(episodeNode.GetXmlData("id")),
Title = episodeNode.GetXmlData("EpisodeName"),
Description = episodeNode.GetXmlData("Overview"),
EpisodeNumber = int.Parse(episodeNode.GetXmlData("EpisodeNumber")),
Director = episodeNode.GetXmlData("Director"),
EpisodeImage = GetBannerUri(episodeNode.GetXmlData("filename")),
FirstAired =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("FirstAired"))
? (DateTime?) null
: Utils.ParseDate(episodeNode.GetXmlData("FirstAired")),
GuestStars =
new List<string>(episodeNode.GetXmlData("GuestStars")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)),
ImdbId = episodeNode.GetXmlData("IMDB_ID"),
Language = episodeNode.GetXmlData("Language"),
LastUpdated =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("lastupdated"))
? 0L
: Convert.ToInt64(episodeNode.GetXmlData("lastupdated")),
Rating =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("Rating"))
? (double?) null
: Convert.ToDouble(episodeNode.GetXmlData("Rating"),
System.Globalization.CultureInfo.InvariantCulture),
RatingCount =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("RatingCount"))
? 0
: Convert.ToInt32(episodeNode.GetXmlData("RatingCount")),
SeasonId = int.Parse(episodeNode.GetXmlData("seasonid")),
SeasonNumber = int.Parse(episodeNode.GetXmlData("SeasonNumber")),
SeriesId = int.Parse(episodeNode.GetXmlData("seriesid")),
ThumbHeight =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("thumb_height"))
? (int?) null
: Convert.ToInt32(episodeNode.GetXmlData("thumb_height")),
ThumbWidth =
string.IsNullOrWhiteSpace(episodeNode.GetXmlData("thumb_width"))
? (int?) null
: Convert.ToInt32(episodeNode.GetXmlData("thumb_width")),
TmsExport = episodeNode.GetXmlData("tms_export"),
Writers =
new List<string>(episodeNode.GetXmlData("Writer")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries))
};
}
public Episode GetResult()
{
return _episode;
}
}
private class EpisodesBuilder
{
private readonly XDocument _doc;
public EpisodesBuilder(XDocument doc)
{
_doc = doc;
}
public List<Episode> BuildEpisodes()
{
var result = new List<Episode>();
foreach (var episodeNode in _doc.Descendants("Episode"))
{
var episode = new EpisodeBuilder(episodeNode).GetResult();
result.Add(episode);
}
return result;
}
}
public class UpdatesBuilder
{
private readonly Updates _updates;
public UpdatesBuilder(XDocument doc)
{
if (doc.Root != null)
{
_updates = new Updates
{
Time = int.Parse(doc.Root.Attribute("time").Value),
UpdatedSeries = doc.Root.Elements("Series")
.Select(elt => new UpdatedSerie
{
Id = int.Parse(elt.Element("id").Value),
Time = int.Parse(elt.Element("time").Value)
})
.ToList(),
UpdatedEpisodes = doc.Root.Elements("Episode")
.Select(elt => new UpdatedEpisode
{
Id = int.Parse(elt.Element("id").Value),
SerieId = int.Parse(elt.Element("Series").Value),
Time = int.Parse(elt.Element("time").Value)
})
.ToList(),
UpdatedBanners = doc.Root.Elements("Banner")
.Select(elt => new UpdatedBanner
{
SerieId = int.Parse(elt.Element("Series").Value),
Format = elt.Element("format").Value,
Language =
elt.Elements("language").Select(n => n.Value).FirstOrDefault() ?? string.Empty,
Path = elt.Element("path").Value,
Type = elt.Element("type").Value,
SeasonNumber = elt.Elements("SeasonNumber").Any()
? int.Parse(elt.Element("SeasonNumber").Value)
: (int?) null,
Time = int.Parse(elt.Element("time").Value)
})
.ToList()
};
}
}
public Updates GetResult()
{
return _updates;
}
}
}
}
+43
View File
@@ -0,0 +1,43 @@
using System.IO;
using System.Net;
using System.Xml.Linq;
using TVDBSharp.Models.Enums;
namespace TVDBSharp.Models.DAO
{
/// <summary>
/// Standard implementation of the <see cref="IDataProvider" /> interface.
/// </summary>
public class DataProvider : IDataProvider
{
public string ApiKey { get; set; }
private const string BaseUrl = "http://thetvdb.com";
public XDocument GetShow(int showID)
{
return GetXDocumentFromUrl(string.Format("{0}/api/{1}/series/{2}/all/", BaseUrl, ApiKey, showID));
}
public XDocument GetEpisode(int episodeId, string lang)
{
return GetXDocumentFromUrl(string.Format("{0}/api/{1}/episodes/{2}/{3}.xml", BaseUrl, ApiKey, episodeId, lang));
}
public XDocument GetUpdates(Interval interval)
{
return GetXDocumentFromUrl(string.Format("{0}/api/{1}/updates/updates_{2}.xml", BaseUrl, ApiKey, IntervalHelpers.Print(interval)));
}
public XDocument Search(string query)
{
return GetXDocumentFromUrl(string.Format("{0}/api/GetSeries.php?seriesname={1}", BaseUrl, query));
}
private static XDocument GetXDocumentFromUrl(string url)
{
using (var web = new WebClient())
using (var memoryStream = new MemoryStream(web.DownloadData(url)))
return XDocument.Load(memoryStream);
}
}
}
+45
View File
@@ -0,0 +1,45 @@
using System.Xml.Linq;
using TVDBSharp.Models.Enums;
namespace TVDBSharp.Models.DAO
{
/// <summary>
/// Defines a Dataprovider API.
/// </summary>
public interface IDataProvider
{
/// <summary>
/// The API key provided by TVDB.
/// </summary>
string ApiKey { get; set; }
/// <summary>
/// Retrieves the show with the given id and returns the corresponding XML tree.
/// </summary>
/// <param name="showID">ID of the show you wish to lookup.</param>
/// <returns>Returns an XML tree of the show object.</returns>
XDocument GetShow(int showID);
/// <summary>
/// Retrieves the episode with the given id and returns the corresponding XML tree.
/// </summary>
/// <param name="episodeId">ID of the episode to retrieve</param>
/// <param name="lang">ISO 639-1 language code of the episode</param>
/// <returns>XML tree of the episode object</returns>
XDocument GetEpisode(int episodeId, string lang);
/// <summary>
/// Retrieves updates on tvdb (Shows, Episodes and Banners)
/// </summary>
/// <param name="interval">The interval for the updates</param>
/// <returns>XML tree of the Updates object</returns>
XDocument GetUpdates(Interval interval);
/// <summary>
/// Returns an XML tree representing a search query for the given parameter.
/// </summary>
/// <param name="query">Query to perform the search with.</param>
/// <returns>Returns an XML tree of a search result.</returns>
XDocument Search(string query);
}
}
@@ -0,0 +1,43 @@
namespace TVDBSharp.Models.Enums
{
/// <summary>
/// Different content ratings. View <c>http://en.wikipedia.org/wiki/TV_Parental_Guidelines</c> for more info.
/// </summary>
public enum ContentRating
{
/// <summary>
/// Not suitable for children under 14.
/// </summary>
TV14,
/// <summary>
/// This program contains material that parents may find unsuitable for younger children.
/// </summary>
TVPG,
/// <summary>
/// This program is designed to be appropriate for all children.
/// </summary>
TVY,
/// <summary>
/// This program is designed for children age 7 and above.
/// </summary>
TVY7,
/// <summary>
/// Most parents would find this program suitable for all ages.
/// </summary>
TVG,
/// <summary>
/// This program is specifically designed to be viewed by adults and therefore may be unsuitable for children under 17.
/// </summary>
TVMA,
/// <summary>
/// Default value if no rating is given.
/// </summary>
Unknown
}
}
+14
View File
@@ -0,0 +1,14 @@
namespace TVDBSharp.Models.Enums
{
public enum Frequency
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
Daily
}
}
+32
View File
@@ -0,0 +1,32 @@
using System;
namespace TVDBSharp.Models.Enums
{
public enum Interval
{
Day,
Week,
Month,
All
}
public static class IntervalHelpers
{
public static string Print(Interval interval)
{
switch (interval)
{
case Interval.Day:
return "day";
case Interval.Week:
return "week";
case Interval.Month:
return "month";
case Interval.All:
return "all";
default:
throw new ArgumentException("Unsupported interval enum: " + interval);
}
}
}
}
+23
View File
@@ -0,0 +1,23 @@
namespace TVDBSharp.Models.Enums
{
/// <summary>
/// Describes the current status of a show.
/// </summary>
public enum Status
{
/// <summary>
/// No more episodes are being released.
/// </summary>
Ended,
/// <summary>
/// The show is ongoing.
/// </summary>
Continuing,
/// <summary>
/// Default value if no status is specified.
/// </summary>
Unknown
}
}
+111
View File
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
namespace TVDBSharp.Models
{
/// <summary>
/// Entity describing an episode of a <see cref="Show" />show.
/// </summary>
public class Episode
{
/// <summary>
/// Unique identifier for an episode.
/// </summary>
public int Id { get; set; }
/// <summary>
/// Director of the episode.
/// </summary>
public string Director { get; set; }
/// <summary>
/// This episode's title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// This episode's number in the appropriate season.
/// </summary>
public int EpisodeNumber { get; set; }
/// <summary>
/// This episode's season.
/// </summary>
public int SeasonNumber { get; set; }
/// <summary>
/// The date of the first time this episode has aired.
/// </summary>
public DateTime? FirstAired { get; set; }
/// <summary>
/// A list of guest stars.
/// </summary>
public List<string> GuestStars { get; set; }
/// <summary>
/// Unique identifier on IMDb.
/// </summary>
public string ImdbId { get; set; }
/// <summary>
/// Main language spoken in the episode.
/// </summary>
public string Language { get; set; }
/// <summary>
/// A short description of the episode.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Average rating as shown on IMDb.
/// </summary>
public double? Rating { get; set; }
/// <summary>
/// Amount of votes cast.
/// </summary>
public int RatingCount { get; set; }
/// <summary>
/// Writers(s) of the episode.
/// </summary>
public List<String> Writers { get; set; }
/// <summary>
/// Let me know if you find out what this is.
/// </summary>
public Uri EpisodeImage { get; set; }
/// <summary>
/// Timestamp of the last update to this episode.
/// </summary>
public long? LastUpdated { get; set; }
/// <summary>
/// Unique identifier of the season.
/// </summary>
public int SeasonId { get; set; }
/// <summary>
/// Unique identifier of the show.
/// </summary>
public int SeriesId { get; set; }
/// <summary>
/// Height dimension of the thumbnail in pixels.
/// </summary>
public int? ThumbHeight { get; set; }
/// <summary>
/// Width dimension of the thumbnail in pixels;
/// </summary>
public int? ThumbWidth { get; set; }
/// <summary>
/// Let me know if you find out what this is.
/// </summary>
public string TmsExport { get; set; }
}
}
+122
View File
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using TVDBSharp.Models.Enums;
namespace TVDBSharp.Models
{
/// <summary>
/// Entity describing a show.
/// </summary>
public class Show
{
/// <summary>
/// Unique identifier used by IMDb.
/// </summary>
public string ImdbId { get; set; }
/// <summary>
/// Unique identifier used by TVDB and TVDBSharp.
/// </summary>
public int Id { get; set; }
/// <summary>
/// List of all actors in the show.
/// </summary>
public List<string> Actors { get; set; }
/// <summary>
/// Day of the week when the show airs.
/// </summary>
public Frequency? AirDay { get; set; }
/// <summary>
/// Time of the day when the show airs.
/// </summary>
public TimeSpan? AirTime { get; set; }
/// <summary>
/// Rating of the content provided by an official organ.
/// </summary>
public ContentRating ContentRating { get; set; }
/// <summary>
/// The date the show aired for the first time.
/// </summary>
public DateTime? FirstAired { get; set; }
/// <summary>
/// A list of genres the show is associated with.
/// </summary>
public List<string> Genres { get; set; }
/// <summary>
/// Main language of the show.
/// </summary>
public string Language { get; set; }
/// <summary>
/// Network that broadcasts the show.
/// </summary>
public string Network { get; set; }
/// <summary>
/// A short overview of the show.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Average rating as shown on IMDb.
/// </summary>
public double? Rating { get; set; }
/// <summary>
/// Amount of votes cast.
/// </summary>
public int RatingCount { get; set; }
/// <summary>
/// Let me know if you find out what this is.
/// </summary>
public int? Runtime { get; set; }
/// <summary>
/// Name of the show.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Current status of the show.
/// </summary>
public Status Status { get; set; }
/// <summary>
/// Link to the banner image.
/// </summary>
public Uri Banner { get; set; }
/// <summary>
/// Link to a fanart image.
/// </summary>
public Uri Fanart { get; set; }
/// <summary>
/// Timestamp of the latest update.
/// </summary>
public long? LastUpdated { get; set; }
/// <summary>
/// Let me know if you find out what this is.
/// </summary>
public Uri Poster { get; set; }
/// <summary>
/// No clue
/// </summary>
public string Zap2ItID { get; set; }
/// <summary>
/// A list of all episodes associated with this show.
/// </summary>
public List<Episode> Episodes { get; set; }
}
}
+49
View File
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
namespace TVDBSharp.Models
{
public class Updates : UnixTimestampedObject
{
public List<UpdatedBanner> UpdatedBanners { get; set; }
public List<UpdatedEpisode> UpdatedEpisodes { get; set; }
public List<UpdatedSerie> UpdatedSeries { get; set; }
}
public class UnixTimestampedObject
{
private static DateTime _startDate = new DateTime(1970, 1, 1);
private int _unixTimestamp;
public DateTime Timestamp
{
get { return _startDate.AddSeconds(_unixTimestamp); }
}
public int Time
{
set { _unixTimestamp = value; }
}
}
public class UpdatedSerie : UnixTimestampedObject
{
public int Id { get; set; }
}
public class UpdatedEpisode : UnixTimestampedObject
{
public int Id { get; set; }
public int SerieId { get; set; }
}
public class UpdatedBanner : UnixTimestampedObject
{
public int SerieId { get; set; }
public string Format { get; set; }
public string Language { get; set; }
public string Path { get; set; }
public string Type { get; set; }
public int? SeasonNumber { get; set; }
}
}