Encourage your company to join the Advent Calendar with Errbot

Preface

The season for Advent Calendar is approaching in 2016 as well. This year as well, Nijibox will use Qiita to do an advent calendar. With the theme of "my own Errbot knowledge"

I prepared a bot script that only tells the frame status of the Advent calendar on weekdays.

--Use Errbot to your liking ――Errbot, which has indescribable functions, is already running, so just create a plug-in.

Expose the code

adventcarendar.py


from datetime import datetime
from errbot import BotPlugin, botcmd, arg_botcmd, webhook
import pytz
import requests
from bs4 import BeautifulSoup
from errcron.bot import CrontabMixin
from errcron.cronjob import CronJob


class Adventcalendar(BotPlugin, CrontabMixin):
    """
    Qiita AdventCalendar
    """
    #Claim 1:You can use crontab
    CRONTAB = [
        '30 18 * * 1,2,3,4,5 .report_calendar_members',
    ]
    TIMEZONE = 'Asia/Tokyo'

    CHANNEL = '#general' #For Qiita bleaching
    CALENDAR_URL = 'http://qiita.com/advent-calendar/2016/nijibox'

    def activate(self):
        super().activate()
        self.activate_crontab()

    def report_calendar_members(self, polled_time):
        """Talk daily frame information
        """
        #Claim 2:Perth when scraping
        resp = requests.get(self.CALENDAR_URL)
        soup = BeautifulSoup(resp.content, 'html.parser')
        items = soup.select('.adventCalendarItem')
        #Claim 3:Separate the number of slots and the number of contributors
        reserved = [
            item
            for item in items
            if len(item.select('.adventCalendarItem_author')) > 0
        ]
        authors = set([
            item.select('.adventCalendarItem_author')[0].text.strip()
            for item in reserved
        ])

        if len(reserved) < 25:
            message = '{}\n{}By people{}Posts will be posted.\n Thank you for your continued participation.'.format(
                self.CALENDAR_URL,
                len(authors),
                len(reserved),
            )
            channel = self.build_identifier(self.CHANNEL)
            self.send(channel, message)

Write a little overview of the code

Claim 1: Have errcron speak on time every day like a crontab

As I wrote earlier, Errbot doesn't have features like crontab, so I am trying to do a simple Crontab-like operation using my own ʻerrcron`. for that reason,

30 18 * * 1,2,3,4,5 .report_calendar_members

The crontab notation is used as it is. This time, it's noisy to talk until the holidays, so I set it to 18:30 on weekdays, which is like a night break.

Claim 2: Quickly scraping with requests and BeautifulSoup4

This time, the information of our company's Advent calendar is not from RSS or API, but HTML is used as it is. ~~ (RSS has only public articles, API is not in the first place) ~~

For requests, you can get a response with requests.get in one shot, and since there is BeautifulSoup, you can get the number of each item in the calendar by the simple means of "counting the elements of the ʻadventCalendarItem` class". Thanks to Qiita for its beautiful structure.

Claim 3: Separate the number of slots and the number of contributors

This is not the case now, but since one member may write multiple articles, I decided to consider ** number of contributors ** separately from ** number of posts **.

About the number of posts

The contents of the .adventCalendarItem box will show the poster name frame of the .adventCalendarItem_author class only when the frame is already filled. In other words, if you take out the contents of ʻauthor from ʻItem for the time being, you can create a post list.

around here


reserved = [
    item
    for item in items
    if len(item.select('.adventCalendarItem_author')) > 0
]

Remove duplicates to get the number of contributors

Since it is troublesome to make a contributor list without duplication from the above post list directly

  1. Extract the name from the post list and make it a list
  2. Use list-> set to remove duplicate names

Prepare a list of contributors (no duplication) by means of.

around here


authors = set([
    item.select('.adventCalendarItem_author')[0].text.strip()
    for item in reserved
])

Finally throw to Slack

Now that we have a list of posts and a list of contributors, all we have to do is post to Slack based on that. Since it is a standard function of Errbot, detailed explanation is a pass this time.

スクリーンショット 2016-10-31 22.18.37.png

I hope this will make our calendar lively.

Recommended Posts

Encourage your company to join the Advent Calendar with Errbot
Throw appointments to others with the LINE WORKS calendar API
How to INNER JOIN with SQLAlchemy
Connect to VPN with your smartphone and turn off / on the server
[TensorFlow / Keras] The road to assembling an RNN with your favorite structure
Join CentOS 8.3 with SSSD to Active Directory
Run the Matrix to your boss's terminal!
To import your own module with jupyter
Get holidays with the Google Calendar API
Match the colorbar to the figure with matplotlib
The road to compiling to Python 3 with Thrift
The road to updating Splunkbase with your own Splunk app for Python v2 / v3