[PYTHON] Play the comment of Nico Nico Douga on the terminal in conjunction with the video

Motivation

I wanted to see "Dried Sister! Umaru-chan" seen on Amazon Prime Video along with the comments on Nico Nico Douga.

The comment itself of Nico Nico Douga is

How to get comments on Nico Nico Douga with Python

Get it with and save it in a file called result.xml.

The essence of the program below is that comments can be displayed in conjunction with the video. Of course, the sense of presence does not match that of Nico Nico Douga, but I have come to enjoy it as it is.

code

# -*- coding: utf-8 -*-
# For Python 2
import xml.etree.ElementTree as ET
from operator import itemgetter
import time

def vpos_str(vpos):
    sec = int(vpos / 100)
    return "%02d:%02d:%02d" % (int(sec / 60), sec % 60, vpos % 100)

tree = ET.parse('result.xml')
root = tree.getroot()
chats = [(int(c.attrib["vpos"]), c.text) for c in root.findall("chat") if c.text]
chats = sorted(chats, key=itemgetter(0))
st = int(time.time() * 100)
i = 0
while i < len(chats):
    time.sleep(0.01)
    t = int(time.time() * 100) - st
    while i < len(chats) and chats[i][0] <= t:
        print(u"%s %s" % (vpos_str(chats[i][0]), chats[i][1]))
        i += 1

Execution result

00:01:18 Look at
00:01:I bought 55.
00:06:31 !
00:10:91 I charged!
00:11:83 Umaru
00:14:39 Who
00:15:79 Who are you! ??

Recommended Posts

Play the comment of Nico Nico Douga on the terminal in conjunction with the video
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Get the tag search results of Nico Nico Douga in XML format. (No login required)
Play with the UI implementation of Pythonista3 [Super Super Introduction]
Implement Sign In With Google on the backend side
Difference in results depending on the argument of multiprocess.Process
Try scraping the data of COVID-19 in Tokyo with Python
A note on the default behavior of collate_fn in PyTorch
Survey on the use of machine learning in real services
Calculate the square root of 2 in millions of digits with python
Let's execute the command on time with the bot of discord
Analyzing data on the number of corona patients in Japan
Count the number of characters in the text on the clipboard on mac
[Homology] Count the number of holes in data with Python
Until the start of the django tutorial with pycharm on Windows
Play with the password mechanism of GitHub Webhook and Python
Get the host name of the host PC with Docker on Linux
Yield in a class that inherits unittest.TestCase didn't work with nose (depending on the version of nose?)
Watch the video on Fedora31
Life game with Python [I made it] (on the terminal & Tkinter)
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Verification of the spread of hoaxes in the "State of Emergency Declaration on April 1"
Process the contents of the file in order with a shell script
Output the contents of ~ .xlsx in the folder to HTML with Python
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
The story of outputting the planetarium master in pdf format with Pycairo
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
Find the rank of a matrix in the XOR world (rank of a matrix on F2)
I checked the image of Science University on Twitter with Word2Vec.
Get the number of readers of a treatise on Mendeley in Python