Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit d7835ae

Browse files
committed
error handling in string enum converter; added post_tags PostPageIncludedAttribute
1 parent 72a3f54 commit d7835ae

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

PatreonDownloader/CustomStringEnumConverter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ private static Dictionary<string, object> GetCache(Type objectType) {
2424
public override bool CanConvert(Type objectType) => objectType.IsEnum;
2525
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
2626
Dictionary<string, object> dictionaries = GetCache(objectType);
27-
return dictionaries[(string) reader.Value];
27+
try {
28+
return dictionaries[(string) reader.Value];
29+
} catch (KeyNotFoundException knfe) {
30+
Console.WriteLine("Warning: found unknown data inside the scraped information.");
31+
Console.WriteLine(knfe.Message);
32+
return existingValue;
33+
}
2834
}
2935

30-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
36+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
3137
Dictionary<string, object> dictionaries = GetCache(value.GetType());
3238
writer.WriteValue(dictionaries.First(kvp => {
3339
return kvp.Value.Equals(value);

PatreonDownloader/Model/PostPageIncluded.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class PostPageIncluded {
1818
IncludedType.PollChoice => RawAttributes.ToObject<PostPageIncludedPollChoice>(),
1919
IncludedType.Goal => RawAttributes.ToObject<PostPageIncludedGoal>(),
2020
IncludedType.Attachment => RawAttributes.ToObject<PostPageIncludedAttachment>(),
21+
IncludedType.PostTag => RawAttributes.ToObject<PostPageIncludedPostTags>(),
2122
_ => throw new InvalidOperationException("This should not happen. PostPageIncluded.Type was " + Type.ToString())
2223
};
2324

@@ -58,6 +59,9 @@ public enum IncludedType {
5859

5960
[JsonProperty("attachment")]
6061
Attachment,
62+
63+
[JsonProperty("post_tag")]
64+
PostTag
6165
}
6266
}
6367
}

PatreonDownloader/Model/PostPageIncludedAttributes.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,13 @@ public class PostPageIncludedAttachment : PostPageIncludedAttributes {
205205
[JsonProperty("url")]
206206
public string Url { get; set; }
207207
}
208+
209+
[Serializable]
210+
public class PostPageIncludedPostTags : PostPageIncludedAttributes {
211+
[JsonProperty("id")]
212+
public string Id { get; set; }
213+
214+
[JsonProperty("type")]
215+
public string Type { get; set; }
216+
}
208217
}

PatreonDownloader/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Program {
1717

1818
private static void Main(string[] args) {
1919
try {
20+
Directory.CreateDirectory(DataFolder);
21+
2022
string sessionToken = null;
2123

2224
CookieExtractor[] cookieExtractors = new CookieExtractor[] {
@@ -221,7 +223,7 @@ private static void DownloadAllPosts(HttpClient client, List<PostPage> pages, st
221223

222224
nextUrl = page.Links?.Next;
223225

224-
Thread.Sleep(TimeSpan.FromSeconds(10));
226+
Thread.Sleep(TimeSpan.FromSeconds(5));
225227
} while (nextUrl != null);
226228

227229
Console.WriteLine("Done, all post data has been saved locally. Run the program again to download the media.");

0 commit comments

Comments
 (0)