I would like to develop from the last time and create a mechanism to automatically post to Qiita.
Automatic posting to Qiita
Automatically post your own draft of Qiita on the scheduled date
I've been doing my best with mechanize until the last time, but I couldn't make a post, so I gave up and decided to use the forbidden selenium ... Please note that only .....
However, if you use selenium, you can really play brain muscles without using your head ... Because JS can be used ...
Gemfile
gem 'selenium-webdriver'
Add the above to your Gemfile
crawler.rb
require 'nokogiri'
require 'mechanize'
require 'selenium-webdriver'
def selemium_init
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
# caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => { args: ["--user-agent=#{ua}", 'window-size=1280x800', '--incognito'] }) #Secret mode
caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => {args: ["--headless","--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--user-agent=#{ua}", 'window-size=1280x800']})
client = Selenium::WebDriver::Remote::Http::Default.new
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
end
driver = selemium_init
driver.navigate.to 'https://qiita.com/login'
driver.execute_script("document.getElementsByName('identity')[0].value = 'mail address'")
driver.execute_script("document.getElementsByName('password')[0].value = 'password'")
driver.execute_script("document.getElementsByName('commit')[0].click()")
sleep 1
driver.navigate.to 'https://qiita.com/drafts'
All you have to do is log in and jump to the draft page ... Already, this is the work lol
If you add a function to actually post an article there and compare the dates ...
crawler.rb
require 'nokogiri'
require 'mechanize'
require 'selenium-webdriver'
def selemium_init
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
# caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => { args: ["--user-agent=#{ua}", 'window-size=1280x800', '--incognito'] }) #Secret mode
caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => {args: ["--headless","--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--user-agent=#{ua}", 'window-size=1280x800']})
client = Selenium::WebDriver::Remote::Http::Default.new
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
end
driver = selemium_init
driver.navigate.to 'https://qiita.com/login'
driver.execute_script("document.getElementsByName('identity')[0].value = 'mail address'")
driver.execute_script("document.getElementsByName('password')[0].value = 'password'")
driver.execute_script("document.getElementsByName('commit')[0].click()")
sleep 1
driver.navigate.to 'https://qiita.com/drafts'
doc = Nokogiri::HTML.parse(driver.page_source, nil, 'utf-8')
json = JSON.parse(doc.css('.js-react-on-rails-component')[1].inner_html)
json['creating_draft_items'].each do |item|
if item['raw_body'].match(/Reservation posting/)
date = item['raw_body'].split('Reservation posting:')[1]
#If there is a reservation posting date
if Date.today == Date.parse(date)
driver.navigate.to "https://qiita.com/drafts/#{item['item_uuid']}/edit?resume=true"
element = driver.find_element(:class, 'editorSubmit_dropdownToggle')
element.click
element = driver.find_elements(:class,'editorSubmit_dropdownItem')[2]
element.click
submit_element = driver.find_element(:class,'editorSubmit_submitBtnLabel')
submit_element.click
label_element = driver.find_element(:class,'creatingModal_label')
label_element.click
button_element = driver.find_elements(:class,'btn-primary')[-1]
button_element.click
end
end
end
That's all there is to it. If you run this batch around 20:00 every day, it will be posted automatically at 20:00 ....
Try setting up cron on the server and running it! !! !! !! (I will try the reserved post in this article lol)
→ No .. I noticed a really stupid mistake ... This article was mossed in perspective because it is also on the source with the letters attached to the reservation post ....
Reservation post: 2020-06-12