Guest User

Untitled

a guest
Jan 24th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. console.log("Loading up the best code ever!!!");
  2.  
  3. //packagess import
  4. var fs = require('fs');
  5. var AWS = require('aws-sdk');
  6. var Singer = require('./Singer')
  7.  
  8. //prepare all the AWS S3 data
  9. AWS.config.update({ region: "us-west-1" });
  10. var credentials = new AWS.SharedIniFileCredentials();
  11. AWS.config.credentials = credentials;
  12. // Create S3 service object
  13. s3 = new AWS.S3({ apiVersion: '2006-03-01' });
  14.  
  15. // Create the parameters for calling createBucket
  16. var bucketParams = {
  17. Bucket: 'pc-backend-exercises',
  18. Key: 'toSearch.json',
  19. ResponseContentType: 'application/json'
  20. };
  21.  
  22. /// prepare the youtube-data-api
  23. var YTAPI = require('node-youtubeapi-simplifier');
  24. var APIKEY = 'AIzaSyDS1p3m9wnZLLCPc1hDQBX3K_UnS4j0CdY'
  25. YTAPI.setup(APIKEY);
  26.  
  27. console.log('connecting to s3 to get the json file');
  28.  
  29. var singers = [];
  30.  
  31. s3.getObject(bucketParams, function (err, data) {
  32. // Handle any error and exit
  33. if (err) {
  34. console.log(err, err.stack);
  35. return err;
  36. }
  37. var fileContents = data.Body.toString();
  38. var json = JSON.parse(fileContents);
  39.  
  40. for (var i = 0; i < json.Search.artists.length; i++) {
  41. var newSinger = new Singer(json.Search.artists[i]);
  42. singers.push(newSinger);
  43. }
  44. console.log('the list of the singers has %d singers',json.Search.artists.length);
  45. search10TopForASinger();
  46. });
  47.  
  48.  
  49. //this function searches using youtube-api-simplfier for each artist name
  50. //returns the list of songs and collects only 10, stores them into a map in the singer object
  51. function search10TopForASinger() {
  52. console.log('entered search10TopForASinger ')
  53. var promises = [];
  54.  
  55. for (var i = 0; i < singers.length; i++) {
  56. //Gets only 10 results
  57. promises.push(YTAPI.searchFunctions.simpleSearch(singers[i].name));
  58. }
  59. Promise.all(promises)
  60. .then((results) => {
  61. console.log('all the threads of the youtube api are back')
  62. for (var i = 0; i < singers.length; i++) {
  63. setSongsArray(results[i], singers[i].songs);
  64. }
  65.  
  66. printJsonAndUpload();
  67. console.log('we are done you can test yourself with testGiladFile.js');
  68. })
  69. .catch((e) => {
  70. // Handle errors here
  71. });
  72.  
  73. }
  74.  
  75.  
  76. //sets 10 or less songs for each artist into an array
  77. //we use the Singer object in order to store the data
  78. function setSongsArray(data, songs) {
  79. console.log('entered setSongsArray')
  80. var size = 10;
  81. if (data.length < 10) {
  82. size = data.length;
  83. }
  84.  
  85. for (var i = 0; i < size; i++) {
  86. songs.push(data[i].title);
  87. }
  88. }
  89.  
  90. //Shutterfly team wanted a json file with a certain format
  91. //we create here to format and upload it back to S3
  92. function printJsonAndUpload() {
  93.  
  94. console.log('entered printJson')
  95. var data = {
  96. results: []
  97. };
  98.  
  99. for (var i = 0; i < singers.length; i++) {
  100. var singerName = singers[i].name;
  101. var singerObj = {}
  102. singerObj[singerName] = []
  103. for (var song = 0; song < singers[i].songs.length; song++) {
  104. var songObj = {};
  105. songObj[song+1] = singers[i].songs[song];
  106. singerObj[singerName].push(songObj)
  107. }
  108. data.results.push(singerObj);
  109. }
  110.  
  111. console.log('uploading file to s3')
  112. s3.putObject({Bucket: 'pc-backend-exercises',Key:'gilad.json',Body: JSON.stringify(data), ContentType: "application/json"},
  113. function(err, data) {
  114. //console.log('error');
  115. //console.log(JSON.stringify(err)+" "+JSON.stringify(data));
  116. });
  117. console.log('the name of the file is gilad.json');
  118. }
Add Comment
Please, Sign In to add comment