Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. ConcurrentBag<Entry> entrybag = new ConcurrentBag<Entry>(entries);
  2. Console.WriteLine("Getting Wikibase Item Ids...");
  3. Parallel.ForEach<Entry>(entrybag, (entry) =>
  4. {
  5. entry.WikibaseItemId = GetWikibaseItemId(entry).Result;
  6. });
  7.  
  8. async static Task<String> GetWikibaseItemId(Entry entry)
  9. {
  10. using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
  11. {
  12. client.BaseAddress = new Uri("https://en.wikipedia.org/w/api.php");
  13. entry.Title.Replace("+", "Plus");
  14. entry.Title.Replace("&", "and");
  15. String queryString = "?action=query&prop=pageprops&ppprop=wikibase_item&format=json&redirects=1&titles=" + entry.Title;
  16. queryString = queryString.Replace("+", "Plus");
  17. HttpResponseMessage response = await client.GetAsync(queryString);
  18.  
  19. response.EnsureSuccessStatusCode();
  20.  
  21. String result = response.Content.ReadAsStringAsync().Result;
  22. dynamic deserialized = JsonConvert.DeserializeObject(result);
  23. String data = deserialized.ToString();
  24. try
  25. {
  26. if (data.Contains("wikibase_item"))
  27. {
  28. return deserialized["query"]["pages"]["" + entry.PageId + ""]["pageprops"]["wikibase_item"].ToString();
  29. }
  30. else
  31. {
  32. return "NONE";
  33. }
  34. }
  35. catch (RuntimeBinderException)
  36. {
  37. return "NULL";
  38. }
  39. catch (Exception)
  40. {
  41. return "ERROR";
  42. }
  43. }
  44. }
  45.  
  46. public class Entry
  47. {
  48. public EntryCategory Category { get; set; }
  49. public int PageId { get; set; }
  50. public String Title { get; set; }
  51. public String WikibaseItemId { get; set; }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement