/
TorrentTracker.cs
138 lines (123 loc) · 4.87 KB
/
TorrentTracker.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using QBittorrent.Client.Converters;
namespace QBittorrent.Client
{
/// <summary>
/// Represents the torrent tracker information.
/// </summary>
public class TorrentTracker
{
private static readonly IDictionary<string, TorrentTrackerStatus> StatusMap =
new Dictionary<string, TorrentTrackerStatus>(StringComparer.OrdinalIgnoreCase)
{
["Disabled"] = TorrentTrackerStatus.Disabled,
["Working"] = TorrentTrackerStatus.Working,
["Updating..."] = TorrentTrackerStatus.Updating,
["Not working"] = TorrentTrackerStatus.NotWorking,
["Not contacted yet"] = TorrentTrackerStatus.NotContacted
};
/// <summary>
/// Tracker URL
/// </summary>
[JsonProperty("url")]
public Uri Url { get; set; }
/// <summary>
/// Tracker status as string
/// </summary>
/// <seealso cref="TrackerStatus"/>
/// <remarks>
/// <para>Until API v2.2.0 this property might return localized status string.</para>
/// <para>Starting from API v2.2.0 this property always returns English status string.</para>
/// </remarks>
[JsonIgnore]
public string Status { get; set; }
/// <summary>
/// Tracker status
/// </summary>
/// <seealso cref="Status"/>
/// <remarks>
/// Until API v2.2.0 this property might always return null on non-English localizations.
/// </remarks>
[JsonIgnore]
public TorrentTrackerStatus? TrackerStatus { get; set; }
/// <summary>
/// Number of peers for current torrent reported by the tracker
/// </summary>
[JsonProperty("num_peers")]
[JsonConverter(typeof(NegativeToNullConverter))]
public int? Peers { get; set; }
/// <summary>
/// Number of seeds for current torrent reported by the tracker
/// </summary>
[JsonProperty("num_seeds")]
[JsonConverter(typeof(NegativeToNullConverter))]
[ApiLevel(ApiLevel.V2, MinVersion = "2.2.0")]
public int? Seeds { get; set; }
/// <summary>
/// Number of leeches for current torrent reported by the tracker
/// </summary>
[JsonProperty("num_leeches")]
[JsonConverter(typeof(NegativeToNullConverter))]
[ApiLevel(ApiLevel.V2, MinVersion = "2.2.0")]
public int? Leeches { get; set; }
/// <summary>
/// Number of completed downloads for current torrent reported by the tracker
/// </summary>
[JsonProperty("num_downloaded")]
[JsonConverter(typeof(NegativeToNullConverter))]
[ApiLevel(ApiLevel.V2, MinVersion = "2.2.0")]
public int? CompletedDownloads { get; set; }
/// <summary>
/// Tracker priority tier. Lower tier trackers are tried before higher tiers
/// </summary>
[JsonProperty("tier")]
[ApiLevel(ApiLevel.V2, MinVersion = "2.2.0")]
public int? Tier { get; set; }
/// <summary>
/// Tracker message (there is no way of knowing what this message is - it's up to tracker admins)
/// </summary>
[JsonProperty("msg")]
public string Message { get; set; }
/// <summary>
/// Additional properties not handled by this library.
/// </summary>
[JsonExtensionData]
public IDictionary<string, JToken> AdditionalData { get; set; }
[OnDeserialized]
[UsedImplicitly]
private void OnDeserialized(StreamingContext context)
{
const string statusKey = "status";
if (AdditionalData != null &&
AdditionalData.TryGetValue(statusKey, out JToken status))
{
if (status.Type == JTokenType.String)
{
Status = status.ToObject<string>();
TrackerStatus = StringToStatus(Status);
AdditionalData.Remove(statusKey);
}
else if (status.Type == JTokenType.Integer)
{
TrackerStatus = status.ToObject<TorrentTrackerStatus>();
Status = StatusToString(TrackerStatus);
AdditionalData.Remove(statusKey);
}
}
}
private static string StatusToString(TorrentTrackerStatus? status)
{
return StatusMap.FirstOrDefault(x => x.Value == status).Key;
}
private static TorrentTrackerStatus? StringToStatus(string statusString)
{
return StatusMap.TryGetValue(statusString, out var status) ? status : (TorrentTrackerStatus?)null;
}
}
}