Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1

background

Over View

App overview chart

  1. Automatically download and save Tenhou's log (purple arrow in the figure)
  2. Reply aggregate information in response to user (LINE) request (green arrow in the figure)

test.png

Complete image

ui.png

Languages, tools, services, modules, etc.

Language used

Tools and services used

This time, I created an application using the following services that can be used for free.

Heroku

LINE bot (LINE Messaging API)

Dropbox API

AWS S3 storage

Module used

Get log of Tenhou private room

This time, we will get the log of Tenhou private room by scraping. I will post more and more.

Synopsis

Code example

# scrape_log.py
# coding *-utf-8-*

import os
import pprint
import time
import urllib.error
import urllib.request
import gzip
import shutil
import datetime
from datetime import date,timedelta
import download4
def download_file(url, dst_path):
    try:
        with urllib.request.urlopen(url) as web_file:
            data = web_file.read()
            with open(dst_path, mode='wb') as local_file:
                local_file.write(data)
    except urllib.error.URLError as e:
        print(e)
if __name__ == "__main__":
    dt_now = datetime.datetime.now()
    yyyymmdd = dt_now.strftime('%Y%m%d')
    # dt_1day_past = dt_now - timedelta(days=1) #Find the log 1 hour ago
    # yyyymmdd = dt_1day_past.strftime('%Y%m%d')
    print(yyyymmdd)

    fname = 'sca{}.log.gz'.format(yyyymmdd)
    URL = "https://tenhou.net/sc/raw/dat/"+fname
    
    dst_path = 'scrape.gz'
    download_file(URL, dst_path)

    with gzip.open('scrape.gz', mode='r') as f_in:
        with open('scrape.txt', 'wb') as f_out:
            shutil.copyfileobj(f_in, f_out)
    with open('scrape.txt') as f:
        lines = f.readlines()


    download4.download("/logvol1.txt","temp.txt")
    with open("temp.txt",'a') as f:
        f.write("{}\n".format(yyyymmdd))
        for line in lines:
            roomid = line.split()[0]
            if roomid == "C1234": #Private room ID
                f.write("{}".format(line)) 
                print(line)

    download4.upload("temp.txt","/logvol1.txt")       
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dropbox

class TransferData:
    def __init__(self, access_token):
        self.access_token = access_token

    def upload_file(self, file_from, file_to):
        """upload a file to Dropbox using API v2
        """
        dbx = dropbox.Dropbox(self.access_token)

        with open(file_from, 'rb') as f:
            dbx.files_upload(f.read(), file_to,mode=dropbox.files.WriteMode.overwrite)
    
    def download_file(self,file_from,file_to):
        """download a file to Dropbox using API v2
        """
        dbx = dropbox.Dropbox(self.access_token)

        with open(file_to, 'rb') as f:
            dbx.files_download_to_file(file_to, file_from)
    


def upload(file_from,file_to):
    access_token = "Hogehoge" #Access token to the application folder
    transferData = TransferData(access_token)
    # API v2
    transferData.upload_file(file_from, file_to)

def download(file_from,file_to):
    access_token = "Hogehoge" #Access token to the application folder
    transferData = TransferData(access_token)
    # API v2
    transferData.download_file(file_from, file_to)

if __name__ == '__main__':
    # upload()
    download('/logvol1.txt',"log.txt")

in conclusion

p.s.

Recommended Posts

Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 2
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part ③
Create a LINE BOT with Minette for Python
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
Create an English word app with python
Create an app that guesses students with python
Let's make an app that can search similar images with Python and Flask Part1
Let's make an app that can search similar images with Python and Flask Part2
I made a LINE BOT with Python and Heroku
[LINE Messaging API] Create parrot return BOT with Python
Create a machine learning app with ABEJA Platform + LINE Bot
Create an LCD (16x2) game with Raspberry Pi and Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a striped illusion with gamma correction for Python3 and openCV3
[LINE Messaging API] Create a BOT that connects with someone with Python
FX automatic trading system made with python and genetic algorithm Part 1
Create an Excel file with Python3
An app for smart people and smart people
Explanation of creating an application for displaying images and drawing with Python
FM modulation and demodulation with Python Part 3
Python installation and package management with pip
Quickly create an excel file with Python #python
Create and decrypt Caesar cipher with python
[Python] [LINE Bot] Create a parrot return LINE Bot
FM modulation and demodulation with Python Part 2
[Python] Quickly create an API with Flask
[LINE WORKS version Trello Bot] How to create a private talk room including a talk bot