From 6ccf4f4a8cfba442ab7e8e01db60c666b634bc79 Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Sat, 17 Feb 2024 13:57:40 +0530 Subject: [PATCH 1/2] Add a second hidden Google API key To avoid some people with malicious intent from hitting the rate limits by using a public API key, also keep public API key available as a backup key Signed-off-by: starry-shivam --- .github/workflows/release.yml | 11 ++++++++--- app/build.gradle | 11 ++++++++++- .../main/java/com/starry/myne/repo/BookRepository.kt | 5 ++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 547e1f1c..3cf70b69 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,7 +3,7 @@ on: push: tags: - 'v*' - + jobs: Build: name: Build/Sign APK @@ -17,7 +17,12 @@ jobs: BUILD_TOOL_VERSION=$(ls /usr/local/lib/android/sdk/build-tools/ | tail -n 1) echo "BUILD_TOOL_VERSION=$BUILD_TOOL_VERSION" >> $GITHUB_ENV echo Last build tool version is: $BUILD_TOOL_VERSION - + + - name: Access GOOGLE_API_KEY + env: + GOOGLE_API_KEY: $ + run: echo GOOGLE_API_KEY=\"$GOOGLE_API_KEY\" >> ./local.properties + - name: set up JDK 17 uses: actions/setup-java@v3 with: @@ -65,7 +70,7 @@ jobs: keyPassword: ${{ secrets.KEY_PASSWORD }} env: BUILD_TOOLS_VERSION: ${{ env.BUILD_TOOL_VERSION }} - + - name: Make artifact uses: actions/upload-artifact@v2 with: diff --git a/app/build.gradle b/app/build.gradle index 3e9359bd..8f9eb991 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,6 +8,13 @@ plugins { apply plugin: 'com.mikepenz.aboutlibraries.plugin' +String getGoogleApiKey() { + def propFile = rootProject.file("./local.properties") + def properties = new Properties() + properties.load(new FileInputStream(propFile)) + return properties['GOOGLE_API_KEY'] ?: "" +} + android { namespace 'com.starry.myne' compileSdk 34 @@ -27,6 +34,8 @@ android { ksp { arg('room.schemaLocation', "$projectDir/schemas") } + + buildConfigField ("String", "GOOGLE_API_KEY", getGoogleApiKey()) } buildTypes { @@ -85,7 +94,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' implementation 'androidx.activity:activity-compose:1.8.2' implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0" - implementation "androidx.navigation:navigation-compose:2.7.6" + implementation "androidx.navigation:navigation-compose:2.7.7" // Jetpack compose. implementation "androidx.compose.ui:ui" implementation "androidx.compose.ui:ui-tooling-preview" diff --git a/app/src/main/java/com/starry/myne/repo/BookRepository.kt b/app/src/main/java/com/starry/myne/repo/BookRepository.kt index 1aa88df6..e050539a 100644 --- a/app/src/main/java/com/starry/myne/repo/BookRepository.kt +++ b/app/src/main/java/com/starry/myne/repo/BookRepository.kt @@ -17,6 +17,7 @@ package com.starry.myne.repo import com.google.gson.Gson +import com.starry.myne.BuildConfig import com.starry.myne.repo.models.BookSet import com.starry.myne.repo.models.ExtraInfo import com.starry.myne.utils.book.BookLanguage @@ -41,7 +42,9 @@ class BookRepository { private val baseApiUrl = "https://myne.pooloftears.xyz/books" private val googleBooksUrl = "https://www.googleapis.com/books/v1/volumes" - private val googleApiKey = "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" + private val googleApiKey = BuildConfig.GOOGLE_API_KEY.ifBlank { + "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" // Backup API key + } private val okHttpClient = OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) From f208d0cef5d26bf6d72b1099ffd756d084842c0c Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Sat, 17 Feb 2024 14:27:56 +0530 Subject: [PATCH 2/2] Minor fix Signed-off-by: starry-shivam --- app/build.gradle | 10 +++++++--- .../main/java/com/starry/myne/repo/BookRepository.kt | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8f9eb991..2d4f9bdf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,12 @@ apply plugin: 'com.mikepenz.aboutlibraries.plugin' String getGoogleApiKey() { def propFile = rootProject.file("./local.properties") def properties = new Properties() - properties.load(new FileInputStream(propFile)) - return properties['GOOGLE_API_KEY'] ?: "" + if (propFile.exists()) { + properties.load(new FileInputStream(propFile)) + return properties.getProperty('GOOGLE_API_KEY', "null") + } else { + return "null" + } } android { @@ -35,7 +39,7 @@ android { arg('room.schemaLocation', "$projectDir/schemas") } - buildConfigField ("String", "GOOGLE_API_KEY", getGoogleApiKey()) + buildConfigField("String", "GOOGLE_API_KEY", getGoogleApiKey()) } buildTypes { diff --git a/app/src/main/java/com/starry/myne/repo/BookRepository.kt b/app/src/main/java/com/starry/myne/repo/BookRepository.kt index e050539a..43a4bb9e 100644 --- a/app/src/main/java/com/starry/myne/repo/BookRepository.kt +++ b/app/src/main/java/com/starry/myne/repo/BookRepository.kt @@ -42,9 +42,9 @@ class BookRepository { private val baseApiUrl = "https://myne.pooloftears.xyz/books" private val googleBooksUrl = "https://www.googleapis.com/books/v1/volumes" - private val googleApiKey = BuildConfig.GOOGLE_API_KEY.ifBlank { - "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" // Backup API key - } + private val googleApiKey = + BuildConfig.GOOGLE_API_KEY ?: "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" // Backup API key + private val okHttpClient = OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS)