-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSharpOmdb.cs
More file actions
79 lines (70 loc) · 4.59 KB
/
SharpOmdb.cs
File metadata and controls
79 lines (70 loc) · 4.59 KB
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
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using Frost.SharpOmdbAPI.Models;
using Newtonsoft.Json;
namespace Frost.SharpOmdbAPI {
/// <summary>The lengt of plot to return</summary>
public enum PlotLength {
/// <summary>Short summary</summary>
Short,
/// <summary>Full description</summary>
Full
}
/// <summary>A client for communication with the OmdbAPI service.</summary>
public static class SharpOmdb {
private const string SEARCH_URI = "http://www.omdbapi.com/?s={0}{1}";
private const string GET_IMDB_URI = "http://www.omdbapi.com/?i={0}&plot={1}&tomatoes={2}";
private const string GET_TITLE_URI = "http://www.omdbapi.com/?t={0}{1}&plot={2}&tomatoes={3}";
/// <summary>Gets the by Imdb movie info by Imdb Id.</summary>
/// <param name="id">The ImdbId identifier.</param>
/// <param name="plotLength">Length of the plot.</param>
/// <param name="includeTomatoesInfo">if set to <c>true</c> includes RottenTomatoes information.</param>
/// <returns>Returns an instance of OmdbMovie if successful, otherwise null.</returns>
/// <exception cref="WebException">Throws when there was an error accessing the web API service.</exception>
public static OmdbMovie GetByImdbId(string id, PlotLength plotLength = PlotLength.Short, bool includeTomatoesInfo = false) {
return !string.IsNullOrEmpty(id)
? Download<OmdbMovie>(string.Format(GET_IMDB_URI, id.ToString(CultureInfo.InvariantCulture), plotLength, includeTomatoesInfo))
: null;
}
/// <summary>Gets the by Imdb movie info by movie title.</summary>
/// <param name="title">The movie title to search for.</param>
/// <param name="plotLength">Length of the plot.</param>
/// <param name="includeTomatoesInfo">if set to <c>true</c> includes RottenTomatoes information.</param>
/// <returns>Returns an instance of OmdbMovie if successful, otherwise <c>null</c>.</returns>
/// <exception cref="WebException">Throws when there was an error accessing the web API service.</exception>
public static OmdbMovie GetByTitle(string title, PlotLength plotLength = PlotLength.Short, bool includeTomatoesInfo = false) {
return !string.IsNullOrEmpty(title)
? Download<OmdbMovie>(string.Format(GET_TITLE_URI, WebUtility.UrlEncode(title), null, plotLength, includeTomatoesInfo))
: null;
}
/// <summary>Gets the by Imdb movie info by movie title.</summary>
/// <param name="title">The movie title to search for.</param>
/// <param name="releaseYear">The year the movie was released in.</param>
/// <param name="plotLength">Length of the plot.</param>
/// <param name="includeTomatoesInfo">if set to <c>true</c> includes RottenTomatoes information.</param>
/// <returns>Returns an instance of OmdbMovie if successful, otherwise <c>null</c>.</returns>
/// <exception cref="WebException">Throws when there was an error accessing the web API service.</exception>
public static OmdbMovie GetByTitle(string title, int releaseYear, PlotLength plotLength = PlotLength.Short, bool includeTomatoesInfo = false) {
return !string.IsNullOrEmpty(title)
? Download<OmdbMovie>(string.Format(GET_TITLE_URI, WebUtility.UrlEncode(title), "&y=" + releaseYear.ToString(CultureInfo.InvariantCulture), plotLength, includeTomatoesInfo))
: null;
}
/// <summary>Searches for movies matching the title and year.</summary>
/// <param name="title">The movie title to search for.</param>
/// <param name="releaseYear">The year the movie was released in.</param>
/// <returns>Returns an <see cref="IEnumerable{OmdbSearch}"/> if succesful, otherwise <c>null</c>.</returns>
/// <exception cref="WebException">Throws when there was an error accessing the web API service.</exception>
public static IEnumerable<OmdbSearch> Search(string title, int releaseYear = 0) {
return !string.IsNullOrEmpty(title)
? Download<OmdbSearchInfo>(string.Format(SEARCH_URI, WebUtility.UrlEncode(title), releaseYear > 0 ? "&y=" + releaseYear.ToString(CultureInfo.InvariantCulture) : null)).Search
: null;
}
private static T Download<T>(string uri) {
using (WebClient cli = new WebClient()) {
string json = cli.DownloadString(uri);
return JsonConvert.DeserializeObject<T>(json);
}
}
}
}