Skip to content

Commit

Permalink
Android bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-nyt committed Mar 12, 2024
1 parent b60aacb commit 1678187
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 4 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ dependencies {
implementation("com.facebook.react:react-android")
implementation("com.facebook.react:flipper-integration")
implementation project(':react-native-fs')
implementation 'org.jsoup:jsoup:1.17.2'

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MainApplication : Application(), ReactApplication {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
// add(RNFSPackage())
add(NovelScraperPackage())
}

override fun getJSMainModuleName(): String = "index"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.novelscraper;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NovelScraperPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}

@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();

modules.add(new ScraperModule(reactContext));

return modules;
}

}
102 changes: 102 additions & 0 deletions android/app/src/main/java/com/novelscraper/ScraperModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.novelscraper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class ScraperModule extends ReactContextBaseJavaModule {
private int listenerCount = 0;

ScraperModule(ReactApplicationContext context) {
super(context);
}

@ReactMethod
public void getNovelPaginationPages(String pagePrefixURL, double startPage, double totalPages, Callback callback) {
try {
WritableArray novelPaginationPages = Arguments.createArray();
for (int i = (int)startPage; i <= (int)totalPages; i++) {
Document doc = Jsoup
.connect(pagePrefixURL + i)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
.header("Accept-Language", "*")
.get();
novelPaginationPages.pushString(doc.html());
}
callback.invoke(null, novelPaginationPages);
} catch (IOException e) {
Log.d("ERROR", e.toString());
callback.invoke(e, null);
}
}

@ReactMethod
public void getNovelChapterPages(ReadableArray chapterLinks, Callback callback) {
try {
WritableArray novelChapterPages = Arguments.createArray();
for (int i = 0; i < chapterLinks.size(); i++) {
Log.d("INFO", "Downloading: " + chapterLinks.getString(i));
Document doc = Jsoup
.connect(chapterLinks.getString(i))
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
.header("Accept-Language", "*")
.get();
novelChapterPages.pushString(doc.html());
}
callback.invoke(null, novelChapterPages);
} catch (IOException e) {
Log.d("ERROR", e.toString());
callback.invoke(e, null);
}
}

private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}

@ReactMethod
public void addListener(String eventName) {
if (listenerCount == 0) {
// Set up any upstream listeners or background tasks as necessary
}

listenerCount += 1;
}

@ReactMethod
public void removeListeners(Integer count) {
listenerCount -= count;
if (listenerCount == 0) {
// Remove upstream listeners, stop unnecessary background tasks
}
}

@Override
public String getName() {
return "ScraperModule";
}
}
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ newArchEnabled=false
# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
hermesEnabled=true

org.gradle.daemon=true
21 changes: 17 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
module.exports = {
presets: ["module:@react-native/babel-preset"],
plugins: ["transform-remove-console"]
};
module.exports = function (api) {
api.cache(false);

const presets = ["module:@react-native/babel-preset"];
const plugins = [];

console.log("NODE_ENV", process.env["NODE_ENV"])
if (!!process.env["NODE_ENV"] && process.env["NODE_ENV"] !== "development") {
console.log("NOT IN DEV")
plugins.push("transform-remove-console");
}

return {
presets,
plugins
}
}
18 changes: 18 additions & 0 deletions src/lib/sources/sources.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import { NovelT } from "../types";
import { NativeModules } from "react-native";
const { ScraperModule } = NativeModules;

export const getNovelPaginationPages = (
pagePrefixURL: string,
startPage: number,
totalPage: number,
callback: (error: any, novelPaginationPages: string[]) => void
) => {
ScraperModule.getNovelPaginationPages(pagePrefixURL, startPage, totalPage, callback)
}

export const getNovelChapterPages = (
chapterLinks: string[],
callback: (error: any, novelPaginationPages: string[]) => void
) => {
ScraperModule.getNovelChapterPages(chapterLinks, callback);
}

export const stripDatabaseInfoFromNovel = (novel: NovelT): NovelT => {
let newNovel = { ...novel };
Expand Down

0 comments on commit 1678187

Please sign in to comment.