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.

194 lines
4.4 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. function fetchAllUsers(username, apiPath, page, lst)
  58. {
  59. return new Promise((resolve, reject)=>
  60. {
  61. queryGitHubAPI(API_USER_PATH + username + apiPath + "?page=" + page).then((data)=>
  62. {
  63. if(data.hasOwnProperty("length"))
  64. {
  65. lst = lst.concat(data)
  66. if(page < 5 && data.length === 30)
  67. {
  68. fetchAllUsers(username, apiPath, page + 1, lst).then((l)=>
  69. {
  70. resolve(l);
  71. });
  72. }
  73. else
  74. {
  75. resolve(lst);
  76. }
  77. }
  78. else
  79. {
  80. reject("Malformed data");
  81. }
  82. }).catch((err)=>
  83. {
  84. reject("error with api request");
  85. });
  86. },
  87. (error)=>
  88. {
  89. if(error.hasOwnProperty("length"))
  90. {
  91. lst.concat(data);
  92. resolve(lst);
  93. }
  94. });
  95. }
  96. function queryFriends(user)
  97. {
  98. return new Promise((resolve, reject)=>
  99. {
  100. fetchAllUsers(user, API_FOLLOWERS, 1, []).then((followers)=>
  101. {
  102. fetchAllUsers(user, API_FOLLOWING, 1, []).then((following)=>
  103. {
  104. resolve(following.concat(followers));
  105. }).catch((err)=>
  106. {
  107. console.log(err);
  108. })
  109. }).catch((error)=>
  110. {
  111. console.log(error);
  112. })
  113. });
  114. }
  115. routes.get("/friends", (request, result)=>
  116. {
  117. if(request.query.hasOwnProperty("name"))
  118. {
  119. result.setHeader('Content-Type', 'application/json');
  120. queryFriends(request.query.name).then(friends=>
  121. {
  122. result.write(JSON.stringify(friends));
  123. result.end();
  124. }).catch(error=>
  125. {
  126. result.status(500);
  127. result.write("API error fetching friends.")
  128. result.end();
  129. });
  130. }
  131. else
  132. {
  133. result.status(400);
  134. result.write("Must provide the name get parameter");
  135. result.end();
  136. }
  137. })
  138. routes.get('/*', (request, result) =>
  139. {
  140. var gitHubAPIURL = request.url;
  141. result.setHeader('Content-Type', 'application/json');
  142. queryGitHubAPI(gitHubAPIURL).then((data)=>
  143. {
  144. if(data.hasOwnProperty("id") || data[0].hasOwnProperty("id"))
  145. {
  146. result.write(JSON.stringify(data));
  147. }
  148. else
  149. {
  150. result.write("[]");
  151. }
  152. result.end();
  153. }).catch((error)=>
  154. {
  155. try
  156. {
  157. if(error.hasOwnProperty("id") || error[0].hasOwnProperty("id"))
  158. {
  159. result.write(JSON.stringify(error));
  160. }
  161. else
  162. {
  163. result.write("[]");
  164. }
  165. }
  166. catch(error)
  167. {
  168. result.write("[]");
  169. };
  170. result.end();
  171. });
  172. if(cache.size() > 50000)
  173. {
  174. cache.clear();
  175. }
  176. });
  177. module.exports = routes;