Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

Ṣe o ni awọn iṣẹ-ṣiṣe ti o tun ọjọ lẹhin ọjọ, ọsẹ lẹhin ọsẹ? Fun apẹẹrẹ, kikọ awọn iroyin. O beere data, ṣe itupalẹ rẹ, wo inu rẹ (ṣe awọn aworan, awọn shatti), lẹhinna firanṣẹ si ọga rẹ. Ṣugbọn kini ti gbogbo eyi ba jẹ adaṣe?

Ninu ikẹkọ yii a yoo ṣẹda bot kan fun Telegram ti yoo ṣe iranlọwọ adaṣe adaṣe. Ati ohun ti o tutu julọ ni pe gbogbo eto naa yoo ni awọn laini koodu 50 nikan! Ti o ba n ṣẹda bot kan fun Telegram fun igba akọkọ, lẹhinna o yẹ ki o tun ka eyi sare.

Skillbox ṣe iṣeduro: Ilana ti o wulo Python Olùgbéejáde lati ibere.

A leti: fun gbogbo awọn oluka ti "Habr" - ẹdinwo ti 10 rubles nigbati o forukọsilẹ ni eyikeyi iṣẹ-ẹkọ Skillbox nipa lilo koodu ipolowo “Habr”.

Jẹ ká bẹrẹ

Awọn ile-ikawe fifi sori ẹrọ

A yoo lo google-awọsanma-bigquery lati gba data lati Google BigQuery. matplotlib, numpy и panda yoo ran o visualize rẹ data. Python-telegram-bot yoo firanṣẹ data ti o pari si Telegram.

pip3 fi sori ẹrọ google-cloud-bigquery matplotlib numpy pandas python-telegram-bot

Nsopọ Google BigQuery API

Ti a ba fẹ lo iṣẹ naa, a nilo lati so Google BigQuery API pọ. Lati ṣe eyi a lọ si Awọn itọnisọna Nẹtiwọki Google ati ṣẹda iṣẹ akanṣe tuntun (tabi yan eyi ti o wa tẹlẹ).

Ninu igbimọ iṣakoso, yan MU APIS ATI Awọn iṣẹ ṣiṣẹ ki o wa BigQuery API.

Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

Yan Muu ṣiṣẹ lati so API pọ.

Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

Ṣẹda bọtini akọọlẹ kan

Jẹ ki a lọ lẹẹkansi Awọn itọnisọna Nẹtiwọki Google, yan taabu Awọn iwe-ẹri, Ṣẹda awọn iwe-ẹri ati bọtini akọọlẹ Iṣẹ.

Lẹhinna - Iroyin iṣẹ titun, ki o si tẹ orukọ sii ni aaye orukọ akọọlẹ Iṣẹ.

Lati akojọ aṣayan-isalẹ, yan Ise agbese> Olohun, lẹhinna Ṣẹda.

Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

Faili ti yoo gba lati ayelujara laifọwọyi ni a npe ni creds.json.

Ṣeto GOOGLE_APPLICATION_CREDENTIALS, ni pato ọna lati lọ si creds.json ni ebute naa.

okeere GOOGLE_APPLICATION_CREDENTIALS='[PATH_TO_CREDS.JSON]'

Ti ohun gbogbo ba lọ daradara, o to akoko lati bẹrẹ kikọ eto naa.

Ṣiṣẹda ohun elo

Fun ikẹkọ a yoo lo data lati bigquery-public-data.stackoverflow, fun ijabọ wa a yoo yan nọmba awọn atẹjade ojoojumọ.

Ohun gbogbo ni o rọrun to.

Beere tabili naa -> Foju inu wo data -> Fipamọ iworan naa -> Firanṣẹ aworan naa

Jẹ ki a ṣẹda iṣẹ kan lati ṣalaye okun kọọkan.

Ibeere si BigQuery

Ni akọkọ a gbe ile-ikawe wọle.

lati google.cloud agbewọle bigquery

A ṣẹda iṣẹ kan ti a pe ni query_to_bigquery, nibiti paramita naa jẹ ibeere.

def query_to_bigquery(query):
    client = bigquery.Client()
    query_job = client.query(query)
    result = query_job.result()
    dataframe = result.to_dataframe()
    return dataframe

Iṣẹ yii yoo da ibeere pada bi fireemu data kan.

Data wiwo

Lati yanju isoro yi, yan matplotlib.

gbe wọle matplotlib.pyplot bi plt

A nilo paramita marun, nibiti x jẹ data x-axis, x_label jẹ akọle fun axis, y ni data y-axis, y_label jẹ akọle fun ipo, ati akọle jẹ akọle gbogbo iworan.

def visualize_bar_chart(x, x_label, y, y_label, title):
    plt.title(title)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    index = np.arange(len(x))
    plt.xticks(index, x, fontsize=5, rotation=30)
    plt.bar(index, y)
    return plt

Fi aworan pamọ

Bayi jẹ ki a lo awọn iṣẹ meji lati ṣẹda iworan kan ati fipamọ.

A yoo firanṣẹ nọmba awọn ifiweranṣẹ ti a tẹjade lojoojumọ. Ni akọkọ a kọ ibeere kan.

query = """
        SELECT DATE(creation_date) date, COUNT(*) total_posts
        FROM `bigquery-public-data.stackoverflow.post_history`
        GROUP BY 1
        HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)
        ORDER BY 1
        """

Ibeere ṣe iranlọwọ lati gba data fun ọsẹ meji ti o bẹrẹ ni Oṣu kejila ọjọ 2, ọdun 2018.

A lo ọjọ yii nitori 2018-12-02 jẹ data tuntun ti o gbasilẹ ni bigquery-public-data.stackoverflow.post_history, ni awọn igba miiran o le lo CURRENT_DATE() lati gba data tuntun.

Pe iṣẹ ibeere_to_bigquery lati gba data naa.

dataframe = query_to_bigquery(ibeere)

Lẹhinna a lo iwe data ọjọ fun ipo-x, ati iwe lapapọ_posts fun y-axis.

x = dataframe['ọjọ'].tolist()
y = fireemu data ['total_posts'].tolist()

A wo oju rẹ nipa lilo iṣẹ visualize_bar_chart ati fipamọ bi aworan.

plt = visualize_bar_chart (x=x, x_label='Ọjọ́', y=y, y_label='Apapọ Awọn ifiweranṣẹ', akọle='Awọn ifiweranṣẹ Ojoojumọ')
plt.savefig('viz.png')

A fi ipari si koodu yii ni iṣẹ kan ti a pe ni get_and_save_image.

def get_and_save_image():
    query = """
            SELECT DATE(creation_date) date, COUNT(*) total_posts
            FROM `bigquery-public-data.stackoverflow.post_history`
            GROUP BY 1
            HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)
            ORDER BY 1
            """
    dataframe = query_to_bigquery(query)  
    x = dataframe['date'].tolist()
    y = dataframe['total_posts'].tolist()
    plt = visualize_bar_chart(x=x, x_label='Date', y=y, y_label='Total Posts', title='Daily Posts')
    plt.savefig('viz.png')

Fi aworan ranṣẹ

Lati le fi ijabọ ranṣẹ si olugba, o nilo lati mọ paramita chat_id.

A nlo userinfobot ki o si tẹ / bẹrẹ. Bot naa dahun pẹlu alaye pataki, chat_id wa ninu aaye id.

Bayi jẹ ki a ṣẹda iṣẹ send_image. Yoo lo iṣẹ get_and_save_image lati gba pada ati fi aworan naa pamọ. Ati lẹhinna a firanṣẹ ohun gbogbo si olubasọrọ to tọ.

def send_image(bot, update):
    get_and_save_image()
    chat_id = 'CHAT_ID_RECEIVER'
    bot.send_photo(chat_id=chat_id, photo=open('viz.png','rb'))

Eto akọkọ

Ni ipari, a ṣẹda iṣẹ miiran, akọkọ, lati ṣe ifilọlẹ ohun elo naa. Maṣe gbagbe lati yi YOU_TOKEN pada fun bot.

Ranti: eto yii yoo fi aworan ranṣẹ laifọwọyi ni akoko ti o pato. Fun apẹẹrẹ, a yoo fi iroyin ranṣẹ ni aago mẹsan owurọ ni gbogbo ọjọ.

def main():
    updater = Updater('YOUR_TOKEN')
    updater.job_queue.run_daily(send_image, time=datetime.datetime.strptime('9:00AM', '%I:%M%p').time(), days=(0,1,2,3,4,5,6))
    updater.start_polling()
    updater.idle()
 
if __name__ == '__main__':
    main()

Bi abajade, ohun elo wa yoo dabi eyi:

from google.cloud import bigquery
from telegram.ext import Updater
 
import matplotlib.pyplot as plt
import numpy as np
import datetime
 
def query_to_bigquery(query):
    client = bigquery.Client()
    query_job = client.query(query)
    result = query_job.result()
    dataframe = result.to_dataframe()
    return dataframe
 
def visualize_bar_chart(x, x_label, y, y_label, title):
    plt.title(title)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    index = np.arange(len(x))
    plt.xticks(index, x, fontsize=5, rotation=30)
    plt.bar(index, y)
    return plt
 
def get_and_save_image():
    query = """
            SELECT DATE(creation_date) date, COUNT(*) total_posts
            FROM `bigquery-public-data.stackoverflow.post_history`
            GROUP BY 1
            HAVING date > DATE_SUB('2018-12-02', INTERVAL 14 DAY)
            ORDER BY 1
            """
    dataframe = query_to_bigquery(query)  
    x = dataframe['date'].tolist()
    y = dataframe['total_posts'].tolist()
    plt = visualize_bar_chart(x=x, x_label='Date', y=y, y_label='Total Posts', title='Daily Posts')
    plt.savefig('viz.png')
 
def send_image(bot, update):
    get_and_save_image()
    chat_id = 'CHAT_ID_RECEIVER'
    bot.send_photo(chat_id=chat_id, photo=open('viz.png', 'rb'))
 
def main():
    updater = Updater('YOUR_TOKEN')
    updater.job_queue.run_daily(send_image, time=datetime.datetime.strptime('9:00AM', '%I:%M%p').time(), days=(0,1,2,3,4,5,6))
    updater.start_polling()
    updater.idle()
 
if __name__ == '__main__':
main()

Fi faili pamọ ki o pe main.py.

A ṣe ifilọlẹ ohun elo naa nipa titẹ aṣẹ ni ebute naa:

Python3 akọkọ.py

Gbogbo rẹ ti šetan. Bayi a ni robot ti o ni awọn laini koodu 50 ti o ṣe agbejade awọn ijabọ laisi idasi wa.

Jẹ ki a ṣayẹwo bot lati ibinipa yiyan aṣẹ / firanṣẹ.

Bii o ṣe le fi awọn ijabọ irọrun ranṣẹ si roboti kan. A kọ bot ni Python ati Google BigQuery

O le gba koodu ti o pari ni GitHub mi.

Skillbox ṣe iṣeduro:

orisun: www.habr.com

Fi ọrọìwòye kun