Added new worker to fetch data and run the wiki url

master
Tomasz Półgrabia 2016-11-03 21:58:06 +01:00
parent ed5be93a6d
commit d62772241a
3 changed files with 122 additions and 14 deletions

View File

@ -0,0 +1,60 @@
package pl.tpolgrabia.urbanexplorer.worker;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.wikibinding.utils.WikiUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by tpolgrabia on 03.11.16.
*/
public class WikiBrowserWorker extends AsyncTask<WikiAppObject, Integer, List<String>> {
private static final Logger lg = LoggerFactory.getLogger(WikiBrowserWorker.class);
private final Context ctx;
private final WikiUtils wikiUtils;
public WikiBrowserWorker(Context ctx, String countryCode) {
this.ctx = ctx;
wikiUtils = new WikiUtils(ctx, countryCode);
}
@Override
protected List<String> doInBackground(WikiAppObject... params) {
List<String> results = new ArrayList<>();
for (WikiAppObject param : params) {
try {
wikiUtils.fetchPageInfo(param.getPageId());
} catch (IOException e) {
lg.error("I/O error during fetch page info", e);
}
}
return results;
}
@Override
protected void onPostExecute(List<String> strings) {
if (strings == null || strings.isEmpty()) {
return;
}
final int n = strings.size();
if (n > 1) {
lg.warn("Too many results to use - {}", n);
}
String wikiUrl = strings.get(0);
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(wikiUrl));
ctx.startActivity(intent);
}
}

View File

@ -1,6 +1,6 @@
package pl.tpolgrabia.wikibinding;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse;
import com.google.gson.JsonObject;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse2;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2;
import retrofit2.Call;
@ -11,7 +11,10 @@ import retrofit2.http.Query;
* Created by tpolgrabia on 27.10.16.
*/
public interface WikiService {
@GET("api.php?action=query&list=geosearch&format=json")
@GET("api.php" +
"?action=query" +
"&list=geosearch" +
"&format=json")
Call<WikiGeoResponse2> fetchGeoSearch(
@Query("gscoord") String gscoord,
@Query("gsradius") Double radius,
@ -28,4 +31,12 @@ public interface WikiService {
"&format=json")
Call<WikiResponse2> fetchPageInfos(
@Query("pageids") String pageIds);
@GET("api.php" +
"?action=query" +
"&prop=info" +
"&inprop=url" +
"&format=json")
Call<JsonObject> fetchPageInfo(
@Query("pageids") Long pageId);
}

View File

@ -7,6 +7,7 @@ import android.widget.Toast;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.google.gson.JsonObject;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
@ -148,11 +149,7 @@ public class WikiUtils {
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());
httpClient.addInterceptor(new WikiDebugInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + countryCode + ".wikipedia.org/w/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
Retrofit retrofit = createStandardInstance();
final String gscoord = "" + latitude + "|" + longitude;
Log.d("XXX", "GSCoord" + gscoord);
@ -318,11 +315,7 @@ public class WikiUtils {
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());
httpClient.addInterceptor(new WikiDebugInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + countryCode + ".wikipedia.org/w/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
Retrofit retrofit = createStandardInstance();
return retrofit.create(WikiService.class)
.fetchPageInfos(StringUtils.join(pageIds, "|"))
@ -363,13 +356,57 @@ public class WikiUtils {
Long pageId,
AjaxCallback<JSONObject> callback) {
NetUtils.createProxyAQueryInstance(ctx).ajax(
"https://" + countryCode + ".wikipedia.org/w/api.php?action=query&prop=info&pageids="
+ pageId + "&inprop=url&format=json",
"https://" + countryCode + ".wikipedia.org/w/api.php" +
"?action=query" +
"&prop=info" +
"&inprop=url" +
"&format=json" +
"&pageids=" + pageId,
JSONObject.class,
callback
);
}
public Response<JsonObject> fetchPageInfo(Long pageId) throws IOException {
Retrofit retrofit = createStandardInstance();
return retrofit.create(WikiService.class).fetchPageInfo(pageId).execute();
}
public String fetchPageInfoUrl(Long pageId) throws IOException {
Response<JsonObject> pageResponse = fetchPageInfo(pageId);
lg.debug("Received page response {}", pageResponse);
final int responseCode = pageResponse.code();
lg.debug("Received page respone code {}", responseCode);
if (responseCode != 200) {
return null;
}
JsonObject pageBody = pageResponse.body();
lg.debug("Received page response body {}", pageBody);
String wikiUrl = pageBody.getAsJsonObject("query")
.getAsJsonObject("pages")
.getAsJsonObject(pageId == null ? null : pageId.toString())
.getAsJsonPrimitive("fullurl")
.getAsString();
lg.debug("Received page wiki url {}", wikiUrl);
return wikiUrl;
}
private Retrofit createStandardInstance() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());
httpClient.addInterceptor(new WikiDebugInterceptor());
return new Retrofit.Builder()
.baseUrl("https://" + countryCode + ".wikipedia.org/w/")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
public void fetchAppData(WikiAppResponseCallback clbk) {
final Location location = LocationUtils.getLastKnownLocation(ctx);