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.

61 lines
1.9 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 = "https://embed.pixiv.net/decorate.php?illust_id=" + pixiv_url[/\d+/]
  34. puts image_url
  35. client.web_client.chat_postMessage(
  36. channel: _data.channel,
  37. as_user: true,
  38. attachments: [
  39. {
  40. fallback: title + " - " + pixiv_url,
  41. text: "<" + ios_url + "|View Image> | <" + ios_mem_url + "|Open Profile>",
  42. title: title,
  43. title_link: pixiv_url,
  44. image_url: image_url,
  45. color: "#2684BD"
  46. }
  47. ].to_json
  48. )
  49. end
  50. end
  51. end
  52. end