Created
August 18, 2016 03:53
-
-
Save venj/72963fd399425440a773b388cc88f898 to your computer and use it in GitHub Desktop.
JLPT notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
exit(0) if File.exists?('/root/.jlpt_sent') | |
require 'net/https' | |
require 'json' | |
require 'open-uri' | |
require 'hpricot' | |
SLACK_URL = "https://hooks.slack.com/services/xxxxx" | |
class Pusher | |
def initialize | |
@uri = URI.parse(SLACK_URL) | |
@http = Net::HTTP.new(@uri.host, @uri.port) | |
@http.use_ssl = true | |
end | |
def post_json(content) | |
request = Net::HTTP::Post.new(@uri.request_uri, { 'Content-Type' => 'application/json' }) | |
request.body = { "text" => content }.to_json | |
response = @http.request(request) | |
response.body | |
end | |
end | |
pusher = Pusher.new | |
doc = Hpricot(open("https://news.etest.net.cn/JLPT/1/newslist.htm").read) | |
doc.search("//table[@class='register']//a").each do |a| | |
title = a.inner_text.strip | |
if title.include?('2016年12月') and title.include?('报名') | |
system('touch /root/.jlpt_sent') | |
pusher.post_json(title) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment