Since a certain idol does not deliver on niconico, the live broadcast is somewhat lonely compared to the previous idol ... → Why not overlay a tweet with a recommended hashtag for live broadcasting?
That's why I made a program in Python that sends Twitter search results like a certain video site.
Captured from https://www.youtube.com/watch?v=G4aogUF_M_Y.
Windows10 Python3.7.6
Since "-transparent color" of tkinter described later is quite environment-dependent, it needs to be realized by another notation in Linux and Mac environments. People in these environments should google appropriately ()
Spaghetti of the soul, which is neither clean nor elaborated, is rolling in here. I want to understand myself well these days.
Register with Twitter API and get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret.
Create a transparent Frame with tkinter
Acquire the search result with an arbitrary search word with Tweepy, and place the character string of the acquired result with appropriate processing as the text of the Label at a certain coordinate.
Reproduce the flow of comments by recursively calling the placed Label with the x coordinate slightly shifted for each nms.
Find a line where the comment has moved to the outside of the screen at intervals of about 5 seconds, and rearrange the search results for that line.
(It feels like who will read it. My memorandum.)
First, use tweepy for the appropriate word = input ()
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)
api = tweepy.API(auth)
results=api.search(q=word,count=num_comment)
To see if the user can use the Twitter API.
If it fails, move to reAuth ()
and enter CK, CS, AT, AS until the authentication is successful, and if it succeeds, write it to config.ini
.
Regarding configparser
Configuration file management module in python program-How to use configparser and precautions-
See.
Next, create a full screen tkinter Frame with a transparent background. The resolution of the display can be obtained below.
from ctypes import windll
ww=windll.user32.GetSystemMetrics(0)#width
wh=windll.user32.GetSystemMetrics(1)#height
You can give this to width
, height
of ttk.Frame ()
.
Transparent Frame
Create a frame with transparent background with tkinter [Python]
Can be made with.
I want the comments to flow over the video, so I set root.wm_attributes ("-topmost ", True)
so that root is in the foreground.
If the comment contains a URL, it will be unnecessarily long.
Exclude tweets containing URLs with tweepy [Python]
Exclude with ʻexURL ()as in. Also, if the comment contains line breaks, the label will span multiple lines and the comments will overlap, so replace it with
text.replace ("\ n", "") `.
Comment acquisition result results
is first copied to nowdatalist
, and the comment is updated about every 5 seconds. By rewriting the element of the line to be rewritten by get_comment ()
to results [i]
, the screen The above comments are managed by nowdatalist
.
get_newcomment()
A function that rearranges a new comment on a line of comments that has flowed off the screen.
The list lefted
(the lack of sense of adding ed to the left left) makes it True
when a comment moves off the screen (x coordinate <0 of a certain label
).
If there is even one True
in lefted
when executing get_newcomment ()
, hit the Twitter API for that number to get the comment and save it in results
.
By specifying since_id
when hitting the API, the id value is higher than the Tweet id. Searches from a group of tweets with a large number.
In addition, according to the specifications of Twitter API, you can hit the API once every 5 seconds, but with a margin, it is executed once every 5.05 seconds.
update_comment()
A function that actually rewrites the screen based on the results
obtained byget_newcomment ()
.
ʻIf (k! = 0 and k% 2 == 0):is used to break the acquired comment without flowing it all at once. That is, 2 comments flow per second. When rewriting
label, the point is to delete the previous
labelas
labels [i] .place_forget (). If you don't do this, the previous
label will continue to remain in the
Frame`.
$pyinstaller jk.py --onefile Finally, this program is converted into an exe file and completed. Not only does this make it easy to run on a non-Python environment PC, Differences in behavior of transparent Frame made with tkinter in pyinstaller [Python] This is because there is a reason described in. I'm not thinking about releasing the exe file.
I studied a lot when I made this program, so I wrote a memorandum.
Create a frame with transparent background with tkinter [Python] Exclude tweets containing URLs with tweepy [Python] Differences in behavior of transparent Frame made with tkinter in pyinstaller [Python] Run Label with tkinter [Python]
The following variables can be changed at any time in the GUI Whether to include fontsize, number of comments, comment length limit, comment default speed and rice length acceleration, URL, RT (I don't think the search word will change that much, so please restart it to solve it.)
When the next comment is sent, if the comment on one line runs out before it overlaps with the next comment, the flow is such that the next comment is added to that line to reproduce the originality. → This is not to judge whether you went to the left end by (lefted) and hit the API for that number to get it, but to get num_comment tweets every 5 seconds, the current coordinates of each line and v, of the next comment Since the judgment must be made in consideration of acc, the processing may become heavy. And above all, it seems that it will be necessary to rewrite the code considerably. Also, managing the y-axis seems to be troublesome. I'm quite satisfied with the current behavior, so I want motivation
Heavy. Since it is heavy, it is very difficult to see that the processing drops and the comment speed changes when the number of characters on the screen increases, so I would like to improve it.
Recommended Posts