[PYTHON] I tried using the API of the salmon data project

Introduction

Recently, I am studying Python again. So, as part of my study, I tried to create a class that would make the public API easier to use.

The code can be found here in the Repository (https://github.com/ironball1113/sakenowa_wrapper).

What is Sakenowa Data Project?

This is a project to publish the data collected from the app Sakeno, which has the concept of "recording the sake you drank and finding your favorite sake". In other words, it is data about sake. Details will be quoted from the public page. https://muro.sakenowa.com/sakenowa-data/

What is salmon data?

This is a project to publish data and popular brand information that quantifies the flavors of the sake app Sakenowa. Anyone can use it for free within the scope of the terms of use, and can display data on the EC site, create apps and services, and perform data analysis.

Data type

At Sakenowa, we analyze user's impression comments and quantify the flavor. In addition to this data, we will publish the following data including ranking information.

--The flavors of each brand are quantified from the six perspectives of gorgeous, mellow, profound, gentle, light, and dry. --Tags of flavor details for each brand (flavor tag) --Ranking of popular stocks in salmon --Other basic information of the brand

end point

--Area list (GET / areas) --List of brands (GET / brands) --List of breweries (GET / breweries) --Ranking (GET / rankings) --Flavor chart (GET / flavor-charts) --Flavor tag list (GET / flavor-tags) --Flavor tags for each brand (GET / brand-flavor-tags)

sakenowa_wrapper I created a class because I thought it would be a little easier to call if I could use it in the pandas data frame format with just the endpoint information. It's easy to do, and the API response is finally returned to a dataframe.

sakenowa_wrapper.py


import ast
import urllib.request

import pandas as pd
from pandas.core.frame import DataFrame


class SakenowaAPI(object):
    urls = {
        "areas": "https://muro.sakenowa.com/sakenowa-data/api/areas",
        "brands": "https://muro.sakenowa.com/sakenowa-data/api/brands",
        "breweries": "https://muro.sakenowa.com/sakenowa-data/api/breweries",
        "rankings": "https://muro.sakenowa.com/sakenowa-data/api/rankings",
        "flavor-charts": "https://muro.sakenowa.com/sakenowa-data/api/flavor-charts",
        "flavor-tags": "https://muro.sakenowa.com/sakenowa-data/api/flavor-tags",
        "brand-flavor-tags": "https://muro.sakenowa.com/sakenowa-data/api/brand-flavor-tags",
    }

    def __init__(self, endpoint: str, ranking_type: str = None) -> None:
        self.endpoint = endpoint
        check_endpoint = self.endpoint in list(self.urls.keys())

        if check_endpoint:
            self.url = self.urls[self.endpoint]
            self.ranking_type = ranking_type

            print(f"\nThe current endpoint is {self.endpoint}")
            if ranking_type is not None:
                print(f"Rankings type is {ranking_type}")
            print("=" * 30)
        else:
            raise ValueError("The argument is incorrect. Please specify again.")

    def _get_body(self, url: str) -> str:
        req = urllib.request.Request(url)
        with urllib.request.urlopen(req) as res:
            body = res.read()
        return body.decode()

    def _set_key(self) -> str:
        if self.endpoint in ["areas", "brands", "breweries"]:
            return self.endpoint

        if self.endpoint == "flavor-charts":
            return "flavorCharts"

        if self.endpoint == "flavor-tags":
            return "tags"

        if self.endpoint == "brand-flavor-tags":
            return "flavorTags"

        if self.endpoint == "rankings" and self.ranking_type is not None:
            return self.ranking_type
        else:
            return "Failure"

    def set_df(self) -> DataFrame:
        body = self._get_body(self.url)
        dic = ast.literal_eval(body)
        key = self._set_key()
        return pd.DataFrame(dic[key])

Below is the trial code. Basically, only do the following:

  1. Create an instance and specify an endpoint
  2. Return a data frame

The endpoints that can be specified with the Sakenowa API are as follows. Since rankings has a comprehensive ranking of popular brands and a regional ranking, an additional argument is required to specify which one to call. ex) SakenowaAPI("rankings", "overall") or SakenowaAPI("rankings", "areas")

test_sakenowa.py



from sakenowa_wrapper import SakenowaAPI

#When there is one argument such as areas
sakenowa_areas = SakenowaAPI("areas")
print(sakenowa_areas.set_df().head())

#rankings is for overall rankings
sakenowa_rank_overall = SakenowaAPI("rankings", "overall")
print(sakenowa_rank_overall.set_df().head())

#If the argument is incorrect
# sakenowa_failue = SakenowaAPI("failue")
# print(sakenowa_failue.set_df().head())


# --------------------Output result--------------------

# The current endpoint is areas
# ==============================
#    id name
#0 1 Hokkaido
#1 2 Aomori Prefecture
#2 3 Iwate Prefecture
#3 4 Miyagi Prefecture
#4 5 Akita Prefecture

# The current endpoint is rankings
# Rankings type is overall
# ==============================
#    rank     score  brandId
# 0     1  4.412219      109
# 1     2  4.100738      792
# 2     3  4.072851      660
# 3     4  4.072180     1033
# 4     5  4.065659       19

# ValueError:The argument is incorrect. Please specify again.

Also, the trial code for drawing flavor charts with matplotlib is below. For the radar chart creation part, I referred to this article. Draw a radar chart with Matplotlib (16 lines)

test_radar_chart.py


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sakenowa_wrapper import SakenowaAPI


sakenowa_flavor_charts = SakenowaAPI("flavor-charts")
dic = sakenowa_flavor_charts.make_dic()
df_copy = sakenowa_flavor_charts.set_df(dic).copy()

col_rename = {
    "f1": "Fruity",
    "f2": "Mellow",
    "f3": "Heavy",
    "f4": "Mild",
    "f5": "Dry",
    "f6": "Light",
}

brand_id = 2
renamed_cols = list(df_copy.rename(columns=col_rename).columns)[1:]
flavor_values = df_copy.query("brandId == @brand_id").values.tolist()[0][1:]

def plot_polar(labels, values, imgname):
    angles = np.linspace(0, 2 * np.pi, len(labels) + 1, endpoint=True)
    values = np.concatenate((values, [values[0]]))
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)
    ax.plot(angles, values, "o-")
    ax.fill(angles, values, alpha=0.25)
    ax.set_thetagrids(angles[:-1] * 180 / np.pi, labels)
    fig.savefig(imgname)
    plt.close(fig)

plot_polar(renamed_cols, flavor_values, "flavor.png ")

flavor.png

Reference URL

Sakenowa Data Project [Static type checking that starts loosely in Python](https://qiita.com/ocknamo/items/6341d0a7757c668782c8#%E3%81%8A%E3%81%BE%E3%81%91stub%E3%82%92% E8% 87% AA% E5% 8B% 95% E7% 94% 9F% E6% 88% 90% E3% 81% 99% E3% 82% 8B) Draw a radar chart with Matplotlib (16 lines)

Recommended Posts

I tried using the API of the salmon data project
[Python] I tried collecting data using the API of wikipedia
I tried using the checkio API
I tried using YOUTUBE Data API V3
I tried using the BigQuery Storage API
I tried using the Google Cloud Vision API
I tried using the image filter of OpenCV
I tried clustering ECG data using the K-Shape method
I tried the Naro novel API 2
I tried using GrabCut of OpenCV
I tried the Naruro novel API
I tried to search videos using Youtube Data API (beginner)
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried face recognition of the laughter problem using Keras.
[For beginners] I tried using the Tensorflow Object Detection API
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I tried the asynchronous server of Django 3.0
I tried using Twitter api and Line api
I tried to get the index of the list using the enumerate function
I tried using the COTOHA API (there is code on GitHub)
I looked at the meta information of BigQuery & tried using it
I tried using UnityCloudBuild API from Python
I tried to touch the COTOHA API
The Python project template I think of.
[Python] I tried to get various information using YouTube Data API!
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
Awareness of using Aurora Severless Data API
I tried using PDF data of online medical care based on the spread of the new coronavirus infection
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried logistic regression analysis for the first time using Titanic data
I tried to get the batting results of Hachinai using image processing
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried to get the authentication code of Qiita API with Python.
I tried to summarize various sentences using the automatic summarization API "summpy"
I tried using the trained model VGG16 of the deep learning library Keras
I tried to get the movie information of TMDb API with Python
I tried to perform a cluster analysis of customers using purchasing data
[Kaggle] I tried feature engineering of multidimensional time series data using tsfresh.
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried the common story of predicting the Nikkei 225 using deep learning (backtest)
I tried using AWS Rekognition's Detect Labels API
I tried using scrapy for the first time
I tried the pivot table function of pandas
I checked the library for using the Gracenote API
I tried using Remote API on GAE / J
I tried cluster analysis of the weather map
vprof --I tried using the profiler for Python
I tried to save the data with discord
I tried using PyCaret at the fastest speed
I tried to correct the keystone of the image
I tried using the Datetime module by Python
I tried DBM with Pylearn 2 using artificial data
I tried using the functional programming library toolz
Check the status of your data using pandas_profiling
Scraping the winning data of Numbers using Docker
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried using argparse
I tried using anytree
I tried using aiomysql