Created
May 8, 2018 04:36
-
-
Save wibisono/25a5e43d380c08afd35813672a5641b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.danielasfregola.twitter4s.TwitterStreamingClient | |
import com.danielasfregola.twitter4s.entities.{Tweet, User} | |
import org.joda.time.DateTime | |
import scala.collection.mutable | |
object HashtagAnalyser extends App { | |
def percent(cnt: Int, tot: Int) = cnt * 100.0 / tot | |
def computeStatistic(users: Set[User], tweetCount: Int) = { | |
val totalUser = users.size | |
val less50 = users.count(_.followers_count < 50) | |
val from50_100 = | |
users.count(u => u.followers_count < 100 && u.followers_count >= 50) | |
val from100_200 = | |
users.count(u => u.followers_count >= 100 && u.followers_count < 200) | |
val from200_400 = | |
users.count(u => u.followers_count >= 200 && u.followers_count < 400) | |
val from400_800 = | |
users.count(u => u.followers_count >= 400 && u.followers_count < 800) | |
val over800 = users.count(u => u.followers_count >= 800) | |
println( | |
"================================================================================") | |
println( | |
s"=== Analyzing follower and account age of tweeting #HTILanjutkan Perjuangan ===") | |
println( | |
"================================================================================") | |
println( | |
s"Total user found so far : ${users.size} tweet counts : $tweetCount") | |
println( | |
"================================================================================") | |
println(s"Follower < 50: ${percent(less50, totalUser)}%") | |
println(s"50 <= Follower <= 100: ${percent(from50_100, totalUser)}%") | |
println(s"100 <= Follower <= 200: ${percent(from100_200, totalUser)}%") | |
println(s"200 <= Follower <= 400: ${percent(from200_400, totalUser)}%") | |
println(s"400 <= Follower <= 800: ${percent(from400_800, totalUser)}%") | |
println(s"Follower >= 800: ${percent(over800, totalUser)}") | |
val age3m = DateTime.now().minusMonths(3).toDate | |
val age6m = DateTime.now().minusMonths(6).toDate | |
val age1y = DateTime.now().minusYears(1).toDate | |
val age2y = DateTime.now().minusYears(2).toDate | |
val age4y = DateTime.now().minusYears(4).toDate | |
val cntAge3m = users.count(_.created_at.after(age3m)) | |
val cntAge3to6m = | |
users.count(u => u.created_at.after(age6m) && u.created_at.before(age3m)) | |
val cntAge6mto1y = | |
users.count(u => u.created_at.after(age1y) && u.created_at.before(age6m)) | |
val cntAge1yto2y = | |
users.count(u => u.created_at.after(age2y) && u.created_at.before(age1y)) | |
val cntAge2yto4y = | |
users.count(u => u.created_at.after(age2y) && u.created_at.before(age4y)) | |
val cntOlder4y = users.count(u => u.created_at.before(age4y)) | |
println( | |
"================================================================================") | |
println( | |
"======= Account age ==========") | |
println( | |
"================================================================================") | |
println(s" < 3 bulan ${percent(cntAge3m, totalUser)}%") | |
println(s"3 - 6 bulan ${percent(cntAge3to6m, totalUser)}%") | |
println(s"6 - 12 bulan ${percent(cntAge6mto1y, totalUser)}%") | |
println(s"1 - 2 tahun ${percent(cntAge1yto2y, totalUser)}%") | |
println(s"2 - 4 tahun ${percent(cntAge2yto4y, totalUser)}%") | |
println(s"> 4 tahun ${percent(cntOlder4y, totalUser)}%") | |
println( | |
"================================================================================") | |
} | |
val hashtags = Seq("HTILanjutkanPerjuangan") | |
val idUsers = mutable.HashMap[Long, User]() | |
val userCount = mutable.HashMap[Long, Int]() | |
var tweetCount = 0 | |
val streamingClient = TwitterStreamingClient() | |
streamingClient.filterStatuses(tracks = hashtags) { | |
case tweet: Tweet => { | |
tweetCount += 1 | |
System.err.println( | |
s"@${tweet.user.map(_.screen_name).orNull} : ${tweet.text}") | |
tweet.user.map(user => { | |
val curcount = userCount.getOrElseUpdate(user.id, 0) | |
userCount.put(user.id, curcount + 1) | |
idUsers.put(user.id, user) | |
}) | |
if (idUsers.size % 10 == 0) { | |
computeStatistic(idUsers.values.toSet, tweetCount) | |
println("Users: ") | |
userCount.toSeq.sortBy(-_._2).foreach { | |
case (userId, count) => println(idUsers(userId).screen_name, count) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment