Skip to content

Commit

Permalink
Issue #64: Added support to export custom lists from trakt.tv
Browse files Browse the repository at this point in the history
  • Loading branch information
damienhaynes committed Apr 19, 2019
1 parent 98805d0 commit 3305419
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 13 deletions.
102 changes: 102 additions & 0 deletions Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,108 @@ public static void CreatePausedMoviesCsv()
}
}

public static void CreateCustomListsCsv()
{
if (Cancel) return;

// First get the users custom lists

UIUtils.UpdateStatus("Getting custom lists from trakt.tv");
var customLists = TraktAPI.TraktAPI.GetCustomLists();
if (customLists != null)
{
var lists = new List<object>();
foreach (var item in customLists)
{
lists.Add(new
{
TraktId = item.Ids.Trakt,
Slug = item.Ids.Slug,
Name = item.Name,
Description = item.Description,
Privacy = item.Privacy,
DisplayNumbers = item.DisplayNumbers,
AllowComments = item.AllowComments,
SortBy = item.SortBy,
SortHow = item.SortHow,
ItemCount = item.ItemCount,
CommentCount = item.Comments,
Likes = item.Likes,
CreatedAt = item.CreatedAt,
UpdatedAt = item.UpdatedAt
});
}

UIUtils.UpdateStatus("Creating custom lists csv file");
WriteToCsv(Path.Combine(AppSettings.CsvExportPath, "custom_lists.csv"), lists);
}
else
{
UIUtils.UpdateStatus("Failed to get current custom lists from trakt.tv", true);
Thread.Sleep(2000);
return;
}

// Next get the items for each custom list

foreach(var list in customLists)
{
UIUtils.UpdateStatus($"Getting custom list items for '{list.Name}' from trakt.tv");
var customListItems = TraktAPI.TraktAPI.GetCustomListItems(list.Ids.Trakt.ToString());
if (customListItems != null)
{
var listItems = new List<object>();
foreach (var item in customListItems)
{
// important to check for null as an item may be of a different type
// NB: the season object does not return for episodes, so get season number from episode object
listItems.Add(new
{
Rank = item.Rank,
Id = item.Id,
Type = item.Type,
ListedAt = item.ListedAt,
MovieTitle = item.Movie?.Title,
MovieYear = item.Movie?.Year,
MovieTraktId = item.Movie?.Ids.Trakt,
MovieImdbId = item.Movie?.Ids.ImdbId,
MovieTmdbId = item.Movie?.Ids.TmdbId,
MovieSlug = item.Movie?.Ids.Slug,
ShowTitle = item.Show?.Title,
ShowYear = item.Show?.Year,
ShowTraktId = item.Show?.Ids.Trakt,
ShowTvdbId = item.Show?.Ids.TvdbId,
ShowTmdbId = item.Show?.Ids.TmdbId,
ShowSlug = item.Show?.Ids.Slug,
SeasonTvdbId = item.Season?.Ids.TvdbId,
SeasonTmdbId = item.Season?.Ids.TmdbId,
SeasonNumber = item.Season?.Number.ToString() ?? item.Episode?.Season.ToString(),
EpisodeNumber = item.Episode?.Number,
EpisodeTraktId = item.Episode?.Ids.Trakt,
EpisodeTvdbId = item.Episode?.Ids.TvdbId,
EpisodeTmdbId = item.Episode?.Ids.TmdbId,
EpisodeImdbId = item.Episode?.Ids.ImdbId,
PersonName = item.Person?.Name,
PersonTraktId = item.Person?.Ids.Trakt,
PersonImdbId = item.Person?.Ids.ImdbId,
PersonTmdbId = item.Person?.Ids.TmdbId,
PersonSlug = item.Person?.Ids.Slug
});
}

UIUtils.UpdateStatus($"Creating custom list '{list.Name}' csv file");
// using the slug for the filename should be file system friendly
WriteToCsv(Path.Combine(AppSettings.CsvExportPath, $"custom_list_{list.Ids.Slug}.csv"), listItems);
}
else
{
UIUtils.UpdateStatus($"Failed to get current custom list items for '{list.Name}' from trakt.tv", true);
Thread.Sleep(2000);
}

}
}

private static void WriteToCsv(string filename, List<object> records)
{
string directory = Path.GetDirectoryName(filename);
Expand Down
1 change: 1 addition & 0 deletions Settings/ExportItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public class ExportItems
public bool WatchlistMovies { get; set; }
public bool PausedEpisodes { get; set; }
public bool PausedMovies { get; set; }
public bool CustomLists { get; set; }
}
}
6 changes: 6 additions & 0 deletions TraktAPI/DataStructures/TraktList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ public class TraktList

[DataMember(Name = "allow_comments")]
public bool AllowComments { get; set; }

[DataMember(Name = "sort_by")]
public string SortBy { get; set; }

[DataMember(Name = "sort_how")]
public string SortHow { get; set; }
}
}
3 changes: 3 additions & 0 deletions TraktAPI/DataStructures/TraktListDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
[DataContract]
public class TraktListDetail : TraktList
{
[DataMember(Name = "created_at")]
public string CreatedAt { get; set; }

[DataMember(Name = "updated_at")]
public string UpdatedAt { get; set; }

Expand Down
10 changes: 8 additions & 2 deletions TraktAPI/DataStructures/TraktListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
[DataContract]
public class TraktListItem
{
[DataMember(Name = "rank")]
public int Rank { get; set; }

[DataMember(Name = "id")]
public int Id { get; set; }

[DataMember(Name = "listed_at")]
public string ListedAt { get; set; }

Expand All @@ -18,10 +24,10 @@ public class TraktListItem
public TraktShow Show { get; set; }

[DataMember(Name = "season")]
public TraktSeason Season { get; set; }
public TraktSeasonEx Season { get; set; }

[DataMember(Name = "episode")]
public TraktEpisode Episode { get; set; }
public TraktEpisodeSummary Episode { get; set; }

[DataMember(Name = "person")]
public TraktPerson Person { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions TraktAPI/DataStructures/TraktSeasonEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.Serialization;

namespace TraktRater.TraktAPI.DataStructures
{
[DataContract]
public class TraktSeasonEx : TraktSeason
{
[DataMember(Name = "ids")]
public TraktSeasonId Ids { get; set; }
}
}
4 changes: 4 additions & 0 deletions TraktRater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ private void StartExport(ExportItems items)
{
Export.CreatePausedMoviesCsv();
}
if (AppSettings.CsvExportItems.CustomLists)
{
Export.CreateCustomListsCsv();
}

// finished
SetControlState(true);
Expand Down
1 change: 1 addition & 0 deletions TraktRater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<Compile Include="TraktAPI\DataStructures\TraktPerson.cs" />
<Compile Include="TraktAPI\DataStructures\TraktPersonId.cs" />
<Compile Include="TraktAPI\DataStructures\TraktSeason.cs" />
<Compile Include="TraktAPI\DataStructures\TraktSeasonEx.cs" />
<Compile Include="TraktAPI\DataStructures\TraktSeasonId.cs" />
<Compile Include="TraktAPI\DataStructures\TraktSeasonSync.cs" />
<Compile Include="TraktAPI\DataStructures\TraktSeasonWatchlist.cs" />
Expand Down
36 changes: 25 additions & 11 deletions UI/ExportDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions UI/ExportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ExportDialog(string path, ExportItems items)
chkEpisodeCollection.Checked = ItemsToExport.CollectedEpisodes;
chkMovieWatchedHistory.Checked = ItemsToExport.WatchedHistoryMovies;
chkEpisodeWatchedHistory.Checked = ItemsToExport.WatchedHistoryEpisodes;
chkCustomLists.Checked = ItemsToExport.CustomLists;

txtExportPath.Text = ExportPath;
#endregion
Expand Down Expand Up @@ -133,6 +134,12 @@ private void chkMoviePausedStates_Click(object sender, EventArgs e)
{
ItemsToExport.PausedMovies = chkMoviePausedStates.Checked;
}

private void chkCustomLists_Click(object sender, EventArgs e)
{
ItemsToExport.CustomLists = chkCustomLists.Checked;
}
#endregion

}
}

0 comments on commit 3305419

Please sign in to comment.