Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
AARON FRANCO FERNANDEZ
Análisis de equipos en una jornada de la Liga Santander
Commits
2cbd4e88
Commit
2cbd4e88
authored
Jan 13, 2022
by
AARON FRANCO FERNANDEZ
Browse files
Upload New File
parent
b5ba4088
Changes
1
Hide whitespace changes
Inline
Side-by-side
Código usado/TwitterProducer.java
0 → 100644
View file @
2cbd4e88
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.AsynchronousServerSocketChannel
;
import
java.nio.channels.AsynchronousSocketChannel
;
import
java.nio.channels.CompletionHandler
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Map
;
import
org.apache.commons.lang3.ArrayUtils
;
//import org.apache.log4j.BasicConfigurator;
import
com.vdurmont.emoji.EmojiParser
;
import
twitter4j.FilterQuery
;
import
twitter4j.Query
;
import
twitter4j.StallWarning
;
import
twitter4j.Status
;
import
twitter4j.StatusDeletionNotice
;
import
twitter4j.StatusListener
;
import
twitter4j.TwitterStream
;
import
twitter4j.TwitterStreamFactory
;
import
twitter4j.conf.ConfigurationBuilder
;
public
class
TwitterProducer
{
public
static
final
String
_consumerKey
=
"VGyCZZgWyIFT6VEZueCqQ8tLu"
;
public
static
final
String
_consumerSecret
=
"jybvrneCLuhbpRW3zcNY9Bx06Xb894TwOgBP5dC6soypRroon8"
;
public
static
final
String
_accessToken
=
"628413791-lk8ngRqFA48iPVN7hfV49igeaRLYc1w6GdhQakYR"
;
public
static
final
String
_accessTokenSecret
=
"VaSLJsQ8whIVl9uTy8VoLMttizPtVHowBUSU9mMb7daoZ"
;
private
static
final
boolean
EXTENDED_TWITTER_MODE
=
true
;
public
class
Handler
{
public
AsynchronousSocketChannel
client
;
public
ByteBuffer
out
=
ByteBuffer
.
allocate
(
1024
);
public
ByteBuffer
in
=
ByteBuffer
.
allocate
(
1024
);
}
public
static
void
main
(
String
[]
args
)
{
//BasicConfigurator.configure();
boolean
writeToFile
=
true
;
TwitterProducer
tp
=
new
TwitterProducer
();
ConfigurationBuilder
configurationBuilder
=
new
ConfigurationBuilder
();
configurationBuilder
.
setOAuthConsumerKey
(
_consumerKey
).
setOAuthConsumerSecret
(
_consumerSecret
)
.
setOAuthAccessToken
(
_accessToken
).
setOAuthAccessTokenSecret
(
_accessTokenSecret
);
AsynchronousServerSocketChannel
server
;
AsynchronousSocketChannel
client
=
null
;
Handler
h
=
tp
.
new
Handler
();
try
{
server
=
AsynchronousServerSocketChannel
.
open
();
server
.
bind
(
new
InetSocketAddress
(
"127.0.0.1"
,
9999
));
server
.
accept
(
h
,
new
CompletionHandler
<
AsynchronousSocketChannel
,
Handler
>()
{
@Override
public
void
completed
(
AsynchronousSocketChannel
result
,
Handler
handler
)
{
// prepare for future connections
if
(
server
.
isOpen
())
server
.
accept
(
null
,
this
);
if
(
result
!=
null
&&
result
.
isOpen
())
{
handler
.
client
=
result
;
}
}
@Override
public
void
failed
(
Throwable
exc
,
Handler
attachment
)
{
// TODO Auto-generated method stub
}
});
}
catch
(
IOException
e
)
{
}
TwitterStream
twitterStream
=
new
TwitterStreamFactory
(
configurationBuilder
.
build
()).
getInstance
();
twitterStream
.
addListener
(
new
StatusListener
()
{
/* when a new tweet arrives */
public
void
onStatus
(
Status
status
)
{
if
(
h
!=
null
&&
!
writeToFile
)
{
if
(
h
.
client
!=
null
)
{
// String tweet = status.getText();
// text almacena el tweet original
String
text
=
status
.
getText
();
// elminamos los retornos de carro
String
noBreaksTweet
=
text
.
replace
(
"\n"
,
""
).
replace
(
"\r"
,
""
);
//QUITAMOS \n PARA QUE NOS DEN LOS TWEETS SEGUIDOS
// procesado de tweets con emojis (dos posibilidades)
// 1) eliminar los emojis (resultado, tweet sin emojis ni retornos de carro)
String
noBreaksNoEmojisTweet
=
EmojiParser
.
removeAllEmojis
(
noBreaksTweet
);
// 2) usar un alias para los emojis (resultado, tweet con texto para describir
// emojis (:smile) y sin retornos de carro)
String
noBreaksParsedEmojis
=
EmojiParser
.
parseToAliases
(
noBreaksTweet
);
System
.
out
.
println
(
noBreaksParsedEmojis
);
h
.
out
.
clear
();
h
.
out
.
put
(
noBreaksParsedEmojis
.
getBytes
(
StandardCharsets
.
UTF_8
));
h
.
out
.
flip
();
h
.
client
.
write
(
h
.
out
);
}
}
else
{
String
aux
=
status
.
getText
().
toLowerCase
();
try
{
FileWriter
fr
=
null
;
fr
=
new
FileWriter
(
"Previa.txt"
,
true
);
PrintWriter
pr
=
new
PrintWriter
(
fr
);
// String tweet = status.getText();
// text almacena el tweet original
String
text
=
status
.
getText
();
// elminamos los retornos de carro
String
noBreaksTweet
=
text
.
replace
(
"\n"
,
""
).
replace
(
"\r"
,
""
);
// procesado de tweets con emojis (dos posibilidades)
// 1) eliminar los emojis (resultado, tweet sin emojis ni retornos de carro)
String
noBreaksNoEmojisTweet
=
EmojiParser
.
removeAllEmojis
(
noBreaksTweet
);
// 2) usar un alias para los emojis (resultado, tweet con texto para describir
// emojis (:smile) y sin retornos de carro)
String
noBreaksParsedEmojis
=
EmojiParser
.
parseToAliases
(
noBreaksTweet
);
System
.
out
.
println
(
noBreaksNoEmojisTweet
);
pr
.
println
(
noBreaksNoEmojisTweet
);
pr
.
close
();
fr
.
close
();
}
catch
(
IOException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
}
}
}
@Override
public
void
onException
(
Exception
arg0
)
{
System
.
out
.
println
(
"Exception on twitter"
);
}
@Override
public
void
onDeletionNotice
(
StatusDeletionNotice
arg0
)
{
System
.
out
.
println
(
"Exception on twitter"
);
}
@Override
public
void
onScrubGeo
(
long
arg0
,
long
arg1
)
{
System
.
out
.
println
(
"onScrubGeo"
);
}
@Override
public
void
onStallWarning
(
StallWarning
arg0
)
{
System
.
out
.
println
(
"EonStallWarning"
);
}
@Override
public
void
onTrackLimitationNotice
(
int
arg0
)
{
System
.
out
.
println
(
"EonTrackLimitationNotice"
);
}
});
int
count
=
0
;
String
track
[]
=
new
String
[]
{
"courtois"
,
"vinicius"
,
"bale"
,
"militao"
,
"alaba"
,
"mendy"
,
"nacho"
,
"kroos"
,
"modric"
,
"casemiro"
,
"valverde"
,
"benzema"
,
"hazard"
,
"asensio"
,
"rodrygo"
,
"ancelotti"
,
"xavi"
,
"riqui puig"
,
"araujo"
,
"pique"
,
"pedri"
,
"ansu fati"
,
"gavi"
,
"messi"
,
"morata"
,
"dembele"
,
"ferran"
,
"kubo"
,
"cholo"
,
"luis suarez"
,
"koke"
,
"oblak"
,
"gimenez"
,
"griezmann"
,
"llorente"
,
"carrasco"
,
"felipe"
,
"rayo"
,
"joao"
,
"falcao"
};
long
follow
[]
=
null
;
FilterQuery
tweetFilterQuery
=
new
FilterQuery
(
count
,
follow
,
track
);
// See
tweetFilterQuery
.
language
(
new
String
[]
{
"es"
});
twitterStream
.
filter
(
tweetFilterQuery
);
}
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment