将变量传递给RestAPI

时间:2017-07-03 03:58:18

标签: jquery api

您好我的代码中出现了小错误。我的代码中有3个函数。我想从downloadCSV函数获取videoid,我需要将相同的videoid传递给downloadCSVVV函数并获取所有的注释特别是视频id.SO,为此我声明了一个全局变量,我在第一个函数即downloadCSV函数中传递它。我在downloadCSVVV函数中传递相同的视频,我在该变量中得到的值是未定义的。你能帮我解决这个问题吗。下面是我的代码。

<!doctype html>
<html>

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>

<body>
    <a href='#' onclick='downloadCSV({ filename: "a.csv" }); downloadCSVV({ filename1:"b.csv"}); downloadCSVVV({ filename2:"c.csv"});'>Download CSV</a>

    <script type="text/javascript">
        $(document).ready(function() {});
        var videos = [];
        function convertArrayOfObjectsToCSV(args) {
            var result, ctr, keys, columnDelimiter, lineDelimiter, data;
            console.log("vid"+videos);
            data = args.data || null;
            if (data == null || !data.length) {
                return null;
            }
            columnDelimiter = args.columnDelimiter || ',';
            lineDelimiter = args.lineDelimiter || '\n';
            keys = Object.keys(data[0]);
            result = '';
            result += keys.join(columnDelimiter);
            result += lineDelimiter;
            data.forEach(function(item) {
                ctr = 0;
                keys.forEach(function(key) {
                    if (ctr > 0) result += columnDelimiter;
                    result += item[key];
                    ctr++;
                    console.log("columnDelimiter " + result);
                });
                result += lineDelimiter;
            });
            return result;

        }

        function downloadCSV(args) {
                var youtubeData = [];
            var data, filename, link,videos;

            $.getJSON('https://www.googleapis.com/youtube/v3/search?part=snippet&q=laptops&maxResults=50&key=""', function(data) {

                for(var i = 0; i < data.items.length; i++) {
                    videos = data.items[i].id.videoId;

                youtubeData.push({
                    "videoid": videos ,
                    "Title": data.items[i].snippet.title
                });
            }
                var csv = convertArrayOfObjectsToCSV({
                    data: youtubeData
                });
                if (csv == null) return;

                filename = args.filename || 'export.csv';

                if (!csv.match(/^data:text\/csv/i)) {
                    csv = 'data:text/csv;charset=utf-8,' + csv;
                }
                data = encodeURI(csv);

                link = document.createElement('a');
                link.setAttribute('href', data);
                link.setAttribute('download', filename);
                link.click();
            });
        }


        function downloadCSVVV(args) {
            var items = [];
            var youtubeData2 = [];
            var data2, filename2, link,videos;
            $.getJSON('https://www.googleapis.com/youtube/v3/commentThreads?key=""&textFormat=plainText&part=snippet,replies&videoId='+videos+'&maxResults=60', function(data2) {
                for(var i = 0; i < data2.items.length; i++) {
                youtubeData2.push({
                    "videoid": data2.items[i].snippet.topLevelComment.snippet.videoId,
                    // "videoid": videos[0],
                    "Authordisplayname": data2.items[i].snippet.topLevelComment.snippet.authorDisplayName,
                    "Textdisplay": data2.items[i].snippet.topLevelComment.snippet.textDisplay,
                });
                }

                console.log(data2);
                var csv = convertArrayOfObjectsToCSV({
                    data: youtubeData2
                });
                if (csv == null) return;

                filename2 = args.filename2 || 'export.csv';

                if (!csv.match(/^data:text\/csv/i)) {
                    csv = 'data:text/csv;charset=utf-8,' + csv;
                }
                data2 = encodeURI(csv);

                link = document.createElement('a');
                link.setAttribute('href', data2);
                link.setAttribute('download', filename2);
                link.click();
            });
        }


         function downloadCSVV(args) {
              var youtubeData1 = [];
            var data1, filename1, link;

            $.getJSON('https://www.googleapis.com/youtube/v3/videos?id=kmJM7Z6_DEw,zsvLai5jxCs,N9uMFDotJPg,OlTH_XPlFrw&key=""&part=snippet,statistics', function(data1) {
                console.log(data1);
                for(var i = 0; i < data1.items.length; i++) {
                youtubeData1.push({
                    "videoid": data1.items[i].id,
                    "Id": data1.items[i].snippet.categoryId,
                    "ChannelId": data1.items[i].snippet.channelId,
                    "Title": data1.items[i].snippet.title,
                    // "Description": data.items[i].snippet.description.replace("\n", ""),
                    "Commentcount": data1.items[i].statistics.commentCount,
                    "ViewCount": data1.items[i].statistics.viewCount,
                    "LikeCount": data1.items[i].statistics.likeCount,
                    "DislikeCount": data1.items[i].statistics.dislikeCount,
                    "FavoriteCount": data1.items[i].statistics.favoriteCount
                });
            }
                var csv = convertArrayOfObjectsToCSV({
                    data: youtubeData1
                });
                if (csv == null) return;

                filename1 = args.filename1 || 'export.csv';

                if (!csv.match(/^data:text\/csv/i)) {
                    csv = 'data:text/csv;charset=utf-8,' + csv;
                }
                data1 = encodeURI(csv);

                link = document.createElement('a');
                link.setAttribute('href', data1);
                link.setAttribute('download', filename1);
                link.click();
            });
        }







    </script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

不要同时调用所有函数,如果要将值从1个函数传递给另一个函数,则调用第一个函数,如第一个函数,

<强> HTML:

<a href='#' onclick='downloadCSV({ filename: "a.csv" });'>Download CSV</a>

<强> JS:

function downloadCSV(args) {
   var youtubeData = [];
   var data, filename, link,videos;

   $.getJSON('https://www.googleapis.com/youtube/v3/search?part=snippet&q=laptops&maxResults=50&key=""', function(data) {
      for(var i = 0; i < data.items.length; i++) {
          videos = data.items[i].id.videoId;
          youtubeData.push({
              "videoid": videos ,
              "Title": data.items[i].snippet.title
          });
          // calling the dependent functions here
          downloadCSVV({ filename1:"b.csv"},videos);
          downloadCSVVV({ filename2:"c.csv"},videos);

      }
      var csv = convertArrayOfObjectsToCSV({
            data: youtubeData
      });
      if (csv == null) return;

      filename = args.filename || 'export.csv';

      if (!csv.match(/^data:text\/csv/i)) {
          csv = 'data:text/csv;charset=utf-8,' + csv;
      }
      data = encodeURI(csv);

      link = document.createElement('a');
      link.setAttribute('href', data);
      link.setAttribute('download', filename);
      link.click();
   });
}

function downloadCSVV(args,videos){
   // your code
   // now use videos here
}

function downloadCSVVV(args,videos){
   // your code
   // now use videos here
}

针对多个视频ID进行了更新

var videoIdArr=[];
for(var i = 0; i < data.items.length; i++) {
    videos = data.items[i].id.videoId;
    youtubeData.push({
       "videoid": videos ,
       "Title": data.items[i].snippet.title
    });
    videoIdArr.push(videos);
}
// calling the dependent functions here with video is array
downloadCSVV({ filename1:"b.csv"},videoIdArr);
downloadCSVVV({ filename2:"c.csv"},videoIdArr);

注意:我不确定这是否有效,但GoogleAPIs不支持。