Website for visualizing a persons github network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.3 KiB

5 years ago
  1. const routes = require('express').Router();
  2. const got = require("got");
  3. const cache = require('memory-cache');
  4. const dotenv = require("dotenv").config();
  5. const GITHUB_API = "https://api.github.com";
  6. const authenticate = `client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}`;
  7. function queryGithubAPIRaw(requestURL)
  8. {
  9. return new Promise((resolve, reject)=>
  10. {
  11. var queryURL;
  12. if(requestURL.includes("?page="))
  13. {
  14. queryURL = GITHUB_API + requestURL + "&" + authenticate;
  15. }
  16. else
  17. {
  18. queryURL = GITHUB_API + requestURL + "?" + authenticate;
  19. }
  20. console.log(queryURL);
  21. got(queryURL, { json: true }).then(response =>
  22. {
  23. resolve(response.body);
  24. cache.put(requestURL, response.body);
  25. }).catch(error =>
  26. {
  27. resolve(error);
  28. cache.put(requestURL, error);
  29. });
  30. });
  31. }
  32. function queryGitHubAPI(requestURL)
  33. {
  34. const apiData = cache.get(requestURL);
  35. return new Promise(function(resolve, reject)
  36. {
  37. if(apiData == null)
  38. {
  39. queryGithubAPIRaw(requestURL).then((dd)=>
  40. {
  41. resolve(dd);
  42. }).catch((err)=>
  43. {
  44. resolve(err);
  45. })
  46. }
  47. else
  48. {
  49. console.log("Fetched From Cache");
  50. resolve(apiData);
  51. }
  52. })
  53. }
  54. const API_FOLLOWING = "/following";
  55. const API_FOLLOWERS = "/followers";
  56. const API_USER_PATH = "/users/";
  57. const API_PAGINATION_SIZE = 100;
  58. const API_MAX_PAGES = 3;
  59. const API_PAGINATION = "&per_page=" + API_PAGINATION_SIZE;
  60. function fetchAllUsers(username, apiPath, page, lst)
  61. {
  62. return new Promise((resolve, reject)=>
  63. {
  64. queryGitHubAPI(API_USER_PATH + username + apiPath + "?page=" + page + API_PAGINATION).then((data)=>
  65. {
  66. if(data.hasOwnProperty("length"))
  67. {
  68. lst = lst.concat(data)
  69. if(page < API_MAX_PAGES && data.length === API_PAGINATION_SIZE)
  70. {
  71. fetchAllUsers(username, apiPath, page + 1, lst).then((l)=>
  72. {
  73. resolve(l);
  74. });
  75. }
  76. else
  77. {
  78. resolve(lst);
  79. }
  80. }
  81. else
  82. {
  83. reject("Malformed data");
  84. }
  85. }).catch((err)=>
  86. {
  87. reject("error with api request");
  88. });
  89. },
  90. (error)=>
  91. {
  92. if(error.hasOwnProperty("length"))
  93. {
  94. lst.concat(data);
  95. resolve(lst);
  96. }
  97. });
  98. }
  99. function queryFriends(user)
  100. {
  101. return new Promise((resolve, reject)=>
  102. {
  103. fetchAllUsers(user, API_FOLLOWERS, 1, []).then((followers)=>
  104. {
  105. fetchAllUsers(user, API_FOLLOWING, 1, []).then((following)=>
  106. {
  107. resolve(following.concat(followers));
  108. }).catch((err)=>
  109. {
  110. console.log(err);
  111. })
  112. }).catch((error)=>
  113. {
  114. console.log(error);
  115. })
  116. });
  117. }
  118. routes.get("/friends/:name", (request, result)=>
  119. {
  120. queryFriends(request.params.name).then(friends=>
  121. {
  122. result.json(friends)
  123. .end();
  124. }).catch(error=>
  125. {
  126. result.status(500)
  127. .json({error: 'API error fetching friends'})
  128. .end();
  129. });
  130. })
  131. routes.get('/*', (request, result) =>
  132. {
  133. var gitHubAPIURL = request.url;
  134. result.setHeader('Content-Type', 'application/json');
  135. queryGitHubAPI(gitHubAPIURL).then((data)=>
  136. {
  137. if(data.hasOwnProperty("id") || data[0].hasOwnProperty("id"))
  138. {
  139. result.write(JSON.stringify(data));
  140. }
  141. else
  142. {
  143. result.write("[]");
  144. }
  145. result.end();
  146. }).catch((error)=>
  147. {
  148. try
  149. {
  150. if(error.hasOwnProperty("id") || error[0].hasOwnProperty("id"))
  151. {
  152. result.write(JSON.stringify(error));
  153. }
  154. else
  155. {
  156. result.write("[]");
  157. }
  158. }
  159. catch(error)
  160. {
  161. result.write("[]");
  162. };
  163. result.end();
  164. });
  165. if(cache.size() > 50000)
  166. {
  167. cache.clear();
  168. }
  169. });
  170. module.exports = routes;