Skip to content

Instantly share code, notes, and snippets.

@TheDarkCode
Created January 1, 2019 17:54
Show Gist options
  • Save TheDarkCode/7bd8b34e52149054b981d1bbfb0c1dbe to your computer and use it in GitHub Desktop.
Save TheDarkCode/7bd8b34e52149054b981d1bbfb0c1dbe to your computer and use it in GitHub Desktop.
Qanon Post Parser
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Example.CLI
{
public class Program
{
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
public static async Task MainAsync(string[] args)
{
Console.Title = "Example CLI";
//await Task.Run(() => Console.WriteLine("Example Command Line Interface"));
await ProcessQPosts();
}
public static async Task ProcessQPosts()
{
using (var httpClient = new HttpClient())
{
string postsStr = await httpClient.GetStringAsync("https://qanon.pub/data/json/posts.json");
List<QAnonPubPost> posts = JsonConvert.DeserializeObject<List<QAnonPubPost>>(postsStr);
//posts = posts.Where(x => x.References != null && x.References.Any()).OrderByDescending(x => x.References.Count()).Take(1).ToList();
//posts = posts.Where(x => x.Media != null && x.Media.Any()).OrderByDescending(x => x.Media.Count()).ToList();
foreach (var post in posts)
{
// JsonConvert.SerializeObject(post.References)
Console.WriteLine(post.Text);
}
}
}
}
public class QAnonPubPost
{
[JsonProperty("id")]
[JsonConverter(typeof(ParseStringConverter))]
public long Id { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("media")]
public List<QAnonPubMedia> Media { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("trip")]
public string Trip { get; set; }
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("threadId", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ParseStringConverter))]
public long? ThreadId { get; set; }
[JsonProperty("threadJson", NullValueHandling = NullValueHandling.Ignore)]
public string ThreadJson { get; set; }
[JsonProperty("timestamp")]
public long TimeStamp { get; set; }
[JsonProperty("edited", NullValueHandling = NullValueHandling.Ignore)]
public long? Edited { get; set; }
[JsonProperty("deleted", NullValueHandling = NullValueHandling.Ignore)]
public long? Deleted { get; set; }
[JsonProperty("timestampDeletion", NullValueHandling = NullValueHandling.Ignore)]
public long? TimestampDeletion { get; set; }
[JsonProperty("references", NullValueHandling = NullValueHandling.Ignore)]
public List<QAnonPubPostReference> References { get; set; }
}
public class QAnonPubPostReference
{
[JsonProperty("id")]
[JsonConverter(typeof(ParseStringConverter))]
public long Id { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("media")]
public List<QAnonPubMedia> Media { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("trip")]
public string Trip { get; set; }
[JsonProperty("subject")]
public string Subject { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonProperty("threadId", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(ParseStringConverter))]
public long? ThreadId { get; set; }
[JsonProperty("threadJson", NullValueHandling = NullValueHandling.Ignore)]
public string ThreadJson { get; set; }
[JsonProperty("timestamp")]
public long TimeStamp { get; set; }
[JsonProperty("edited", NullValueHandling = NullValueHandling.Ignore)]
public long? Edited { get; set; }
[JsonProperty("references", NullValueHandling = NullValueHandling.Ignore)]
public List<QAnonPubPostReference> References { get; set; }
}
public class QAnonPubMedia
{
public string FileName { get; set; }
public string Uri { get; set; }
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class ParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment