Created
January 9, 2018 07:57
-
-
Save garex/bc5e3a73a97bf2757c4824be00883fb2 to your computer and use it in GitHub Desktop.
vkontakte wall comments into subtitles (SBV)
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
#!/usr/bin/env php | |
<?php | |
$options = getopt('', [ | |
'url:', | |
'first-comment-to:' | |
]); | |
if (empty($options['url']) || empty($options['first-comment-to'])) { | |
exit('Parameters: --url=https://vk.com/sibhl?w=wall-96808993_127304 --first-comment-to=00:00:00'.PHP_EOL); | |
} | |
$url = parse_url($options['url']); | |
parse_str($url['query'], $query); | |
$wall = $query['w']; | |
preg_match('/^wall(.+)_(.+)$/', $wall, $m); | |
$ownerId = $m[1]; | |
$postId = $m[2]; | |
$apiUrl = sprintf( | |
'https://api.vk.com/method/wall.getComments?owner_id=%d&post_id=%d&need_likes=0&count=100&preview_length=0&v=5.69', | |
$ownerId, | |
$postId | |
); | |
$response = file_get_contents($apiUrl); | |
$json = json_decode($response, true); | |
$items = $json['response']['items']; | |
$diff = null; | |
foreach ($items as $item) { | |
if ($item['from_id'] != $ownerId) { | |
continue; | |
} | |
$date = new DateTimeImmutable(); | |
$date = $date->setTimestamp($item['date']); | |
if (null == $diff) { | |
$date1 = new DateTime($date->format('H:i:s')); | |
$date2 = new DateTime($options['first-comment-to']); | |
$diff = $date2->diff($date1); | |
} | |
$date = $date->sub($diff); | |
$writeTime = intval(mb_strlen($item['text']) / 2); | |
$readTime = intval(mb_strlen($item['text']) / 5); | |
$showTime = $writeTime + $readTime; | |
echo $date->sub(DateInterval::createFromDateString($showTime.' seconds'))->format('H:i:s').","; | |
echo $date->format('H:i:s')."\n"; | |
echo $item['text']."\n\n"; | |
} | |
// 300 знаков в минуту, 5 знаков в секунду, чтение | |
// 120 знаков в минуту, 2 знака в секунду, набор | |
// https://support.google.com/youtube/answer/2734698?hl=ru | |
// *.sbv формат | |
// ./vk-subtitles.php --url=https://vk.com/sibhl?w=wall-96808993_127304 --first-comment-to=00:00:59 > titles.sbv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment