Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ConcurrentBag<Entry> entrybag = new ConcurrentBag<Entry>(entries);
- Console.WriteLine("Getting Wikibase Item Ids...");
- Parallel.ForEach<Entry>(entrybag, (entry) =>
- {
- entry.WikibaseItemId = GetWikibaseItemId(entry).Result;
- });
- async static Task<String> GetWikibaseItemId(Entry entry)
- {
- using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
- {
- client.BaseAddress = new Uri("https://en.wikipedia.org/w/api.php");
- entry.Title.Replace("+", "Plus");
- entry.Title.Replace("&", "and");
- String queryString = "?action=query&prop=pageprops&ppprop=wikibase_item&format=json&redirects=1&titles=" + entry.Title;
- queryString = queryString.Replace("+", "Plus");
- HttpResponseMessage response = await client.GetAsync(queryString);
- response.EnsureSuccessStatusCode();
- String result = response.Content.ReadAsStringAsync().Result;
- dynamic deserialized = JsonConvert.DeserializeObject(result);
- String data = deserialized.ToString();
- try
- {
- if (data.Contains("wikibase_item"))
- {
- return deserialized["query"]["pages"]["" + entry.PageId + ""]["pageprops"]["wikibase_item"].ToString();
- }
- else
- {
- return "NONE";
- }
- }
- catch (RuntimeBinderException)
- {
- return "NULL";
- }
- catch (Exception)
- {
- return "ERROR";
- }
- }
- }
- public class Entry
- {
- public EntryCategory Category { get; set; }
- public int PageId { get; set; }
- public String Title { get; set; }
- public String WikibaseItemId { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement