Stream Twitter search results like a certain video site [python]

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.

Image diagram

live.gif

Captured from https://www.youtube.com/watch?v=G4aogUF_M_Y.

environment

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 ()

Whole code

Spaghetti of the soul, which is neither clean nor elaborated, is rolling in here. I want to understand myself well these days.

Rough flow

  1. Register with Twitter API and get ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret.

  2. Create a transparent Frame with tkinter

  3. 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.

  4. Reproduce the flow of comments by recursively calling the placed Label with the x coordinate slightly shifted for each nms.

  5. 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.

specification

Commentary

(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 withtext.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 rewritinglabel, the point is to delete the previous labelaslabels [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.

Related article

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]

Functions you want to implement in the future

Recommended Posts

Stream Twitter search results like a certain video site [python]
Search Twitter using Python
Search twitter tweets with python
[Python] Let LINE notify you of the ranking of search results on your site on a daily basis.
Write a binary search in Python
Write a depth-first search in Python
Search the maze with the python A * algorithm
Let's make a Twitter Bot with Python!