Skip to content

Commit

Permalink
#10: Added support for Import of CSV Watchlist from IMDb.
Browse files Browse the repository at this point in the history
  • Loading branch information
damienhaynes committed Mar 9, 2014
1 parent 5895629 commit 162392b
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 118 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
13 changes: 9 additions & 4 deletions Settings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class AppSettings
const string cTraktPassword = "TraktPassword";
const string cTVDbAccountId = "TVDbAccountId";
const string cTMDbSessionId = "TMDbSessionId";
const string cIMDbFilename = "IMDbFilename";
const string cIMDbRatingsFilename = "IMDbFilename";
const string cIMDbWatchlistFilename = "IMDbWatchlistFilename";
const string cIMDbUsername = "IMDbUsername";
const string cIMDBSyncWatchlist = "IMDBSyncWatchlist";
const string cMarkAsWatched = "MarkAsWatched";
Expand Down Expand Up @@ -56,7 +57,9 @@ public static string TraktPassword

public static string TMDbRequestToken { get; set; }

public static string IMDbFilename { get; set; }
public static string IMDbRatingsFilename { get; set; }

public static string IMDbWatchlistFilename { get; set; }

public static bool IMDbSyncWatchlist { get; set; }

Expand Down Expand Up @@ -102,7 +105,8 @@ public static void Load()
TraktPassword = xmlReader.GetSettingValueAsString(cTraktPassword, string.Empty);
TVDbAccountIdentifier = xmlReader.GetSettingValueAsString(cTVDbAccountId, string.Empty);
TMDbSessionId = xmlReader.GetSettingValueAsString(cTMDbSessionId, string.Empty);
IMDbFilename = xmlReader.GetSettingValueAsString(cIMDbFilename, string.Empty);
IMDbRatingsFilename = xmlReader.GetSettingValueAsString(cIMDbRatingsFilename, string.Empty);
IMDbWatchlistFilename = xmlReader.GetSettingValueAsString(cIMDbWatchlistFilename, string.Empty);
IMDbUsername = xmlReader.GetSettingValueAsString(cIMDbUsername, string.Empty);
IMDbSyncWatchlist = xmlReader.GetSettingValueAsBool(cIMDBSyncWatchlist, false);
MarkAsWatched = xmlReader.GetSettingValueAsBool(cMarkAsWatched, true);
Expand Down Expand Up @@ -140,7 +144,8 @@ public static void Save()
xmlWriter.WriteSetting(cTraktPassword, TraktPassword);
xmlWriter.WriteSetting(cTVDbAccountId, TVDbAccountIdentifier);
xmlWriter.WriteSetting(cTMDbSessionId, TMDbSessionId);
xmlWriter.WriteSetting(cIMDbFilename, IMDbFilename);
xmlWriter.WriteSetting(cIMDbRatingsFilename, IMDbRatingsFilename);
xmlWriter.WriteSetting(cIMDbWatchlistFilename, IMDbWatchlistFilename);
xmlWriter.WriteSetting(cIMDbUsername, IMDbUsername);
xmlWriter.WriteSetting(cIMDBSyncWatchlist, IMDbSyncWatchlist.ToString());
xmlWriter.WriteSetting(cMarkAsWatched, MarkAsWatched.ToString());
Expand Down
122 changes: 106 additions & 16 deletions Sites/IMDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,32 @@ public class IMDb : IRateSite
#region Variables

bool ImportCancelled = false;
string CSVFile = null;

string CSVRatingsFile = null;
string CSVWatchlistFile = null;

List<Dictionary<string, string>> RateItems = new List<Dictionary<string, string>>();
List<Dictionary<string, string>> WatchlistItems = new List<Dictionary<string, string>>();

bool ImportCSVRatings = false;
bool ImportCSVWatchlist = false;

#endregion

#region Constructor

public IMDb(string filename, bool enabled)
public IMDb(string ratingsFile, string watchlistFile, bool enabled)
{
Enabled = !string.IsNullOrEmpty(filename) && File.Exists(filename) && enabled;
CSVFile = filename;
Enabled = enabled;

if (!enabled) return;

// just do a simple null check, we check file existence on parse
ImportCSVRatings = !string.IsNullOrEmpty(ratingsFile);
ImportCSVWatchlist = !string.IsNullOrEmpty(watchlistFile);

CSVRatingsFile = ratingsFile;
CSVWatchlistFile = watchlistFile;
}

#endregion
Expand All @@ -45,18 +60,31 @@ public IMDb(string filename, bool enabled)
public void ImportRatings()
{
ImportCancelled = false;
List<Dictionary<string, string>> watchedMovies = new List<Dictionary<string,string>>();
List<Dictionary<string, string>> watchedMovies = new List<Dictionary<string,string>>();

#region Parse Ratings CSV
UIUtils.UpdateStatus("Reading IMDb ratings export...");
if (!ParseCSVFile(CSVFile))
if (ImportCSVRatings && !ParseCSVFile(CSVRatingsFile, out RateItems))
{
UIUtils.UpdateStatus("Failed to parse IMDb ratings file!", true);
Thread.Sleep(2000);
return;
}
if (ImportCancelled) return;
#endregion

#region Parse Watchlist CSV
UIUtils.UpdateStatus("Reading IMDb watchlist export...");
if (ImportCSVWatchlist && !ParseCSVFile(CSVWatchlistFile, out WatchlistItems))
{
UIUtils.UpdateStatus("Failed to parse IMDb watchlist file!", true);
Thread.Sleep(2000);
return;
}
if (ImportCancelled) return;
#endregion

#region Movies
#region Import Rated Movies
var movies = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Movie).ToList();
if (movies.Count() > 0)
{
Expand Down Expand Up @@ -88,7 +116,7 @@ public void ImportRatings()
if (ImportCancelled) return;
#endregion

#region TV Shows
#region Import Rated TV Shows
var shows = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Show).ToList();
if (shows.Count() > 0)
{
Expand Down Expand Up @@ -117,7 +145,7 @@ public void ImportRatings()
if (ImportCancelled) return;
#endregion

#region Episodes
#region Import Rated Episodes
var episodes = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Episode).ToList();
TraktEpisodes episodesRated = null;
if (episodes.Count() > 0)
Expand All @@ -131,7 +159,7 @@ public void ImportRatings()
// Filter out shows to rate from existing ratings online
// IMDb CSV does not contain a IMDb for the show, only episode so we can't use that for matching
// For the show name, check the start of the IMDb Title is in the trakt Title (some show names dont match up e.g. Doctor Who -> Doctor Who 2005
// We check episode titles so the starts with will provider an accurate match
// We check episode titles as well so the 'starts with' will provide an accurate match.
episodes.RemoveAll(e => currentUserEpisodeRatings.Any(c => c.ShowDetails.Title.ToLowerInvariant().StartsWith(Helper.GetShowName(e[IMDbFieldMapping.cTitle]).ToLowerInvariant()) && c.EpisodeDetails.Title.ToLowerInvariant() == Helper.GetEpisodeName(e[IMDbFieldMapping.cTitle]).ToLowerInvariant()));
}

Expand All @@ -152,7 +180,7 @@ public void ImportRatings()
if (ImportCancelled) return;
#endregion

#region Mark as Watched
#region Mark Rated Items as Watched
if (AppSettings.MarkAsWatched)
{
#region Movies
Expand Down Expand Up @@ -195,6 +223,66 @@ public void ImportRatings()
}
#endregion

#region Import Watchlist Movies
movies = WatchlistItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Movie).ToList();
if (movies.Count() > 0)
{
//add all movies to watchlist
UIUtils.UpdateStatus(string.Format("Importing {0} IMDb watchlist movies to trakt.tv ...", movies.Count()));
var watchlistMoviesResponse = TraktAPI.TraktAPI.SyncMovieLibrary(Helper.GetSyncMoviesData(movies), TraktSyncModes.watchlist);
if (watchlistMoviesResponse == null || watchlistMoviesResponse.Status != "success")
{
UIUtils.UpdateStatus("Failed to send watchlist for IMDb movies.", true);
Thread.Sleep(2000);
if (ImportCancelled) return;
}
}
if (ImportCancelled) return;
#endregion

#region Import Watchlist TV Shows
shows = WatchlistItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Show).ToList();
if (shows.Count() > 0)
{
//add all shows to watchlist
UIUtils.UpdateStatus(string.Format("Importing {0} IMDb watchlist shows to trakt.tv ...", shows.Count()));
var watchlistShowsResponse = TraktAPI.TraktAPI.SyncShowLibrary(Helper.GetSyncShowsData(shows), TraktSyncModes.watchlist);
if (watchlistShowsResponse == null || watchlistShowsResponse.Status != "success")
{
UIUtils.UpdateStatus("Failed to send watchlist for IMDb tv shows.", true);
Thread.Sleep(2000);
if (ImportCancelled) return;
}
}
if (ImportCancelled) return;
#endregion

#region Import Watchlist Episodes
episodes = WatchlistItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Episode).ToList();
if (episodes.Count() > 0)
{
UIUtils.UpdateStatus(string.Format("Importing {0} IMDb watchlist episodes...", episodes.Count()));
var watchlistEpisodes = Helper.GetEpisodeData(episodes, false);

var syncEpisodeData = Helper.GetSyncEpisodeData(watchlistEpisodes.Episodes);

foreach (var showSyncData in syncEpisodeData)
{
if (ImportCancelled) return;

// send the episodes from each show as watched
UIUtils.UpdateStatus(string.Format("Importing {0} episodes of {1} to watchlist...", showSyncData.EpisodeList.Count(), showSyncData.Title));
var watchlistEpisodesResponse = TraktAPI.TraktAPI.SyncEpisodeLibrary(showSyncData, TraktSyncModes.watchlist);
if (watchlistEpisodesResponse == null || watchlistEpisodesResponse.Status != "success")
{
UIUtils.UpdateStatus(string.Format("Failed to send watchlist for IMDb '{0}' episodes.", showSyncData.Title), true);
Thread.Sleep(2000);
continue;
}
}
}
#endregion

return;
}

Expand All @@ -208,8 +296,10 @@ public void Cancel()

#region Private Methods

private bool ParseCSVFile(string filename)
private bool ParseCSVFile(string filename, out List<Dictionary<string, string>> parsedCSV)
{
parsedCSV = new List<Dictionary<string, string>>();

if (!File.Exists(filename)) return false;

string[] fieldHeadings = new string[]{};
Expand All @@ -236,19 +326,19 @@ private bool ParseCSVFile(string filename)

// get each field value
int index = 0;
var rateItem = new Dictionary<string, string>();
var exportItem = new Dictionary<string, string>();

foreach (string field in fields)
{
rateItem.Add(fieldHeadings[index], field);
exportItem.Add(fieldHeadings[index], field);
index++;
}

// Set provider to web or csv
rateItem.Add(IMDbFieldMapping.cProvider, "csv");
exportItem.Add(IMDbFieldMapping.cProvider, "csv");

// add to list of items
RateItems.Add(rateItem);
parsedCSV.Add(exportItem);
}
parser.Close();
}
Expand Down
Loading

0 comments on commit 162392b

Please sign in to comment.