Сохранение чата twitch.tv
- use "std"
- use "math"
- use "date"
- use "http"
- use "json"
- use "types"
- use "files"
- use "functional"
- DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'"
- vod = ""
- if (length(vod) == 0) {
- use "canvas"
- vod = prompt("Twitch VOD id")
- }
- messages = []
- chunks = []
- thread(::http, "https://api.twitch.tv/kraken/videos/v" + vod,
- combine(::jsondecode, ::processVodInfo))
- def processVodInfo(vodInfo) {
- if (arrayKeyExists("error", vodInfo)) {
- println "Error: " + vodInfo.message
- return 0
- }
- recordDate = newDate(DATE_PATTERN, vodInfo.recorded_at)
- startTime = toTimestamp(recordDate) / 1000
- startTime += (3 * 60 * 60)
- endTime = long(startTime + ceil(vodInfo.length / 30.0) * 30)
- maxIterations = (endTime - startTime) / 30
- for i = 0, i < maxIterations, i++ {
- println sprintf("%d / %d", i + 1, maxIterations)
- timestamp = startTime + i * 30
- url = "http://rechat.twitch.tv/rechat-messages?start=" + timestamp + "&video_id=v" + vod
- chunksString = sync(def(ret) = http(url, ret))
- chunksObject = jsondecode(chunksString)
- foreach(chunksObject.data, ::processChatChunk)
- }
- saveResults(vodInfo)
- }
- def processChatChunk(chunk) {
- attr = chunk.attributes
- formattedDate = formatDate(newDate(attr.timestamp))
- chunks = chunks :: chunk
- messages ::= sprintf("%s (%s)\n%s",
- attr.tags["display-name"], formattedDate, attr.message)
- }
- def saveResults(vodInfo) {
- result = vodInfo.channel.display_name + ": " + vodInfo.title + "\n";
- result += formatDate(newDate(DATE_PATTERN, vodInfo.recorded_at)) + "\n"
- result += vodInfo.url + "\n\n";
- result += join(messages, "\n\n=====\n\n")
- saveFile(vod + "_chat.txt", result)
- saveFile(vod + "_chat.json", jsonencode(chunks))
- }
- def saveFile(filename, content) {
- f = fopen(filename, "w")
- writeText(f, content)
- flush(f)
- fclose(f)
- }
Вводим vod id (восьмизначное число, например 67509345) и получаем чат твича в текстовом файле и в json
Онлайн версия
Онлайн версия