Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LINEで馬券を購入する / Purchase a betting ticket at LINE

73e80d16abc7cc65e08d430c2eae811b?s=47 taminif
August 03, 2018

LINEで馬券を購入する / Purchase a betting ticket at LINE

関西Node学園3時限目

73e80d16abc7cc65e08d430c2eae811b?s=128

taminif

August 03, 2018
Tweet

Transcript

  1. LINEͰഅ݊Λߪೖ͢Δ ؔ੢NodeֶԂ #3 2018/08/03 େౡ ޫو@sbntaminif

  2. ࣗݾ঺հ • ΢ΣϒϦΦגࣜձࣾWEBΤϯδχΞ + ϓϩμΫτ Ϛωʔδϟʔɹ࠷ۙ͸ϚωʔδϟʔଟΊ • ҰԠओ࠵΍ͬͯ·͕͢ࢲͩͬͯ஻Γ͍ͨ • ීஈ͸ژ౎Ͱಇ͍͍ͯ·͢

    • ࠓ೔͜Εͷ࿩Ұ੾͠·ͤΜ↘
  3. Q. അ݊͸ Ͳ͏΍ͬͯ ߪೖ͢Δʁ A. ωοτߪೖ αʔϏεΛ ࢖༻͠·͢

  4. അ݊ωοτߪೖαʔϏε • JRA I-PATͱݴ͍·͢ɻ • ଈPAT, A-PAT΋͋Γ·͢ • ਃ͠ࠐΈ੍ͷձһઐ༻αΠτ •

    Webݶఆ • ΋ͪΖΜ20ࡀҎ্ʢֶੜͰ΋OKʣ
  5. ϑΝϛίϯ + ి࿩ઢͰ΋ ߪೖͰ͖ͨ ※ݱࡏ͸αʔϏεऴྃɻεʔϑΝϛɺυϦʔϜΩϟετͰ΋ങ͑·ͨ͠ https://www.youtube.com/watch?v=ks0JeyhZsR4

  6. LINEͰഅ݊ΛങͬͯΈ͍ͨ εϚʔτϑΥϯશ੝ظ ࣗ෼ͷ޷͖ͳUIͰ അ݊Λങ͍͍ͨ

  7. ͍ͣΕ௕೥ͷເ അ݊Λߪೖ͢ΔΞϓϦΛ ࡞Γ͍ͨ ※ͨͩ͠API͸ఏڙ͞Εͳ͍

  8. API͕ͳ͍ͳΒ ࡞Ε͹ྑ͍

  9. I-PAT https://www.ipat.jra.go.jp/

  10. ΦϯϥΠϯߪೖ͸Ͱ͖Δ WebαΠτΛ AutomationԽ࣮ͯ͠ݱ Ͳ͏΍Δ͔➡

  11. Headless Chrome https://developers.google.com/web/updates/2017/04/headless-chrome?hl=en

  12. Headless Chromeͱ͸ • ChromeΛϔουϨεͰಈ࡞͢Δ • ϔουϨε = UIͳ͠Ͱ࣮ߦ • Chrome59͕Πϯετʔϧ͞Ε͍ͯΕ͹࢖༻Մ

    ೳʢWindows͸61ʣ • Chromeͷػೳ͕શͯ࢖͑Δ
  13. ίϚϯυ͔Β ChromeΛUIͳ͠Ͱ ࣮ߦͰ͖Δ

  14. Ͱ΋CLIͰ શͯͷૢ࡞Λॻ͘ͷ͸ਏ͍

  15. ChromeΛ ૢ࡞͢ΔͨΊʹ։ൃ͞Εͨ ϥΠϒϥϦ

  16. puppeteer https://github.com/GoogleChrome/puppeteer

  17. puppeteerͱ͸ • NodeJSͰHeadless ChromeΛ
 ૢ࡞͢ΔϥΠϒϥϦ • Chrome DevToolsͷ্͔Βૢ࡞͢ΔAPI • GoogleChromeνʔϜ͕࣮૷

    • ʮύϖςΟΞʯͱൃԻ
  18. ಈ࡞ϨΠϠʔ

  19. ChromeΛࣗಈͰಈ͔ͯ͠ അ݊ߪೖ·Ͱ ͨͲΓͭ͘

  20. ࣮૷͢Δ const puppeteer = require(‘puppeteer'); (async () => { const

    browser = await puppeteer.launch({headless: true}); const page = await browser.newPage(); await page.setViewport({width: 640, height: 460}); await page.goto(‘https://www.ipat.jra.go.jp/’); //////////////////////////////////////////// })(); $ISPNFΛىಈ ৽͍͠λϒΛ։͘ ΢Οϯυ΢αΠζΛܾఆ 63-Λࢦఆͯ͠ભҠ
  21. ࣮૷͢Δ

  22. ࣮૷͢Δ //////////////////////////////////////////// await page.type(‘[name=“inetid”]’,’********’); await page.click(".button a"); await page.waitForNavigation(); ////////////////////////////////////////////

    *%Λೖྗ ϩάΠϯϘλϯΛΫϦοΫ ભҠΛ଴ͭ
  23. ࣮૷͢Δ

  24. ͜Μͳײ͡Ͱ Ұͭͣͭૢ࡞ͯ͠ ໨త·Ͱୡ੒͢Δ

  25. ࡞ͬͨ΋ͷ͕ͪ͜Β https://github.com/taminif/bettingInTheIPAT

  26. await஍ࠈ

  27. Ͳ͏ͯ͠΋ϦΫΤετΛ ଴ͭඞཁ͕͋ΔͷͰ ݁ՌΛ଴ͨ͟ΔΛಘͳ͍

  28. ίϚϯυΛୟ͚͹ അ͕݊ങ͑ΔAPI͕Ͱ͖ͨ

  29. API͕Ͱ͖ͨ .BD؀ڥͰ ಈ࡞͢Δ"1* ίϚϯυݺͼग़͠ ͜Ε͚ͩͰ͸͜ͷMacͰ͔͠࢖͑ͳ͍

  30. ͜ͷAPIΛαʔόʔͰ ݺͼग़ͤΔΑ͏ʹ͍ͨ͠

  31. APIΛͲ͜Ͱಈ͔͔͢ • Headless Chrome͸Ͳͷ؀ڥͰ΋ಈ͘ • puppeteer͸Node v6.4.0͔Βಈ͘ • ͭ·Γαʔόʔߏஙͯ͠؀ڥΛ༻ҙ͢Ε͹ಈ͘
 ->

    Ͱ΋ΊΜͲ͍͘͞ • AWS Lambda΍GCPͰ΋ಈ͘ ->AWS LambdaͰಈ͔ͯ͠APIʹ͢Δ
  32. LambdaͰಈ͔ͨ͠ -BNCEBͰ ಈ࡞͢Δ"1* -BNCEBݺͼग़͠ +40/ \ QMBDFbവؗ  SBDF/Pb 

    SBDFS/P``  BNPVOU  ^ ʢิ଍ʣ "84-BNCEBͷ࠷େλΠϜΞ΢τઃఆ͸ ඵ෼ ભҠ͕ଟ͗͢Δͱ͕࣌ؒ଍Γͳ͍ Մೳੑ͸͋Δ
  33. ·ͩAWSͷ ؅ཧը໘͔Β͔͠࢖͑ͳ͍ ͜ͷAPIΛͲ͔͜ΒͰ΋ ݺͼग़ͤΔΑ͏ʹ͍ͨ͠

  34. API Gateway͔Βݺͼग़͢ -BNCEBͰ ಈ࡞͢Δ"1* )5513FRVFTUΛ ड͚෇͚Δ )5514௨৴ +40/ \ QMBDFbവؗ

     SBDF/Pb  SBDFS/P``  BNPVOU  ^
  35. ͔͠͠ɺ্ख͍͜ͱ ಈ͍ͯ͘Εͳ͍ API GatewayͷλΠϜΞ΢τʹ Ҿ͔͔ͬΔ

  36. API GatewayͷλΠϜΞ΢τ͸ ໿2෼

  37. Lambda͔ΒLambdaΛίʔϧ ඇಉظͰݺͼग़͢ )5514௨৴ +40/ \ QMBDFbവؗ  SBDF/Pb  SBDFS/P``

     BNPVOU  ^ ݺͼग़ͨ͠Β ऴΘΓ
  38. Ͳ͔͜ΒͰ΋APIΛ ݺͼग़ͤΔΑ͏ʹͳͬͨ

  39. ωοτϫʔΫΛܦ༝͢Ε͹ അ݊Λങ͑ΔΑ͏ʹͳͬͨ

  40. ωοτϫʔΫؒͰ LINE͕APIΛݺͼग़ͤ͹ അ͕݊ങ͑ΔͷͰ͸

  41. LINE Messaging API https://developers.line.me/ja/services/messaging-api/

  42. LINE Messaging APIͱ͸ • LINE DeveloperͷҰػೳ • ʮ͋ͳͨͷαʔϏεͱLINEϢʔβʔͷ
 ɹ૒ํ޲ίϛϡχέʔγϣϯΛՄೳʹ͢Δʯ •

    LINEͷ༑ୡ௥Ճ΍ϝοηʔδૹ৴ͳͲͷΠϕϯτ ΛτϦΨʔʹWebhookΛ࣮ߦ͢Δ
  43. LINEʹૹ৴ͨ͠஋Λ API Gateway͔ΒLambdaʹ ౉͢

  44. LINE BotͰഅ݊Λߪೖ͢Δ ʢਖ਼֬ʹ͸͜͜ʹ-*/&͞Μͷ αʔόʔ͕͋Δʣ ϝοηʔδ  വؗ   

    -*/&ʹೖྗ͞Εͨ ৘ใΛड෇ -BNCEB༻ʹՃ޻ +40/ \ QMBDFbവؗ  SBDF/Pb  SBDFS/P``  BNPVOU  ^
  45. ങ͑ΔΑ͏ʹ͸ͳ͕ͬͨ Ϩεϙϯε͕ͳ͍ LINE Messaging APIͰ ϦΫΤετΛड͚ͨޙʹ Ԡ౴ϝοηʔδΛฦ͢

  46. Ԡ౴ϝοηʔδػೳ • Ϣʔβʔ͔ΒͷϝοηʔδҰ݅ʹରͯ͠
 Ұճ͚ͩฦ٫Ͱ͖Δ • ϝοηʔδʹ͍͍ͭͯΔϢʔβʔIDΛ࢖ͬͯ
 Ԡ౴ϝοηʔδΛૹ৴ • ࣗ༝ʹϢʔβʔʹૹ৴͢ΔPush API͸༗ྉɾɾ


    ˢ͜Ε͕͋Ε͹ߪೖ൑ఆ͕ฦͤΔɾɾ
  47. Ԡ౴ϝοηʔδΛฦ͢ \ QMBDFbവؗ  SBDF/Pb  SBDFS/P``  BNPVOU 

    ^ വؗ    ύϥϝʔλʔ͕ਖ਼ৗͳΒ -BNCEBΛݺͼग़ͯ͠ Ԡ౴ϝοηʔδΛૹΔ -BNCEB͔Β-BNCEBΛඇಉظͰ ݺͼग़͍ͯ͠ΔͷͰ੒ޭ͔ͨ͠͸ෆ໌
  48. LINEͰഅ݊Λ ങ͑ΔΑ͏ʹͳͬͨ!

  49. ࣮ફฤ puppeteerͰ ͭ·͍ͮͨ͜ͱ

  50. puppeteerͰͭ·͍ͮͨ͜ͱ • ൣғ֎ͷཁૉΫϦοΫ • onclickΠϕϯτ͕ൃՐ͠ͳ͍ • waitForͱwaitForNavigationͱwaitForSelector • ChromeͱpuppeteerͷόʔδϣϯΛ߹ΘͤΔ •

    (AWS Lambda͸ͱͯ΋ॲཧ͕஗͍)
  51. ൣғ֎ͷཁૉΫϦοΫ

  52. Headless ChromeͰ͸ ը໘֎ͷཁૉͷΫϦοΫ͕ൃՐ͠ͳ͍ ΢Οϯυ΢αΠζΛ ͜͜·Ͱʹઃఆ ͜ͷϘλϯ͸ ΫϦοΫͰ͖ͳ͍ ϒϥ΢β෯

  53. const puppeteer = require(‘puppeteer'); (async () => { const browser

    = await puppeteer.launch({headless: true}); const page = await browser.newPage(); await page.setViewport({width: 1920, height: 2160}); await page.goto(‘https://www.ipat.jra.go.jp/’); })(); ը໘શ͕ͯ࿮಺ʹ͓͞·ΔΑ͏ͳେ͖͞ ʢղܾࡦʣ ը໘Λେ͖͘͢Δ
  54. ͜͏͢Ε͹ԡͤΔΑ͏ʹͳͬͨʂ ϒϥ΢β෯ ۭന

  55. waitForͱ waitForNavigationͱ waitForSelector

  56. ͦΕͧΕͷ࢖͍෼͚ • waitFor
 -> ϛϦඵΛࢦఆɻࢦఆ͚ͨ࣌ؒͩ͠଴ͭ • waitForNavigation
 -> Ҿ਺ͳ͠ɻ •

    waitForSelector
 -> ηϨΫλͰࢦఆɻཁૉ͕දࣔ͞ΕΔ·Ͱ଴ͭ ->͜ΕΒ͚ͩͰ͸ͳ͍ɻଞʹ΋·ͩ͋Δ
  57. ࠷ॳͷ͏ͪ͸waitForͰ ϩʔυΛ଴ͭ ׳Εͯ͘Ε͹ ਖ਼͘͠࢖͍෼͚Δͱ ਐΊ΍͍͢

  58. ·ͩHeadless Chromeͷ ັྗΛ஻ͬͯͳ͍

  59. Ξϐʔϧฤ Headless Chrome͸ ૉ੖Β͍͠ϓϩμΫτͰ͋Δʂ

  60. ·ͣ͸͡Ίʹ Headless ChromeΛ࿩্͢Ͱ ͔ܽͤͳ͍ಈը

  61. The power of Headless Chrome https://www.youtube.com/watch?v=lhZOFUY1weo 2018೥ͷGoogle IOͷߨԋ

  62. Headless Chromeͱ puppeteerͰग़དྷΔ 10ͷ͜ͱ͕࿩͞Ε͍ͯ·͢ ʢൈਮʣ SSR ABςετ Web Audio ύοΫϚϯ

  63. Headless Chrome͸ େ͖ͳՄೳੑΛൿΊ͍ͯΔ

  64. ࢲͷߟ͑Δ ࠷΋ັྗతͳٕज़

  65. ͲΜͳWebαʔϏεͰ΋ APIԽͰ͖Δ

  66. Πϯλʔωοτશ੝ظ ϒϥ΢βΛ࢖Θͳ͍ਓ͸ ΄΅͍ͳ͍Ͱ͢ΑͶ

  67. ීஈͷωοταʔϑΟϯΛ ࣗಈԽͰ͖Δ

  68. ࣗಈԽͯ͠APIʹ͢Ε͹ ͍ͭͰ΋ݺͼग़ͤΔ

  69. APIԽ͓͚ͯ͠͹ ΠϯλʔϑΣʔε͸ ͲΕͰ΋࢖͑Δ ࠓ࿩୊ͷεϚʔτεϐʔΧʔ Ͱ΋OK

  70. ԿͰ΋Ͱ͖Δ

  71. ເ͕޿͕Γ·ͤΜ͔ʁ

  72. ⚠8BSOJOH

  73. Ԭ࡚ࢢཱதԝਤॻؗࣄ݅ https://ja.wikipedia.org/wiki/Ԭ࡚ࢢཱதԝਤॻؗࣄ݅

  74. ΍Γ͗͢Δͱॻྨૹݕ·Ͱ ͋Γ·͢ ৗࣝతͳΞΫηεʹ ԡ͑͞·͠ΐ͏

  75. ͜ͷηογϣϯʹײԽ͞Εͯ ൃੜͨ͠ଛ֐౳ʹ͍ͭͯɺ ੹೚͸ෛ͍݉Ͷ·͢

  76. ༻๏༻ྔΛकͬͯ ศརʹ࢖͍·͠ΐ͏

  77. ͱ͜ΖͰɺࠓ·Ͱͷ εΫϨΠϐϯάπʔϧͰ͸ Ͱ͖ͳ͔ͬͨͷʁ

  78. Ͱ͖ͳ͍͜ͱ͸ͳ͍͚ΕͲ ೉͔ͬͨ͠ • ֤ݴޠͷHTTPϦΫΤετͰ͸ϩάΠϯ৘ใΛ
 อ࣋͢Δ͜ͱ͕೉͔ͬͨ͠ • PhantomJS͸webkitϕʔεͰɺSPA΍৽͍͠λϒ Λ։͘Α͏ͳαʔϏεͷରԠ͕Ͱ͖ͳ͔ͬͨ
 (Headless Chrome͕ग़ͨ͜ͱͰαʔϏεऴྃ)

    • Selenium͸৮ͬͯͳ͍͚ͲɺAPIԽ͸೉͍͠ʁ
  79. Headless Chrome͸ ChromeΛ࢖͍ͬͯΔ Α͏ͳ΋ͷͳͷͰ ΄ͱΜͲͷ͜ͱΛָʹରԠͰ͖Δ

  80. ࠷ߴ͡Όͳ͍Ͱ͔͢ʁ

  81. ͜ͷಈըͷதͰ ڧ͘࿩͞Ε͍ͯΔ͜ͱ https://www.youtube.com/watch?v=lhZOFUY1weo

  82. ʮ͜ͷ࿩͸ςετࣗಈԽͷ ࿩Ͱ͸͋Γ·ͤΜʯ ʮࣗಈԽ͢Δ͜ͱ͕Ͱ͖Δʯ ʮࣗಈԽ͢ΔπʔϧͰ͋Δʯ

  83. ීஈͷ࡞ۀΛࣗಈԽ͢Δ ͜Εͦ͜ΤϯδχΞϦϯά

  84. ΍ͬͯΈͨ͘ͳΓ·ͤΜ͔ʁ

  85. ࣗಈԽ Έͳ͞Μͥͻ ΍ͬͯΈ·͠ΐ͏ʂ

  86. ͋Γ͕ͱ͏͍͟͝·ͨ͠