PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/localModules/socialScraper/index.js

https://gitlab.com/mirica.andrei23/useranalysisnode
JavaScript | 276 lines | 266 code | 7 blank | 3 comment | 25 complexity | ea14df3b2df1de2bec93e85344d4f917 MD5 | raw file
  1. /**
  2. * Created by Administrator on 05/04/16.
  3. */
  4. var async = require('async');
  5. var Q = require('q');
  6. var fullContactUrl = 'https://api.fullcontact.com/v2/company/lookup.json?domain=';
  7. var request = require('request');
  8. var Config = require('../../config/environment.js');
  9. var config = new Config();
  10. var indeedScraper = require ('./indeedGlassScraper');
  11. var linkedinscraper = require("./linkedinScraper");
  12. var TwitterScraper = require("./twitterScraper");
  13. var facebookFeed = require("./facebookFeed");
  14. exports.fullProfiles = function fullProfiles(companyURL, FB, FBToken) {
  15. var deferred = Q.defer();
  16. var fullContactCall = fullContactUrl + companyURL + '&apiKey=' + config.fullContactKey;
  17. request(fullContactCall, function (error, response, body) {
  18. if (!error && response.statusCode == 200) {
  19. var objectToSend = {
  20. twitter : [],
  21. facebook: [],
  22. linkedin: []
  23. };
  24. objectToSend.companyLogo = JSON.parse(body).logo ? JSON.parse(body).logo : '';
  25. objectToSend.companyIntel = JSON.parse(body).organization;
  26. if(JSON.parse(body).socialProfiles.length > 0){
  27. var socialProfiles = JSON.parse(body).socialProfiles;
  28. var indeedObj = {
  29. typeId: 'Indeed'
  30. };
  31. if(objectToSend.companyIntel.name.split(' ').length > 0){
  32. indeedObj.id = companyURL.split('.')[0];
  33. } else {
  34. indeedObj.id = objectToSend.companyIntel.name;
  35. }
  36. socialProfiles.push(indeedObj);
  37. async.eachSeries(socialProfiles, function(socialProfile, callback) {
  38. console.log(socialProfile.typeId);
  39. switch(socialProfile.typeId){
  40. case 'twitter':
  41. objectToSend.twitter.push({
  42. url : socialProfile.url,
  43. username: socialProfile.username,
  44. id: socialProfile.id
  45. });
  46. var currentTwitterIndex = objectToSend.twitter.length - 1;
  47. TwitterScraper.crawlTwitterUpdates(objectToSend.twitter[currentTwitterIndex].username, objectToSend.twitter[currentTwitterIndex].url).then(
  48. function(success){
  49. objectToSend.twitter[currentTwitterIndex].newsFeed = [];
  50. objectToSend.twitter[currentTwitterIndex].newsFeed = success;
  51. callback();
  52. },
  53. function(err){
  54. callback(err);
  55. }
  56. );
  57. break;
  58. case 'linkedincompany':
  59. if(!socialProfile.id){
  60. callback();
  61. } else {
  62. objectToSend.linkedin.push({
  63. url : socialProfile.url,
  64. username: socialProfile.username,
  65. id: socialProfile.id
  66. });
  67. var currentLinkedinIndex =objectToSend.linkedin.length - 1;
  68. linkedinscraper.crawlLinkedinUpdates(objectToSend.linkedin[currentLinkedinIndex].url, objectToSend.linkedin[currentLinkedinIndex].id).then(
  69. function (success) {
  70. objectToSend.linkedin[currentLinkedinIndex].newsFeed = success;
  71. callback();
  72. },
  73. function (err) {
  74. callback(err);
  75. }
  76. );
  77. }
  78. break;
  79. case 'facebook':
  80. objectToSend.facebook.push({
  81. url : socialProfile.url,
  82. companyName : socialProfile.url.replace('https://www.facebook.com/', '')
  83. });
  84. var currentFacebookIndex =objectToSend.facebook.length - 1;
  85. facebookFeed.facebookNewsFeed(objectToSend.facebook[currentFacebookIndex].companyName, FBToken, FB)
  86. .then(
  87. function(success){
  88. objectToSend.facebook[currentFacebookIndex].newsFeed = success;
  89. callback();
  90. },
  91. function (err) {
  92. callback(err);
  93. }
  94. );
  95. break;
  96. case 'Indeed':
  97. console.log(socialProfile);
  98. objectToSend.Indeed = {
  99. url : 'http://www.indeed.com/cmp/' + socialProfile.id + '/reviews',
  100. name: socialProfile.id
  101. };
  102. indeedScraper.crawlReviews(objectToSend.Indeed.url).then(
  103. function (success) {
  104. objectToSend.Indeed.newsFeed = success;
  105. callback();
  106. },
  107. function (err) {
  108. callback(err);
  109. }
  110. );
  111. break;
  112. default:
  113. callback();
  114. }
  115. }, function(err){
  116. if( err ) {
  117. console.log(err);
  118. deferred.reject({error: 'Eroare la procesarea profilurilor de socializare!'});
  119. } else {
  120. deferred.resolve(objectToSend);
  121. }
  122. });
  123. } else {
  124. objectToSend.socials = [];
  125. console.log('Wrong!');
  126. deferred.resolve(objectToSend);
  127. }
  128. } else {
  129. deferred.reject({error: 'Eroare la interogare!'});
  130. }
  131. });
  132. return deferred.promise;
  133. };
  134. exports.partialProfiles = function getOnlyProfiles(companyURL){
  135. var deferred = Q.defer();
  136. var fullContactCall = fullContactUrl + companyURL + '&apiKey=' + config.fullContactKey;
  137. request(fullContactCall, function (error, response, body) {
  138. if (!error && response.statusCode == 200) {
  139. var objectToSend = {
  140. twitter : [],
  141. facebook: [],
  142. linkedin: []
  143. };
  144. objectToSend.companyLogo = JSON.parse(body).logo ? JSON.parse(body).logo : '';
  145. objectToSend.companyIntel = JSON.parse(body).organization;
  146. if(JSON.parse(body).socialProfiles.length > 0){
  147. var socialProfiles = JSON.parse(body).socialProfiles;
  148. var indeedObj = {
  149. typeId: 'Indeed'
  150. };
  151. if(objectToSend.companyIntel.name.split(' ').length > 0){
  152. indeedObj.id = companyURL.split('.')[0];
  153. } else {
  154. indeedObj.id = objectToSend.companyIntel.name;
  155. }
  156. socialProfiles.push(indeedObj);
  157. async.eachSeries(socialProfiles, function(socialProfile, callback) {
  158. console.log(socialProfile.typeId);
  159. switch(socialProfile.typeId){
  160. case 'twitter':
  161. objectToSend.twitter.push({
  162. url : socialProfile.url,
  163. username: socialProfile.username,
  164. id: socialProfile.id
  165. });
  166. callback();
  167. break;
  168. case 'linkedincompany':
  169. objectToSend.linkedin.push({
  170. url : socialProfile.url,
  171. username: socialProfile.username,
  172. id: socialProfile.id
  173. });
  174. callback();
  175. break;
  176. case 'facebook':
  177. objectToSend.facebook.push({
  178. url : socialProfile.url,
  179. companyName : socialProfile.url.replace('https://www.facebook.com/', '')
  180. });
  181. callback();
  182. break;
  183. case 'Indeed':
  184. objectToSend.Indeed = {
  185. url : 'http://www.indeed.com/cmp/' + socialProfile.id + '/reviews',
  186. name: socialProfile.id
  187. };
  188. callback();
  189. break;
  190. default:
  191. callback();
  192. }
  193. }, function(err){
  194. if( err ) {
  195. console.log(err);
  196. deferred.reject({error: 'Eroare la procesarea profilurilor de socializare!'});
  197. } else {
  198. deferred.resolve(objectToSend);
  199. }
  200. });
  201. } else {
  202. objectToSend.socials = [];
  203. console.log('Wrong!');
  204. deferred.resolve(objectToSend);
  205. }
  206. } else {
  207. deferred.reject({error: 'Eroare la interogare!'});
  208. }
  209. });
  210. return deferred.promise;
  211. };
  212. exports.profileIntel = function profileIntel(profileName, profileObject, FB, FBToken){
  213. var deferred = Q.defer();
  214. switch (profileName){
  215. case 'twitter':
  216. TwitterScraper.crawlTwitterUpdates(profileObject.username, profileObject.url).then(
  217. function(success){
  218. profileObject.newsFeed = [];
  219. profileObject.newsFeed = success;
  220. deferred.resolve(profileObject);
  221. },
  222. function(err){
  223. deferred.reject(err);
  224. }
  225. );
  226. break;
  227. case 'linkedincompany':
  228. if(!profileObject.id){
  229. deferred.resolve(profileObject);
  230. } else {
  231. linkedinscraper.crawlLinkedinUpdates(profileObject.url, profileObject.id).then(
  232. function (success) {
  233. profileObject.newsFeed = success;
  234. deferred.resolve(profileObject);
  235. },
  236. function (err) {
  237. deferred.reject(err);
  238. }
  239. );
  240. }
  241. break;
  242. case 'facebook':
  243. facebookFeed.facebookNewsFeed(profileObject.companyName, FBToken, FB)
  244. .then(
  245. function(success){
  246. profileObject.newsFeed = success;
  247. deferred.resolve(profileObject);
  248. },
  249. function (err) {
  250. deferred.reject(err);
  251. }
  252. );
  253. break;
  254. case 'Indeed':
  255. indeedScraper.crawlReviews(profileObject.url).then(
  256. function (success) {
  257. profileObject.newsFeed = success;
  258. deferred.resolve(profileObject);
  259. },
  260. function (err) {
  261. deferred.reject(err);
  262. }
  263. );
  264. break;
  265. default:
  266. deferred.resolve(profileObject);
  267. }
  268. return deferred.promise;
  269. };