JSONファイルから取得した見積もりを投稿しようとしています。私のコードは20秒ごとにツイートを投稿します(テスト目的では20秒です)。私は引用符を見つけてserver(quoteIndex)
関数を使ってJSONファイルに入れることができます。 server(quoteIndex)
は私のoutput.json
ファイルに新しい見積もりを追加します。 output.json
は、偶数インデックスの見積もりを見つけるたびに{"quote": ""}
の部分を更新することを知っています。しかし、私のtweetIt()
関数で、var quoteFile = require("./output.json")
を割り当てると、私のquoteFile.quote
の値は更新されません。私のjavascriptボットは引用符をつぶやくtwitterボットなので、これは問題です。そしてtwitterは二重引用符を許可せず、私に "重複ステータス"エラーを渡します。更新された値を取得していないJSON変数
これは主コード
// I wanted to start at the 8th index and continue with every even index.
var quoteIndex = 8;
// post a tweet every 20 seconds
setInterval(tweetIt, 1000*20);
tweetIt()
function tweetIt() {
//
// This will start looking for quotes to post
// It will put the quote in a JSON file
//
quoteIndex = quoteIndex + 2;
console.log('value of index: ' + quoteIndex)
server(quoteIndex);
var js = require('json-update');
js.load('./output.json', function(err, obj) {
console.log("Loaded from json:");
console.log(obj); // loads most recent quote
}); // this is what I want to tweet
var quoteFile = require('./output.json');
// Read from JSON file for the quote
var params = {
status: quoteFile.quote
}
//
// prints same quote each time, does not update
//
console.log("quote to tweet: " + quoteFile.quote)
// tweet a quote
//
// The first quote will tweet, however when the JSON file
// updates, I will get a "duplicate status" error.
// This is because my quoteFile still has not updated.
//
T.post('statuses/update', params, getData);
function getData(err, data, response) {
if (err) {
console.log("Something went wrong!: " + err);
} else {
console.log("Tweeted something!");
}
}
}
サーバ(quoteNumber)
function server(quoteNumber) {
var fs = require('fs');
var request = require("request"),
cheerio = require("cheerio"),
url = "https://en.wikiquote.org/wiki/A_Song_of_Ice_and_Fire";
request(url, function (error, response, body) {
if (!error) {
var $ = cheerio.load(body);
var quote;
var json = {quote : ""};
$("div.mw-parser-output ul").filter(function(INDEX) {
// even indexed quotes are the ones I want
if (INDEX % 2 == 0 && (INDEX == quoteNumber)) {
quote = $(this).text();
json.quote = quote; // this also has the most recent quote
}
});
} else {
console.log("We’ve encountered an error: " + error);
}
//
// Write our quotes out to a JSON file.
//
fs.writeFile('output.json', JSON.stringify(json, null, 4).replace(/\\n/g, " "), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
});
BASICAでありますlly、node.jsを使ってコードを実行すると、JSONファイルに引用符が付いているため、最初の引用がつぶれてしまいます。その後、quoteIndex = quoteIndex + 2
を使用して次の見積もりを見つけるときは、私のJSONファイルがプロジェクトフォルダ内で予想どおりに更新されます。私の主な問題は、tweetIt()
関数では、JSONファイルに更新された見積もりがあるのに、quoteFile.quote
は更新された見積もりを表示していないことです。更新された見積もりはどうすれば入手できますか?
ヒントありがとう、ありがとう。
私は 'readFile()'の実装について混乱しています。私は 'fs.readFile( './ output.json'、...)'を実行することによってそれに行くことができますが、その中のすべて(引用データ)、私は 'params'変数に割り当てることができません。 – hbhatt687
もちろん、すべてのロジックをコールバックの 'readFile()'の下に置くこともできます。同期呼び出しを使用する場合は、 'readFileSync()'を使用してください。 – mscdex