Created
January 4, 2023 01:05
-
-
Save iProdigy/e6f19a5644326382de4b175f28ccea37 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.github.twitch4j.helix.TwitchHelixBuilder | |
import com.github.twitch4j.helix.domain.Clip | |
import com.github.twitch4j.util.PaginationUtil | |
import org.apache.commons.lang.StringEscapeUtils | |
import java.nio.file.Files | |
import java.nio.file.Paths | |
import java.time.Instant | |
import java.time.temporal.ChronoUnit | |
import java.util.concurrent.ConcurrentHashMap | |
import java.util.concurrent.Executors | |
import java.util.concurrent.TimeUnit | |
private val api = TwitchHelixBuilder.builder() | |
.withClientId("your_client_id") | |
.withClientSecret("your_client_secret") | |
.build() | |
private val initial = Instant.parse("2022-01-01T00:00:00Z") | |
private val cutoff = Instant.now() | |
private const val channelId = "207813352" | |
private const val viewThreshold = -1 | |
fun main() { | |
val exec = Executors.newWorkStealingPool() | |
val clips = ConcurrentHashMap.newKeySet<Clip>() | |
for (i in 0..Int.MAX_VALUE) { | |
val start = initial.plus(i.toLong(), ChronoUnit.DAYS).takeIf { it < cutoff } ?: break | |
val end = start.plus(24L, ChronoUnit.HOURS).plus(10L, ChronoUnit.MINUTES) | |
exec.execute { | |
clips += PaginationUtil.getPaginated( | |
{ | |
try { | |
val r = api.getClips(null, channelId, null, null, it, null, 100, start, end).execute() | |
if (r.data.all { c -> (c.viewCount ?: 0) <= viewThreshold }) null else r | |
} catch (e: Exception) { | |
e.printStackTrace() | |
null | |
} | |
}, | |
{ it?.data }, | |
{ it?.pagination?.cursor } | |
) | |
} | |
} | |
exec.shutdown() | |
exec.awaitTermination(60, TimeUnit.MINUTES) | |
Files.newBufferedWriter( | |
Paths.get("C:\\clips-$channelId-${Instant.now().toEpochMilli()}.csv") | |
).use { out -> | |
out.write("id,creator,game,title,video,offset,views,duration,created") | |
out.newLine() | |
for (c in clips) { | |
val fields = arrayOf( | |
c.id, | |
c.creatorName, | |
c.gameId, | |
c.title, | |
c.videoId, | |
c.vodOffset?.toString(), | |
c.viewCount?.toString(), | |
c.duration?.toString(), | |
c.createdAtInstant.toString() | |
).joinToString(separator = ",") { StringEscapeUtils.escapeCsv(it ?: "") } | |
out.write(fields) | |
out.newLine() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment