我已经打了一个项目,我一直在努力的路障,我想知道如果有人可以帮助我与此。P5.js,节点和Twitter的API
因此,我试图开发一个node.js应用程序,它使用HTML表单查询Twitter API - 然后使用检索的数据(以JSON形式)在P5.js草图中显示推文。
到目前为止,我已经能够使用查询API的表单并将拉动的推文写入JSON文件。 单独,我也能够将JSON文件拉入P5并按我认为合适的方式处理数据。
问题是 - 我的最终游戏是我希望它是一个无缝的过程,我需要初步向用户提供草图,加载默认数据集。然而,我需要的形式来更新动态草图(使用AJAX我想象?)
我的问题是:是否有一个不太复杂的方式来实现?我正在猜测使用JQuery & Ajax的组合,但我很疲惫,试图辨别什么会运行客户端,什么会运行服务器端。
TL; DR - 我想传递用户输入的数据作为参数来查询Twitter API并异步更新p5.js草图。
我已经附上了我正在使用的代码,如果有人能够帮助我,我将非常感激,因此我不必拔出更多的头发!即使把我指向正确的方向也会有很大的帮助。
感谢提前:)
Server.js
var http = require('http'),
util = require('util'),
fs = require('fs'),
url = require('url'),
qs = require('querystring');
//Imports the Twit package
var Twit = require('twit');
var totalTweets = [];
var query;
var tweetcount;
//Imports the JSONFILE package
var jsonfile = require('jsonfile')
jsonfile.spaces = 3
//Creates new Twit object
var T = new Twit({
consumer_key: 'x'
, consumer_secret: 'x'
, access_token: 'x'
, access_token_secret: 'x'
})
//Parameters for the search
var params = {
q: "Hello",
lang:" ",
count: 70
}
var server = http.createServer(function (req,res){
var url_parts = url.parse(req.url,true);
//console.log(url_parts);
var body = '';
if(req.method === 'POST'){
// res.end('post');
console.log('Request found with POST method');
req.on('data', function (data) {
body += data;
console.log('got data:'+data);
});
req.on('end', function() {
var POST = qs.parse(body);
// use POST
res.end("Sent data are name:"+POST.name+" age:"+POST.age);
});
} else {
console.log('Request found with GET method');
req.on('data',function(data){ res.end(' data event: '+data);});
if(url_parts.pathname == '/')
fs.readFile('./form.html',function(error,data){
console.log('Serving the page form.html');
res.end(data);
});
else if(url_parts.pathname == '/getData'){
console.log('Serving the Got Data.');
getData(res,url_parts);
}
}
});
server.listen(8080);
console.log('Server listenning at localhost:8080');
function getData(res,url_parts){
console.log("Data submitted by the user name: "+url_parts.query.name+" and age:"+url_parts.query.age);
res.end("Data submitted by the user name: "+url_parts.query.name+" and age: "+url_parts.query.age);
tweetcount = url_parts.query.age;
query = url_parts.query.name;
console.log(tweetcount + query);
//Parameters for the search
var params = {
q: query,
lang:" ",
count: tweetcount
}
//Searches Twitter Based on arguments
T.get('search/tweets', params, gotData)
function gotData(err, data, response){
var tweets = data.statuses;
//Creates a loop that stores tweets in an array
for (var i=0; i < tweets.length; i++) {
totalTweets[i] = {
Username: "@" + tweets[i].user.name,
Tweet: tweets[i].text,
Time: tweets[i].created_at
};
}
var file = 'tweets.json'
//var obj = JSON.stringify(pulledtweets);
//Writes contents of array to JSON file
jsonfile.writeFileSync(file, totalTweets);
//jsonfile.writeFileSync(file, JSON.stringify(obj))
console.log("File written successfully");
console.log("Total number of tweets : " + tweets.length);
}
}
Sketch.js
var myTweets;
var x;
var brokenTweet;
var totalWords = [];
var currentTweet;
var obj = { };
console.log(obj);
function preload() {
var tweetsFile = "tweets.JSON"
myTweets = loadJSON(tweetsFile);
}
console.log(totalTweets);
function setup(){
createCanvas(windowWidth,windowHeight);
background(255);
fill(125);
ellipse(100,100,50,50);
x = 1;
// create canvas
createCanvas(windowWidth, windowHeight);
input = createInput();
input.position(width/2 - input.width/2, 100);
button = createButton('submit');
button.position(input.x + input.width, 100);
textAlign(CENTER);
textSize(50);
console.log(myTweets[x].Tweet);
}
function draw(){
background(255);
textSize(18);
fill(0);
text(myTweets[x].Tweet,input.x-25,input.y+50, 300,300);
}
function greet(){
}
function keyPressed(){
if(keyCode == UP_ARROW){
x++;
console.log(Object.keys(myTweets).length);
//Object.keys counts the amount of object in JSON file
for(var i = 0; i < (Object.keys(myTweets).length) ; i++){
currentTweet = JSON.stringify(myTweets[i].Tweet);
brokenTweet = currentTweet.split(" ");
//console.log(brokenTweet);
for(var a = 0; a < brokenTweet.length; a++){
var currentWord = brokenTweet[a];
// console.log(currentWord);
totalWords.push(currentWord.replace(/[^a-zA-Z]/g, ''));
}
//console.log(totalWords);
}
//Loops through array and
for (var i = 0, j = totalWords.length; i < j; i++) {
if (obj[totalWords[i]]) {
obj[totalWords[i]]++;
}
else {
obj[totalWords[i]] = 1;
}
}
console.log(obj);
}
if(keyCode == DOWN_ARROW){
x--;
if(x < 0){
x = 0;
}
}
}
form.html