Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#12: read daemon port from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
hugbug committed Mar 31, 2019
1 parent 11cfc20 commit 84ad70a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
50 changes: 44 additions & 6 deletions app/src/main/java/net/nzbget/nzbget/APIManager.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package net.nzbget.nzbget;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import okhttp3.MediaType;
Expand All @@ -18,25 +23,58 @@ public class APIManager {

private static OkHttpClient mClient = new OkHttpClient();

public static JSONObject getConfig() throws IOException, JSONException {
private static String mPort;

private static String RpcUrl(Context context)
{
if (mPort == null)
{
// Most of the time, dataDir will be "/data/data/net.nzbget.nzbget"
String dataDir = context.getApplicationInfo().dataDir;
String configFile = dataDir + "/nzbget/nzbget.conf";

File file = new File(configFile);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null)
{
if (line.startsWith("ControlPort="))
{
mPort = line.substring("ControlPort=".length());
break;
}
}
br.close();
}
catch (IOException e)
{
mPort = "6789";
}
}

return "http://localhost:" + mPort + "/jsonrpc";
}

public static JSONObject getConfig(Context context) throws IOException, JSONException {
Request request = new Request.Builder()
.url("http://localhost:6789/jsonrpc/config")
.url(RpcUrl(context) + "/config")
.build();
Response response = mClient.newCall(request).execute();
String responseData = response.body().string();
return new JSONObject(responseData);
}

public static JSONObject getHistory() throws IOException, JSONException {
public static JSONObject getHistory(Context context) throws IOException, JSONException {
Request request = new Request.Builder()
.url("http://localhost:6789/jsonrpc/history?=false")
.url(RpcUrl(context) + "/history?=false")
.build();
Response response = mClient.newCall(request).execute();
String responseData = response.body().string();
return new JSONObject(responseData);
}

public static boolean postEditQueue(String command, String param, int[] ids) throws IOException, JSONException {
public static boolean postEditQueue(Context context, String command, String param, int[] ids) throws IOException, JSONException {
JSONObject jsonBody = new JSONObject();
jsonBody.put("method", "editqueue");
JSONArray params = new JSONArray();
Expand All @@ -46,7 +84,7 @@ public static boolean postEditQueue(String command, String param, int[] ids) thr
jsonBody.put("params", params);
RequestBody body = RequestBody.create(mJsonMediaType, jsonBody.toString());
Request request = new Request.Builder()
.url("http://localhost:6789/jsonrpc")
.url(RpcUrl(context))
.post(body)
.build();
Response response = mClient.newCall(request).execute();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/nzbget/nzbget/DaemonService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void startWatchingHistory() {
@Override
public void run() {
try {
JSONObject response = APIManager.getConfig();
JSONObject response = APIManager.getConfig(DaemonService.this);
// Get queue path
JSONArray resultArray = response.getJSONArray("result");
String queueDir = null;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/net/nzbget/nzbget/HistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void run() {
private void getHistory() {
// Get history fom API
try {
JSONObject response = APIManager.getHistory();
JSONObject response = APIManager.getHistory(mCtx);
JSONArray resultArray = response.getJSONArray("result");
for (int i = 0; i < resultArray.length(); i++) {
JSONObject object = resultArray.getJSONObject(i);
Expand Down Expand Up @@ -136,7 +136,7 @@ private void setHandledHistoryEntry(JSONObject jsonObject) {
try {
int nzbId = jsonObject.getInt("NZBID");
// Set entry as handled in NZBGet
boolean success = APIManager.postEditQueue("HistorySetParameter", "HandledByAndroidDaemon=true", new int[]{nzbId});
boolean success = APIManager.postEditQueue(mCtx, "HistorySetParameter", "HandledByAndroidDaemon=true", new int[]{nzbId});
if (!success) {
Log.e(mLogTag, "Could not set history parameter using edit queue");
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/net/nzbget/nzbget/StorageActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.nzbget.nzbget;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -82,7 +83,7 @@ private void setUpActivity() {
@Override
public void run() {
try {
JSONObject response = APIManager.getConfig();
JSONObject response = APIManager.getConfig(StorageActivity.this);
// Get queue path
JSONArray resultArray = response.getJSONArray("result");
for (int i = 0; i < resultArray.length(); i++) {
Expand Down

0 comments on commit 84ad70a

Please sign in to comment.