Skip to content

Commit

Permalink
Issue #64: Added support to export liked comments/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 53ca0df commit 7fb7c6a
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
97 changes: 97 additions & 0 deletions Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,103 @@ public static void CreateCommentedListsCsv()
}
}

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

int i = 0;
UIUtils.UpdateStatus($"Getting liked lists from trakt.tv, Page: {++i}");
var firstPage = TraktAPI.TraktAPI.GetLikedItems(type: "lists");
if (firstPage != null)
{
int pageCount = firstPage.TotalPages;

// store the results from the first page request
var pagedItems = firstPage.Likes;

for (i = 2; i <= firstPage.TotalPages; i++)
{
UIUtils.UpdateStatus($"Getting commented lists from trakt.tv, Page: {i}/{pageCount}");
var nextPage = TraktAPI.TraktAPI.GetLikedItems(type: "lists", page: i);
if (nextPage == null) break;

// add the next page of items to the collection
pagedItems = pagedItems.Union(nextPage.Likes);
}

var listsLiked = new List<object>();
foreach (var item in pagedItems)
{
listsLiked.Add(new
{
ListId = item.List.Ids.Trakt,
LikedAt = item.LikedAt,
ListSlug = item.List.Ids.Slug,
ListName = item.List.Name,
Likes = item.List.Likes
});
}

UIUtils.UpdateStatus("Creating liked lists csv file");
WriteToCsv(Path.Combine(AppSettings.CsvExportPath, "liked_lists.csv"), listsLiked);
}
else
{
UIUtils.UpdateStatus("Failed to get current list of liked lists from trakt.tv", true);
Thread.Sleep(2000);
}
}

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

int i = 0;
UIUtils.UpdateStatus($"Getting liked comments from trakt.tv, Page: {++i}");
var firstPage = TraktAPI.TraktAPI.GetLikedItems(type: "comments");
if (firstPage != null)
{
int pageCount = firstPage.TotalPages;

// store the results from the first page request
var pagedItems = firstPage.Likes;

for (i = 2; i <= firstPage.TotalPages; i++)
{
UIUtils.UpdateStatus($"Getting liked comments from trakt.tv, Page: {i}/{pageCount}");
var nextPage = TraktAPI.TraktAPI.GetLikedItems(type: "comments", page: i);
if (nextPage == null) break;

// add the next page of items to the collection
pagedItems = pagedItems.Union(nextPage.Likes);
}

var commentsLiked = new List<object>();
foreach (var item in pagedItems)
{
commentsLiked.Add(new
{
Id = item.Comment.Id,
LikedAt = item.LikedAt,
ParentId = item.Comment.ParentId,
Comment = item.Comment.Text,
IsSpoiler = item.Comment.IsSpoiler,
IsReview = item.Comment.IsReview,
Replies = item.Comment.Replies,
Likes = item.Comment.Likes
});
}

UIUtils.UpdateStatus("Creating liked comments csv file");
WriteToCsv(Path.Combine(AppSettings.CsvExportPath, "liked_comments.csv"), commentsLiked);
}
else
{
UIUtils.UpdateStatus("Failed to get current list of liked comments from trakt.tv", true);
Thread.Sleep(2000);
}
}

private static void WriteToCsv(string filename, List<object> records)
{
string directory = Path.GetDirectoryName(filename);
Expand Down
2 changes: 2 additions & 0 deletions Settings/ExportItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public class ExportItems
public bool CommentedShows { get; set; }
public bool CommentedMovies { get; set; }
public bool CommentedLists { get; set; }
public bool LikedLists { get; set; }
public bool LikedComments { get; set; }
}
}
53 changes: 53 additions & 0 deletions TraktAPI/DataStructures/TraktLike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Runtime.Serialization;

namespace TraktRater.TraktAPI.DataStructures
{
[DataContract]
public class TraktLike : IEquatable<TraktLike>
{
[DataMember(Name = "liked_at")]
public string LikedAt { get; set; }

[DataMember(Name = "type")]
public string Type { get; set; }

[DataMember(Name = "list")]
public TraktListDetail List { get; set; }

[DataMember(Name = "comment")]
public TraktComment Comment { get; set; }

#region IEquatable
public bool Equals(TraktLike other)
{
if (other == null || (other.Comment == null && other.Type == "comment") || (other.List == null && other.Type == "list"))
return false;

if (this.Type == "list")
{
if (this.List.Ids == null || other.List.Ids == null)
return false;

return (this.Type == other.Type && this.List.Ids.Trakt == other.List.Ids.Trakt);
}
else
{
return (this.Type == other.Type && this.Comment.Id == other.Comment.Id);
}
}

public override int GetHashCode()
{
if (this.Type == "list")
{
return (this.List.Ids.Trakt.ToString() + "_" + this.Type).GetHashCode();
}
else
{
return (this.Comment.Id.ToString() + "_" + this.Type).GetHashCode();
}
}
#endregion
}
}
9 changes: 9 additions & 0 deletions TraktAPI/DataStructures/TraktLikes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace TraktRater.TraktAPI.DataStructures
{
public class TraktLikes : TraktPagination
{
public IEnumerable<TraktLike> Likes { get; set; }
}
}
35 changes: 35 additions & 0 deletions TraktAPI/TraktAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,41 @@ public static TraktComments GetUsersComments(string username = "me", string comm
}
#endregion

#region Likes
/// <summary>
/// Gets the current users liked items (comments and/or lists)
/// </summary>
/// <param name="type">The type of liked item: all (default), lists or comments</param>
/// <param name="extendedInfoParams">Extended Info: min, full, images (comma separated)</param>
/// <param name="page">Page Number</param>
/// <param name="maxItems">Maximum number of items to request per page (this should be consistent per page request)</param>
public static TraktLikes GetLikedItems(string type = "all", string extendedInfoParams = "min", int page = 1, int maxItems = 10)
{
var headers = new WebHeaderCollection();

var response = TraktWeb.GetFromTrakt(string.Format(TraktURIs.UserLikedItems, type, extendedInfoParams, page, maxItems), out headers);
if (response == null)
return null;

try
{
return new TraktLikes
{
CurrentPage = page,
TotalItemsPerPage = maxItems,
TotalPages = int.Parse(headers["X-Pagination-Page-Count"]),
TotalItems = int.Parse(headers["X-Pagination-Item-Count"]),
Likes = response.FromJSONArray<TraktLike>()
};
}
catch
{
// most likely bad header response
return null;
}
}
#endregion

#endregion
}
}
1 change: 1 addition & 0 deletions TraktAPI/TraktURIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class TraktURIs
public const string SyncPausedEpisodes = "https://api.trakt.tv/sync/playback/episodes";
public const string SyncPausedRemove = "https://api.trakt.tv/sync/playback/{0}";

public const string UserLikedItems = "https://api.trakt.tv/users/likes/{0}?extended={1}&page={2}&limit={3}";
public const string UserComments = "https://api.trakt.tv/users/{0}/comments/{1}/{2}?extended={3}&page={4}&limit={5}&include_replies=false";

}
Expand Down
8 changes: 8 additions & 0 deletions TraktRater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,14 @@ private void StartExport(ExportItems items)
{
Export.CreateCommentedListsCsv();
}
if (AppSettings.CsvExportItems.LikedComments)
{
Export.CreateLikedCommentsCsv();
}
if (AppSettings.CsvExportItems.LikedLists)
{
Export.CreateLikedListsCsv();
}

// finished
SetControlState(true);
Expand Down
2 changes: 2 additions & 0 deletions TraktRater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@
<Compile Include="TraktAPI\DataStructures\TraktEpisodeWatchedSync.cs" />
<Compile Include="TraktAPI\DataStructures\TraktEpisodeWatchlist.cs" />
<Compile Include="TraktAPI\DataStructures\TraktId.cs" />
<Compile Include="TraktAPI\DataStructures\TraktLike.cs" />
<Compile Include="TraktAPI\DataStructures\TraktLikes.cs" />
<Compile Include="TraktAPI\DataStructures\TraktList.cs" />
<Compile Include="TraktAPI\DataStructures\TraktListDetail.cs" />
<Compile Include="TraktAPI\DataStructures\TraktListItem.cs" />
Expand Down
28 changes: 28 additions & 0 deletions UI/ExportDialog.Designer.cs

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

12 changes: 12 additions & 0 deletions UI/ExportDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public ExportDialog(string path, ExportItems items)
chkShowComments.Checked = ItemsToExport.CommentedShows;
chkMovieComments.Checked = ItemsToExport.CommentedMovies;
chkListComments.Checked = ItemsToExport.CommentedLists;
chkLikedLists.Checked = ItemsToExport.LikedLists;
chkLikedComments.Checked = ItemsToExport.LikedComments;

txtExportPath.Text = ExportPath;
#endregion
Expand Down Expand Up @@ -169,6 +171,16 @@ private void chkListComments_Click(object sender, EventArgs e)
{
ItemsToExport.CommentedLists = chkListComments.Checked;
}

private void chkLikedComments_Click(object sender, EventArgs e)
{
ItemsToExport.LikedComments = chkLikedComments.Checked;
}

private void chkLikedLists_Click(object sender, EventArgs e)
{
ItemsToExport.LikedLists = chkLikedLists.Checked;
}
#endregion

}
Expand Down

0 comments on commit 7fb7c6a

Please sign in to comment.