SHARE
TWEET

Duolingo HOF extractor script

a guest Aug 9th, 2014 365 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // HOF extractor script
  2. //
  3. // To be run at https://www.duolingo.com/comment/2933824
  4. //
  5. //
  6. // The `user` object:
  7. // user = {
  8. //    name : '<name>',
  9. //    streak: <streak duration>,
  10. //    levels: {
  11. //      <language>:<level>,
  12. //      ...
  13. //    }
  14. //  }
  15.  
  16. // Sort list of `user` objects first by streak
  17. // duration, then by username.
  18. function sortByStreak(users){
  19.   return users.sort(function(a, b){
  20.     var c = b.streak - a.streak;
  21.     if(c===0)
  22.       c = b.name < a.name ? -1 : 1;
  23.     return c;
  24.   });
  25. }
  26.  
  27. // `node` must be a <header> element with a
  28. // child <a class=username>.
  29. function extractUserName(node){
  30.   return node.querySelector('a.username').innerHTML;
  31. }
  32.  
  33. // `node` must be a <header> element.
  34. function extractLevels(node){
  35.   return undefined;
  36. }
  37.  
  38. // `node` must be a <header> element.
  39. function extractStreak(node){
  40.   var streakIcon = node.querySelector('.icon-streak-micro');
  41.   if(streakIcon)
  42.     return parseInt(
  43.              streakIcon
  44.                .parentNode
  45.                .textContent
  46.              , 10);
  47.     else
  48.       return 0;
  49. }
  50.  
  51. // Print segment of HOF. `users` is the list of user objects,
  52. // `q` is the streak length required to qualify for this HOF segment,
  53. // `start` is where to start looking in the `users` list.
  54. // When it encounters a user that does not qualify, it returns
  55. // the current list position.
  56. function printHOFsegment(users,q,start){
  57.   for (var i = start, m = users.length; i < m; i++){
  58.     var user = users[i];
  59.     if(user.streak < q)
  60.       return i;
  61.     console.log(user.streak,user.name);
  62.   }
  63. }
  64.  
  65. function printHallOfFame(users){
  66.   var gold_q = 500;
  67.   var silver_q = 400;
  68.   var bronze_q = 365;
  69.   var soon_to_be_q = 300;
  70.  
  71.   sortByStreak(users);
  72.   console.log('Gold');
  73.   var i = printHOFsegment(users,gold_q,0);
  74.   console.log('Silver');
  75.   i = printHOFsegment(users,silver_q, i);
  76.   console.log('Bronze');
  77.   i = printHOFsegment(users,bronze_q, i);
  78.   console.log("Soon to be's");
  79.   i = printHOFsegment(users,soon_to_be_q, i);
  80. }
  81.  
  82. //
  83. // Get the headers from all posts in this thread.
  84. var nodeList = document.querySelectorAll('header > a.username');
  85. var headers = [];
  86. for(var i=0,m=nodeList.length; i<m;i++){
  87.   headers.push(nodeList.item(i).parentNode);
  88. }
  89.  
  90. // Just an object containing the usernames of users
  91. // we already processed.
  92. processed_users={};
  93.  
  94. // List of user objects.
  95. users=[];
  96.  
  97. // Iterate through the headers, collecting data for
  98. // each user as we go along.
  99. for(var i=0,m=headers.length;i<m;i++){
  100.   var n = headers[i];
  101.   var name = extractUserName(n);
  102.   if(name in processed_users) continue;
  103.   var userData = {};
  104.   var streak = extractStreak(n);
  105.   userData.name = name;
  106.   userData.streak = streak;
  107.   users.push(userData);
  108.   processed_users[name] = undefined;
  109. }
  110.  
  111. printHallOfFame(users);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top