[JAVA] Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.

Overview

Purpose

Target

Such.

Premise (environment, etc.)

Source code

https://github.com/yu-suke-dev/fanza-bot

Creating a project

Plugin installation

Start the project

The project hierarchy looks like this:

[sample]
   ├── HelloWorldFunction
   │   ├── build.gradle
   │   ├── gradle
   │   │   └── wrapper
   │   │       ├── gradle-wrapper.jar
   │   │       └── gradle-wrapper.properties
   │   ├── gradlew
   │   ├── gradlew.bat
   │   └── src
   │       ├── main
   │       │   └── java
   │       │       └── helloworld
   │       │           ├── App.java
   │       │           └── GatewayResponse.java
   │       └── test
   │           └── java
   │               └── helloworld
   │                   └── AppTest.java
   ├── README.md
   ├── sample.iml
   └── template.yaml

Replace with Kotlin.

build.gradle


plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.40'
}

apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

jar {
    manifest {
        attributes('Main-Class': 'fanza.App')
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

dependencies {
    compile "com.amazonaws:aws-lambda-java-core:1.2.0"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.40"
    compile "com.squareup.okhttp3:okhttp:3.9.0"
    compile "org.json:json:20180813"
    testCompile "junit:junit:4.12"
}
[src]
  ├── main
  │   └── kotlin
  │       └── fanza
  │           └── App.java
  └── test
  │   └── java
  │       └── fanza
  │           └── AppTest.java

App.kt


package fanza

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.json.JSONObject
import java.io.IOException

/**
 * Handler for requests to Lambda function.
 */
class App : RequestHandler<Any, Any> {

    /**
     * called method
     */
    override fun handleRequest(input: Any, context: Context): Any {

        try {
            //ID required for fanza API
            var fanzaApiId = System.getenv("FANZA_API_ID")
            var affiliateId = System.getenv("FANZA_AFFILIATE_ID")

            //Create the URL you want to search
            val fanzaUrl: String = "https://api.dmm.com/affiliate/v3/ItemList"
                    .plus("?api_id=$fanzaApiId")
                    .plus("&affiliate_id=$affiliateId")
                    .plus("&site=FANZA&service=digital&floor=videoa")
                    .plus("&hits=20&sort=date&keyword=amateur&output=json")

            //GET to FANZA
            val fanzaInfo = get(fanzaUrl).replace("\\", "")

            //Regular expression of the part you want to extract
            val regex = Regex("https://www.dmm.co.jp/litevideo/-/part/=/cid=[a-z]{3,5}[0-9]{3,5}/size=720_480/")

            //Randomly extract one from the extracted results
            val result: String = regex.findAll(fanzaInfo)
                    .map { it.value }
                    .toList()
                    .random()

            //Slack webhook URL
            var slackUrl: String = System.getenv("SLACK_URL")

            //What to send to Slack
            var slackJson = JSONObject()
            slackJson.put("channel", "#fanza")
            slackJson.put("username", "fanza-bot")
            slackJson.put("text", result)
            slackJson.put("icon_emoji", ":womens:")

            //post send
            post(slackUrl, slackJson.toString())

            return "Success"

        } catch (e: IOException) {
            return RuntimeException(e)
        }
    }

    /**
     * get
     */
    @Throws(IOException::class)
    operator fun get(url: String): String {
        val request = Request.Builder()
                .url(url)
                .header("Content-Type", "application/json")
                .addHeader("X-Custom-Header", "application/json")
                .build()
        val client = OkHttpClient.Builder().build()
        val response = client.newCall(request).execute()
        return response.body()!!.string()
    }

    /**
     * post
     */
    @Throws(IOException::class)
    fun post(url: String, json: String): String {
        val body = RequestBody
                .create(MediaType.parse("application/json; charset=utf-8"), json)
        val request = Request.Builder()
                .url(url)
                .put(body)
                .build()
        val client = OkHttpClient()
        return client.newCall(request).execute().body()!!.string()
    }
}

Setting environment variables

runtime local-app env

Operation check

Execution result

Jar file creation

gradle jar

Upload to Lambda

Linking AWS IoT Buttons with AWS Lambda

Execution result

Press the * button. (Pochi)

Summary

Referenced article (Thank you)

Recommended Posts

Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.
Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.
Introduce docker to the application you are creating
Are you using the default method of the interface properly?
Are you still exhausted to implement the search function? Gem'ransack' that can be implemented in an instant
How to perform a specific process when the back button is pressed in Android Fragment
When you receive a call, send an SMS to that number