Notify LINE of TV appearance information of your favorite entertainer

Purpose

I miss the appearance of my favorite idol and comedian on TV, so I decided to notify them on LINE. 2020-05-15 (9).png

Overview

[Yahoo! TV G Guide](https://tv.yahoo.co.jp/search/?q=%E7%9C%89%E6%9D%91%E3%81%A1%E3%81%82% We obtained TV program information from E3% 81% 8D & g = & a = 23 & oa = 1 & s = 1% 22) and notified it using LINE Notify. I set the program to run on my Raspberry pi at 9am every morning.

Program information acquisition

For example, in the case of the terrestrial program of "Chiaki Mayumura" (regional setting Tokyo), this is the URL. https://tv.yahoo.co.jp/search/?q=眉村ちあき&g=0&a=23&oa=1&s=1

Define a function called tv_information (query, romaji, jenre) and receive the name of the entertainer you want to notify as input, the name of the text file that records the appearance program, and the genre of the tv program you want to notify. I got the information from the URL that I entered. This time, we are limited to terrestrial programs in Tokyo. I wanted to be notified of information about comedy duo and New York, but since many programs related to New York in the US state are notified in the New York ward, I am making it possible to specify the genre of the program and notify it. Enter jenre = 0 for all genres and genre = 5 for variety.

LINE LINE Notify I got a token from here. Enter the obtained token in place of line_notify_token =" Obtained LINE API ".

Code used

tv_line.py


import requests
from bs4 import BeautifulSoup
import sys
from time import sleep

#Get TV information(argument:Performer's name, romaji(Make it a file name), Genre)
def tv_information(query,romaji,jenre):
#0 for all genres, 5 for variety
    if jenre==0:
        no=''
    elif jenre==5:
        no='05'
    url = "https://tv.yahoo.co.jp/search/?q="+query+"&g="+no+"&a=23&oa=1&s=1" 
    res = requests.get(url)
    status = res.status_code

    #If the status code of Requests is not 200, notify LINE and exit.
    if status != 200:
        def LineNotify(message):
            line_notify_token = "Acquired LINE API"
            line_notify_api = "https://notify-api.line.me/api/notify"
            payload = {"message": message}
            headers = {"Authorization": "Bearer " + line_notify_token}
            requests.post(line_notify_api, data=payload, headers=headers)
        message = "Requests failed"
        LineNotify(message)
        sys.exit()
    

    #If the status code is 200, processing continues
    else:
        pass
    
    #Get the number of keyword searches
    soup = BeautifulSoup(res.text,"html.parser")
    p = soup.find("p",class_="floatl pt5p")

    #If the number of searches is 0, the process ends
    if p == None:
        print('0')
        non={}
        return non  

    #Continue processing if the number of searches is 1 or more
    else:
        pass
    
    answer = int(p.em.text) #Number of searches
    page = 1
    list1 = []  #Broadcast date and time
    list2 = []  #Broadcaster
    list3 = []  #Program title

    #Get information for each page
    while answer > 0:
        url = "https://tv.yahoo.co.jp/search/?q="+query+"&g="+no+"&a=23&oa=1&s="+str(page)
        res = requests.get(url)
        soup = BeautifulSoup(res.text, "html.parser")

        dates = soup.find_all("div",class_="leftarea")
        for date in dates:
            d = date.text
            d = ''.join(d.splitlines())
            list1.append(d)

        for s in soup("span",class_="floatl"):
            s.decompose()
        tvs = soup.find_all("span",class_="pr35")
        for tv in tvs:
            list2.append(tv.text)

        titles = soup.find_all("div",class_="rightarea")
        for title in titles:
            t = title.a.text
            list3.append(t)

        page = page + 10
        answer = answer - 10

        sleep(3)

    #A list that summarizes the broadcast date and time + broadcasting station + program title from list1 to list3_Create new
    list_new = [x +" "+ y for (x , y) in zip(list1,list2)]
    list_new = [x +" "+ y for (x , y) in zip(list_new,list3)]
    filename=romaji+'.txt'
    #Expand the previous data as a set from a text file
    f = open(filename,'r') 
    f_old = f.read()
    list_old = f_old.splitlines()   
    set_old = set(list_old)         
    f.close()           
    
    f = open(filename, 'w') 
    for x in list_new:
        f.write(str(x)+"\n")    #File writing
    f.close()   

    #Take the difference set between the previous data and the current data
    set_new = set(list_new)
    set_dif = set_new - set_old
    return set_dif

def LINE_notify(set_dif,query,romaji):

    #If there is no difference set, processing ends
    if len(set_dif) == 0:
        return  

    #If there is a difference set, take it out as a list and notify LINE
    else:
        list_now = list(set_dif)
        list_now.sort()

        for L in list_now:
            def LineNotify(message):
                line_notify_token = "Acquired LINE API"
                line_notify_api = "https://notify-api.line.me/api/notify"
                payload = {"message": message}
                headers = {"Authorization": "Bearer " + line_notify_token}
                requests.post(line_notify_api, data=payload, headers=headers)
            message = query+"Appearance program information\n\n" + L
            LineNotify(message)
            sleep(2)
        return

#Notification of programs featuring Chiaki Mayumura in all genres
set_dif1=tv_information('Chiaki Mayumura','mayumura',0)
LINE_notify(set_dif1,'Chiaki Mayumura','mayumura')

#Notify the appearance programs of all genres of air stairs
set_dif2=tv_information('Air stairs','kuuki',0)
LINE_notify(set_dif2,'Air stairs','kuuki')

#Notify New York appearance programs of variety genres
set_dif3=tv_information('New York','newy',5)
LINE_notify(set_dif3,'New York','newy')

Run

I tried to run this program on my Raspberry pi every day at 9am. I wrote the following statement in the file with crontab -e.

00 09 * * * python3 /home/pi/tv_line.py

Write the job after the run time. Write the execution time in the order of minutes, hours, days, months, and days of the week.

reference

Getting information from Yahoo TV program guide (Web scraping beginner) Getting the program of Minami Hamabe by scraping [Summary of crontab commands [Linux command collection]] (https://eng-entrance.com/linux-command-crontab)

Recommended Posts

Notify LINE of TV appearance information of your favorite entertainer
Notify LINE of train operation information
Notify LINE of location information (Google Maps) with GPS multi-unit SORACOM Edition
Let's get notified of the weather in your favorite area from yahoo weather on LINE!