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.
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)
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.
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.
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 **.
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
]
Since it is troublesome to make a contributor list without duplication from the above post list directly
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
])
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.
I hope this will make our calendar lively.
Recommended Posts