A story made by a person who has no knowledge of Python or Json

Preface

https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4 Thanks to Bot Tsuyotsuyo Man who wrote this article, I was motivated.

Also, I wanted Princess Maho to speak

Also, since it's a Japanese fucking coarse fish, I don't know what I'm saying, so please call the code and do your best to understand it () ~~ I'm programming in an atmosphere ~~

The one I made

Maho princess bot sスクリーンショット 2020-12-14 183945.png cute This time I would like to talk about the timetable function of Princess Maho's function.

Timetable function

スクリーンショット 2020-12-14 204843.png Maho Hime Timetable Tells you the timetable. The general flow of this function is "Maho Hime Today's timetable is 1, subject name 2, subject name 3, subject name 4, subject 5, subject name", and the subject name is entered in the google spreadsheet and "Maho Hime" "Tell me the timetable" is the flow of extracting data from google spreadsheet and outputting it.

1. About getting google spreadsheet api

** About api ** I think it's about getting an access token and using the features of the software.

Please refer to the following URL for obtaining googlespreadsheetapi. https://qiita.com/164kondo/items/eec4d1d8fd7648217935 (I tried to access Google Spread Sheets using Python)

2. Acquisition of subject name

Consider a description that extracts the subject name from the toot and outputs it to google spreadsheet

Bot.py


        if re.search(r'((today|Today)of(Timetable|School timetable)Is)', content):
            nagasa = len(content)
            firstclass = content[content.find('1,') + 2: content.find('2,')]
            secondclass = content[content.find('2,') + 2: content.find('3,')]
            thirdclass = content[content.find('3,') + 2: content.find('4,')]
            fourthclass = content[content.find('4,') + 2: content.find('5,')]
            fifthclass = content[content.find('5,') + 2: nagasa]

Use the find function of python to specify the location of the subject name, and put the data of the 1st period, 2nd period, ... 5th period in a variable to get the subject name.

3, fill in google spread sheet

Enter the subject name obtained in 1 in google spreadshhet

Bot.py


                    a = 3
                    while(str(sh.cell(a, 1).value) != ''):
                        if str(sh.cell(a,1).value) == username:
                            sh.update_cell(a,2,firstclass)
                            time.sleep(1)
                            sh.update_cell(a,3,secondclass)
                            time.sleep(1)
                            sh.update_cell(a,4,thirdclass)
                            time.sleep(1)
                            sh.update_cell(a,5,fourthclass)
                            time.sleep(1)
                            sh.update_cell(a,6,fifthclass)
                            time.sleep(1)
                            sh.update_cell(a,7,'ok')
                            time.sleep(1)
                            break
                        else:
                            a += 1
                    else:
                        sh.update_cell(a,1,username)
                        time.sleep(1)
                        sh.update_cell(a,2,firstclass)
                        time.sleep(1)
                        sh.update_cell(a,3,secondclass)
                        time.sleep(1)
                        sh.update_cell(a,4,thirdclass)
                        time.sleep(1)
                        sh.update_cell(a,5,fourthclass)
                        time.sleep(1)
                        sh.update_cell(a,6,fifthclass)
                        time.sleep(1)
                        sh.update_cell(a,7,'ok')
                        time.sleep(1)

Get the user's username from the toot data (this article omits how to get the username) If the username is in the spreadsheet, then if you don't enter the subject name in the cell, enter the username Later, enter the subject name in the cell. If the entry is successful, it will look like thisScreenshot 2020-12-14 222345.png

4, Output spreadsheet data to Mastodon

Output the subject data entered in the spreadsheet to Mastodon

Bot.py


                a = 3
                while(str(sh.cell(a, 1).value) != ''):
                    if  str(sh.cell(a, 1).value) == username:
                        if(sh.cell(a,7).value == 'ok'):
                            mastodon.toot('The timetable is\n1 period is'+sh.cell(a,2).value+'\n2 period is'+sh.cell(a,3).value+'\n3 period is'+sh.cell(a,4).value+'\n4th period'+sh.cell(a,5).value+'\n5 period is'+sh.cell(a,6).value+'\n yawa ~')
                            break
                        else:
                            mastodon.toot('I haven't registered the timetable ~ Have you registered?')
                            break
                    else:
                        a += 1
                else:
                    mastodon.toot('It's strange from the timetable ~ Easy to tell ~ ♪\n How to register the timetable is "Princess Maho Today's timetable is 1,subject name,2,subject name3,subject name4,subject name5,subject name」にして空きコマなら無しって書いておくれやす~')

Similar to 3, it is a description that identifies whether the username matches and if there is no username, prompts you to register, and if the username matches and the timetable is registered, the data is acquired and mastodon is used. スクリーンショット 2020-12-14 204843.png You did it, Princess Maho tooted the timetable! !!

Finally

It is a story that people who do not know python or json at all started making bots and have become able to do so far. Yes. If you want to make a bot! !! If there is a person who says, you can lay the foundation of Mastodon Bot by looking at the article of a certain Bot Tsuyotsuyo Man of C3. https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4

References

[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】 https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4 I tried to access Google Spread Sheets using Python https://qiita.com/164kondo/items/eec4d1d8fd7648217935

Recommended Posts

A story made by a person who has no knowledge of Python or Json
A story about a Python beginner who was about to be crushed by ModuleNotFoundError: No module named'tweepy'
Story of power approximation by Python
Impressions of touching Dash, a data visualization tool made by python
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
Connect a lot of Python or and and
A story of a person who started aiming for data scientist from a beginner
The story of blackjack A processing (python)
A textbook for beginners made by Python beginners
A memorandum of extraction by python bs4 request
A story when a Python user passes a JSON file
Add a function to tell the weather of today to slack bot (made by python)
Story that an inexperienced person made a masked solver
Group by consecutive elements of a list in Python
A python regular expression, or a memo of a match object
AttributeError: The story of solving module'tensorflow' has no attribute'log'.
__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python
Basic knowledge of Python
[Python] How to make a list of character strings character by character
[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans
A story about a python beginner stuck with No module named'http.server'
An app made by a newcomer who loves music games by learning python for only one day
[Python] A program that compares each element of list one by one and wins or loses. zip ()
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.