Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MyWatchlistGenerator #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions WikiClientLibrary/Generators/GeoSearchGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Newtonsoft.Json.Linq;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Pages.Queries.Properties;
using WikiClientLibrary.Sites;

Expand Down
6 changes: 1 addition & 5 deletions WikiClientLibrary/Generators/GeoSearchResultItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using WikiClientLibrary.Pages;
using WikiClientLibrary.Pages;

namespace WikiClientLibrary.Generators
{
Expand Down
72 changes: 72 additions & 0 deletions WikiClientLibrary/Generators/MyWatchlistGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using WikiClientLibrary.Generators.Primitive;
using WikiClientLibrary.Infrastructures;
using WikiClientLibrary.Sites;

namespace WikiClientLibrary.Generators
{
/// <summary>
/// Get all pages on the current user's watchlist.
/// </summary>
public class MyWatchlistGenerator : WikiPageGenerator<MyWatchlistResultItem>
{
public MyWatchlistGenerator(WikiSite site) : base(site)
{
}

/// <summary>
/// Only list pages in the given namespaces.
/// </summary>
public IEnumerable<int>? NamespaceIds { get; set; }

/// <summary>
/// Only show pages that have not been changed.
/// </summary>
public PropertyFilterOption NotChangedPagesFilter { get; set; }

/// <summary>
/// Gets/sets a value that indicates whether the links should be listed in
/// the descending order. (MediaWiki 1.19+)
/// </summary>
public bool OrderDescending { get; set; }

/// <summary>
/// Title (with namespace prefix) to begin enumerating from.
/// </summary>
public string? FromTitle { get; set; }

/// <summary>
/// Title (with namespace prefix) to stop enumerating at.
/// </summary>
public string? ToTitle { get; set; }

public override IEnumerable<KeyValuePair<string, object?>> EnumListParameters()
{
return new Dictionary<string, object?>
{
{"wrnamespace", NamespaceIds == null ? null : MediaWikiHelper.JoinValues(NamespaceIds)},
{"wrlimit", PaginationSize},
{"wrprop", "changed"},
{"wrshow", NotChangedPagesFilter.ToString("!changed", "changed", null)},
{"wrdir", OrderDescending ? "descending" : "ascending"},
{"wrfromtitle", FromTitle},
{"wrtotitle", ToTitle},
};
}

protected override MyWatchlistResultItem ItemFromJson(JToken json)
{
DateTime? changedTime = null;
if (json["changed"] != null)
{
changedTime = DateTime.Parse((string)json["changed"]);
}

return new MyWatchlistResultItem(MediaWikiHelper.PageStubFromJson((JObject)json), json["changed"] != null, changedTime);
}

public override string ListName => "watchlistraw";
}
}
19 changes: 19 additions & 0 deletions WikiClientLibrary/Generators/MyWatchlistResultItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using WikiClientLibrary.Pages;

namespace WikiClientLibrary.Generators
{
public sealed class MyWatchlistResultItem
{
public WikiPageStub Page { get; }
public bool IsChanged { get; }
public DateTime? ChangedTime { get; }

public MyWatchlistResultItem(WikiPageStub page, bool isChanged, DateTime? changedTime = null)
{
Page = page;
IsChanged = isChanged;
ChangedTime = changedTime;
}
}
}
15 changes: 15 additions & 0 deletions WikiClientLibrary/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ static bool ValueEquals(object? existing, object? incoming)

public static JToken? FindQueryResponseItemsRoot(JToken jresult, string actionName)
{
if (actionName == "watchlistraw")
{
// "watchlistraw" list isn't nested inside an object with "query" key, it has the following structure
/*
{
"batchcomplete": true,
"continue": {
"wrcontinue": "0|3Р41",
"continue": "-||tokens"
},
"watchlistraw": [{"ns": 0,"title": "page title","changed": "2021-03-01T14:59:52Z"}]
}
*/
return (JArray?)jresult[actionName];
}
// If there's no result, "query" node will not exist.
var queryNode = (JObject?)jresult["query"];
if (queryNode != null && queryNode.HasValues)
Expand Down
Loading