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.

69 lines
2.0 KiB

  1. # coding: utf-8
  2. module SlackMathbot
  3. module Commands
  4. class Member < SlackRubyBot::Commands::Base
  5. match /member\.php(?<url>.*)$/ do |client, _data, _match|
  6. # Initalize Mechanize
  7. agent = Mechanize.new
  8. pixiv_url = "http://www.pixiv.net/member.php" + _match[:url][0..-2]
  9. puts pixiv_url.split("?id=")[-1]
  10. # Scrape page title
  11. title = Mechanize.new.get(pixiv_url).title
  12. puts title
  13. # Scrape username
  14. username = Mechanize.new.get(pixiv_url).at(".name").text
  15. puts username
  16. # Scrape profile comment
  17. profile_comment = Mechanize.new.get(pixiv_url).at(".profile-comment").text.split("br { display : none;}")[-1]
  18. puts profile_comment
  19. # Scrape works count
  20. #works = Mechanize.new.get(pixiv_url).at(".count")
  21. works = agent.get(pixiv_url).links_with(:class => /int/)
  22. puts works
  23. # Create iOS Member URL, must find correct URL scheme
  24. ios_mem_url = "pixiv://users/" + pixiv_url.split("?id=")[-1]
  25. puts ios_mem_url
  26. client.web_client.chat_postMessage(
  27. channel: _data.channel,
  28. as_user: true,
  29. attachments: [
  30. {
  31. fallback: title + " - " + pixiv_url,
  32. text: "<" + ios_mem_url + "|Open in App>",
  33. text: profile_comment,
  34. title: username,
  35. title_link: pixiv_url,
  36. color: "#2684BD",
  37. fields: [
  38. {
  39. title: "Works",
  40. value: works[0].text,
  41. short: true,
  42. },
  43. {
  44. title: "Bookmarks",
  45. value: works[1].text,
  46. short: true,
  47. },
  48. {
  49. title: "Follow",
  50. value: works[2].text,
  51. short: true,
  52. }
  53. ]
  54. }
  55. ].to_json
  56. )
  57. end
  58. end
  59. end
  60. end