Create a mechanism to post reservations to Qiita [Reservation posting] ~ Coding 10 lines a day ~ ~

Day 9

I would like to develop from the last time and create a mechanism to automatically post to Qiita.

[Technical theme]

Automatic posting to Qiita

language

Target deliverable

Automatically post your own draft of Qiita on the scheduled date

Code and documentation

Frustration

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 .....

Make up to the last time with selenium at explosive speed

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

Implemented from date comparison to reserved posting

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

Recommended Posts

Create a mechanism to post reservations to Qiita [Reservation posting] ~ Coding 10 lines a day ~ ~
Create a mechanism for posting reservations to Qiita [Draft acquisition] ~ Coding 10 lines a day ~ ~
Coding 10 lines a day ~ Scraping implementation ~
Coding 10 lines a day ~ Scraping environment construction ~
post to Qiita patching version
post to Qiita patching version
How to create a method
I made a gem to post the text of org-mode to qiita
I want to graph the number of photo AC downloads [MySQL ring cooperation] ~ Coding 10 lines a day ~