gpt4 book ai didi

java - 在Processing中正确读取IRC数据

转载 作者:行者123 更新时间:2023-12-01 13:24:25 26 4
gpt4 key购买 nike

我正在尝试读取 IRC 并从处理中的各个 IRC 消息中提取数据。您可以看到代码(也可以使用 Twitter 库,请忽略它),我需要一些关于如何以 Nick:Message 格式提取数据的指示,以便可以将其显示在可视化中。

    //Twitter

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;



// Import the net libraries
import processing.net.*;

// Declare a client
Client client;


Twitter twitter;
String searchString = "god";
List<Status> tweets;

String server = "irc.twitch.tv";
String nick = "NugShow";
//String user = "simple_bot";
int port = 6667;
String channel = "#nugshow";
String password = "xx";

String in = "butt";
String checkFor;

//bools
Boolean isLive = false;


int privMsgIndex;
int atIndex;
String playerSubstring;



// The channel which the bot will joString channel = "#irchacks";

int currentTweet;

void setup()
{
size(800,600);
frameRate(60);

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xx");
cb.setOAuthConsumerSecret("xx");
cb.setOAuthAccessToken("xx");
cb.setOAuthAccessTokenSecret("xx");

TwitterFactory tf = new TwitterFactory(cb.build());

twitter = tf.getInstance();

getNewTweets();

currentTweet = 0;

thread("refreshTweets");
thread("loopChat");
connectToServer();



//IRC
}

void draw()
{
if (client.available() > 0) {
String in = client.readString();
println(in);

}
if (isLive == false){
if (client.available() > 0) {


}
} else {

}

/*
fill(0, 40);
rect(0, 0, width, height);

currentTweet = currentTweet + 1;

if (currentTweet >= tweets.size())
{
currentTweet = 0;
}

Status status = tweets.get(currentTweet);

fill(200);
text(status.getText(), random(width), random(height), 300, 200);

delay(100);

*/
}

void joinChannel() {
String in = client.readString();
client.write( "JOIN " + channel + "\n\r" );
client.clear();
in = client.readString();
println(in);
if (in != null){
//println("Recieved data");
println(in);
//String inString = myClient.readStringUntil("");

isLive = true;
println(isLive);

}
}

void connectToServer()
{
client = new Client(this, server , 6667);
client.write( "PASS " + password + "\n\r" );
println(password + " sent!");
client.write( "NICK " + nick + "\n\r" );
println(nick + " sent!");
joinChannel();
}


void getNewTweets()
{
try
{
Query query = new Query(searchString);

QueryResult result = twitter.search(query);

tweets = result.getTweets();
}
catch (TwitterException te)
{
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}



void refreshTweets()
{
while (true)
{
getNewTweets();

println("Updated Tweets");

delay(30000);
}
}

void loopChat()
{
while (true)
{

if (privMsgIndex != 0){

println(privMsgIndex);
//privMsgIndex = privMsgIndex - 15;
atIndex = in.indexOf("@");
println(atIndex);
//atIndex = atIndex + 1;
playerSubstring = in.substring(atIndex, privMsgIndex);
println(playerSubstring);
} else {
println("looped");
}



delay(300);
client.clear();
in = null;
}
}

void keyPressed()
{

}


void tweet()
{
try
{
Status status = twitter.updateStatus("This is a tweet sent from Processing!");
System.out.println("Status updated to [" + status.getText() + "].");
}
catch (TwitterException te)
{
System.out.println("Error: "+ te.getMessage());
}
}

聊天命令如下所示: :nugshow!nugshow@nugshow.testserver.local PRIVMSG #nugshow :dddd 其中 nugshow 是用户名,#nugshow 是 channel ,dddd 是消息。我需要将其转换为 nugshow: dddd 的格式。

有很多 header 信息,我不确定如何从 client.recieved 缓冲区中删除,它看起来像这样:

:testserver.local 001 nugshow :Welcome, GLHF!
:testserver.local 002 nugshow :Your host is testserver.local

:testserver.local 003 nugshow :This server is rather new
:testserver.local 004 nugshow :-
:testserver.local 375 nugshow :-
:testserver.local 372 nugshow :You are in a maze of twisty passages, all alike.
:testserver.local 376 nugshow :>

:nugshow!nugshow@nugshow.testserver.local JOIN #nugshow
:nugshow.testserver.local 353 nugshow = #nugshow :nugshow
:nugshow.testserver.local 366 nugshow #nugshow :End of /NAMES list

:jtv!jtv@jtv.testserver.local PRIVMSG nugshow :HISTORYEND nugshow

最佳答案

我不会在这里推荐正则表达式。至少如果您希望能够捕获所有类型的 IRC 消息,则不需要。关键是查看消息代码以了解您实际上可以从消息中得到什么。因为我也在写一个 IRC 客户端(只是为了咯咯笑),所以我有一些笔记给你。

请务必回复服务器发送给您的所有 PING,以免您被踢掉。由于 PING 是通过标识符发送的,因此您需要捕获该标识符并将其发回。执行此操作的一个简单方法是检查从服务器发送的最后一行并对其进行子字符串化。

String line = inputStream.readLine();
if(line.substring(0,4).equals("PING")){
send("PONG " + line.substring(5)); // Assume you have some send-function.
}

这将确保您不会被踢出并可以继续实际留在服务器上。

正如我所提到的,我不建议为此使用正则表达式,因为它会成为正则表达式的厄运。所以,我所做的就是分割你得到的行并将所有结果放入一个字符串数组中。

String[] arr = line.split(" ");

正如您通过自己发布的消息行所知,IRC 协议(protocol)用空格分隔事物。因此,按空格分割并不是那么糟糕(我们稍后会了解如何处理实际文本)。消息中始终相同的基本结构(据我所知)是 PREFIX COMMAND PARAM PARAM/TRAILING。那么这是什么意思? PREFIX 是消息的发送位置。示例“:user!user@host.com”。命令就是该行的实际内容。您正在此处寻找 PRIVMSG,但您可能还需要处理很多很多其他内容。例如 JOIN、PART、QUIT、332(当前主题)、353( channel 中的缺口、404(无法发送到 channel )等。 PARAM 和 PARAM/TRAILING 都取决于 COMMAND。

那么,我们从空间 split 中得到什么?这:

arr[0] = :user!user@host.com
arr[1] = COMMAND
arr[2] = PARAM
arr[3 onwards] = rest

我们现在可以按照自己需要的方式轻松管理每个命令。

事不宜迟,让我们开始讨论您的实际问题:PRIVMSG。我将使用这个字符串:“:Chewtoy!chewtoy@stackoverflow.com PRIVMSG #stackoverflow:Ty:我看到你的问题并认为我应该给你一个答案。”在空格处进行分割后,我们得到数组

arr[0] = :Chewtoy!chewtoy@stackoverflow.com
arr[1] = PRIVMSG
arr[2] = #stackoverflow
arr[3] = :Ty:
arr[4] = I
arr[5] = saw
...

如您所见,从 3 开始的所有内容都是您想要的消息。 0-2 是你需要能够知道谁发送了什么内容到哪里的东西。我获取所有这些的代码如下所示:

String[] arr = receivedLine.split(" ");
if(arr[1].equals("PRIVMSG")){
String[] usr = arr[0].split(!"); // Get the user, which is in usr[0]. Host in usr[1]
StringBuilder msg = new StringBuilder();
for(int i=3; i<arr.length; i++){ // We know the message starts at arr[3], so start counting from that.
msg.append(arr[i] + " ");
}

String chan = "";
if(arr[2].substring(0,1).equals("#")){ // We need to differentiate between sending to channel and sending a PM. The only difference in this is where the text is sent.
chan = arr[2];
} else{
chan = usr[0].substring(1); // substring(1) because we want Chewtoy, not :Chewtoy
}

// The result here will be:
// <#stackoverflow> Chewtoy: Ty: I saw your question and thought I should give you an answer.
sendItAllToWhereYouWantIt("<" + chan +"> " + usr[0].substring(1) + ": " + msg.substring(1));
}

希望有帮助。

关于java - 在Processing中正确读取IRC数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21847972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com