Skip to content

Instantly share code, notes, and snippets.

@retasretas
Created February 12, 2017 11:16
Show Gist options
  • Save retasretas/df828d8a6b3610b8ed3a1fc8786b485e to your computer and use it in GitHub Desktop.
Save retasretas/df828d8a6b3610b8ed3a1fc8786b485e to your computer and use it in GitHub Desktop.
SlackにDLsiteとDMM同人の販売本数をスクレイピングして投稿するスクリプト
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import scraperwiki
import requests
import re
from pyquery import PyQuery as pq
# 3rd party library
try:
from urllib.parse import urljoin
from urllib.parse import urlencode
import urllib.request as urlrequest
except ImportError:
from urlparse import urljoin
from urllib import urlencode
import urllib2 as urlrequest
import json
class Slack():
def __init__(self, url=""):
self.url = url
self.opener = urlrequest.build_opener(urlrequest.HTTPHandler())
def notify(self, **kwargs):
"""
Send message to slack API
"""
return self.send(kwargs)
def send(self, payload):
"""
Send payload to slack API
"""
payload_json = json.dumps(payload)
data = urlencode({"payload": payload_json})
req = urlrequest.Request(self.url)
response = self.opener.open(req, data.encode('utf-8')).read()
return response.decode('utf-8')
html = requests.get("http://www.dlsite.com/maniax/new")
html2 = requests.get("http://www.dmm.co.jp/dc/doujin/-/schedule/")
post_text = ""
d = pq(html.content)
for i in d('#__workbox #box1 h3 span').items():
print i.text()
pattern = ur"((.*)本)"
matchOB = re.search(pattern , i.text())
post_text = u"【本日の販売数】\n" + u"DLsiteは" + matchOB.group(1) + u"本"
# メーターでわかりやすく
for ii in range(0, int(matchOB.group(1))):
post_text += u"|"
# 1回でOK
break
d = pq(html2.content)
for i in d('#doujinList .pager-txt').items():
print i.text()
pattern = ur"全(.*)タイトル"
matchOB = re.search(pattern , i.text())
post_text += u"\n" + u"DMMは" + matchOB.group(1) + u"本"
# メーターでわかりやすく
for ii in range(0, int(matchOB.group(1))):
post_text += u"|"
# 1回でOK
break
# 投稿
slack = Slack(url = "https://hooks.slack.com/services/hogehogehoge/") #ここにWeb hook URL
slack.notify(text = post_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment