A Slack Bot that pulls Pixiv information and posts the full image(s) into Slack, with iOS shortcuts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

8 years ago
  1. # coding: utf-8
  2. module SlackMathbot
  3. module Commands
  4. class Illust < SlackRubyBot::Commands::Base
  5. match /member_illust\.php(?<url>.*)$/ do |client, _data, _match|
  6. # Initalize Mechanize
  7. agent = Mechanize.new
  8. # Create Pixiv URL
  9. # If a full URL is given, extra characters are appended for some reason.
  10. # This ensures we catch them better
  11. if (_data[:text].include? "pixiv.net")
  12. pixiv_url = "http://www.pixiv.net/member_illust.php" + _match[:url][0..-2]
  13. else
  14. pixiv_url = "http://www.pixiv.net/member_illust.php" + _match[:url][0..-1]
  15. end
  16. pixiv_url = CGI.unescapeHTML(pixiv_url)
  17. puts pixiv_url
  18. # Create iOS Illustration URL, regex pixiv_url to
  19. # extract numbers only, such as /^[0-9]+$/
  20. #ios_url = "pixiv://illusts/" + pixiv_url.split("illust_id=")[-1]
  21. ios_url = "pixiv://illusts/" + pixiv_url[/\d+/]
  22. puts ios_url
  23. # Scrape profile link
  24. profile = agent.get(pixiv_url).at("div.userdata-row > h2.name > a").attributes['href'].to_s
  25. puts profile
  26. # Create iOS Members URL
  27. ios_mem_url = "pixiv://users/" + profile[/\d+/]
  28. puts ios_mem_url
  29. # Scrape page title
  30. title = agent.get(pixiv_url).title
  31. puts title
  32. # Scrape image
  33. image_url = agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  34. begin
  35. # Attempt to check on image
  36. page = agent.head(image_url)
  37. # Make sure page came back kosher
  38. if page.code.to_i == 200
  39. # Image url is fine
  40. puts "Image is good to go!"
  41. else
  42. # Fallback on http error to be safe
  43. image_url = "https://embed.pixiv.net/decorate.php?illust_id=" + pixiv_url[/\d+/]
  44. end
  45. rescue Mechanize::ResponseCodeError
  46. # Fallback to og:image url
  47. puts "Image failed to load"
  48. image_url = "https://embed.pixiv.net/decorate.php?illust_id=" + pixiv_url[/\d+/]
  49. end
  50. puts image_url
  51. client.web_client.chat_postMessage(
  52. channel: _data.channel,
  53. as_user: true,
  54. attachments: [
  55. {
  56. fallback: title + " - " + pixiv_url,
  57. text: "<" + ios_url + "|View Image> | <" + ios_mem_url + "|Open Profile>",
  58. title: title,
  59. title_link: pixiv_url,
  60. image_url: image_url,
  61. color: "#2684BD"
  62. }
  63. ].to_json
  64. )
  65. end
  66. end
  67. end
  68. end