Node.js缩短了网址

时间:2017-04-04 02:58:27

标签: javascript node.js npm

我刚刚开始学习编码大约5天前,我正在努力实现的目标是使用rssfeed-to-twitter脚本发布缩短的网址而不是完整的网站/文章提要网址。我发现了一个名为TinyURL的node.js模块可以做到这一点,但我很难让它发挥作用。这是完整的脚本:

var simpleTwitter = require('simple-twitter');
var fs = require('fs');
var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.end('RSS Twitter Bot\n');
}).listen(5693);

var timeInterval = 300000; // run every 30m
var timerVar = setInterval (function () {runBot()}, timeInterval); 

function runBot(){
    var lastCompleted = Date.parse(new Date(0));
    console.log(lastCompleted);

    try {
        var lastcompletedData = fs.readFileSync('./lastCompleted.json', 'utf8');
        var timeData = JSON.parse(lastcompletedData);

        var lastCompletedFromFile = Date.parse(new Date(timeData.lastCompleted));
        if ( isNaN(lastCompletedFromFile) == false ) {
            lastCompleted = lastCompletedFromFile;
        }

    } catch (e) {
        console.log(e);
    }

    fs.readFile('./config.json', 'utf8', function (err, data) {
        if (err) console.log(err); // we'll not consider error handling for now
        var configData = JSON.parse(data);

        console.log(configData);

        var twitter = new simpleTwitter( configData.consumerKey //consumer key from twitter api
                                 , configData.consumerSecret //consumer secret key from twitter api
                                 , configData.accessToken //access token from twitter api 
                                 , configData.accessTokenSecret //access token secret from twitter api 
                                 , 3600); 

        var dateNow = Date.parse(new Date());
        var FeedParser = require('feedparser');
        var request = require('request');

        var req = request(configData.feedUrl);
        var feedparser = new FeedParser();

        req.on('error', function (error) {
            console.log(error);
        });

        req.on('response', function (res){
            var stream = this;

            if (res.statusCode != 200 ) return this.emit('error', new Error('Bad status code'));
            stream.pipe(feedparser);
        });

        feedparser.on('error', function(error) {  
            console.log(error);
        });

        feedparser.on('readable', function() { 
            var stream = this;
            var meta = this.meta;

            var item;

            while (item = stream.read()) {
                var itemDate = Date.parse(item.date);

                //check to not publish older articles
                if (itemDate > lastCompleted){
                    var titleLength = item.title.length;
                    var itemTitle = item.title;
                    var itemLink = item.link;

                    if (titleLength > 100) {
                        itemTitle = itemTitle.substring(0, 100);
                    }

                    twitter.post('statuses/update'
                                , {'status' : itemTitle + ' ' + itemLink + " " + configData.tags}
                                , function (error, data) {
                                    console.dir(data);
                                });

                    console.log(itemTitle + ' ' + item.link + configData.tags);
                }
            }

            //TO KNOW WHEN FROM TO START POSTING
            var dateCompleted = new Date();
            console.log('loop completed at ' + dateCompleted);

            var outputData = {
              lastCompleted : dateCompleted
            }

            var outputFilename = './lastCompleted.json';

            fs.writeFile(outputFilename, JSON.stringify(outputData, null, 4), function(err) {
                if(err) {
                  console.log(err);
                } else {
                  console.log("JSON saved to " + outputFilename);
                }
            }); 
        });
    });
}

这是TinyURL node.js模块

var TinyURL = require('tinyurl');

TinyURL.shorten('http://google.com', function(res) {
    console.log(res); //Returns a tinyurl 
});

'http://google.com'字符串更改为itemLink var工作正常,并按预期将其打印在终端中。

TinyURL.shorten(itemLink, function(res) {
    console.log(res); //Returns a tinyurl 
});

我想要实现的目标是:

twitter.post('statuses/update', {'status' : itemTitle + ' ' + tinyurlLink + " " + configData.tags}

如何将响应转换为例如var tinyurlLink以替换itemLink var?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

正如@zerkms从TinyURL.shorten工作内部发送推文所建议的那样!