mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Merge pull request #33 from misaochan/api-calls
Implemented calls to API to extract categories
This commit is contained in:
commit
d98dc42368
5 changed files with 204 additions and 0 deletions
|
|
@ -0,0 +1,161 @@
|
|||
package fr.free.nrw.commons.upload;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.Cache;
|
||||
import com.android.volley.NetworkResponse;
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.HttpHeaderParser;
|
||||
import com.android.volley.toolbox.JsonRequest;
|
||||
import com.android.volley.toolbox.Volley;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class MwVolleyApi {
|
||||
|
||||
private static RequestQueue REQUEST_QUEUE;
|
||||
private static final Gson GSON = new GsonBuilder().create();
|
||||
private Context context;
|
||||
|
||||
public MwVolleyApi(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
public void request(String apiUrl) {
|
||||
JsonRequest<QueryResponse> request = new QueryRequest(apiUrl,
|
||||
new LogResponseListener<QueryResponse>(), new LogResponseErrorListener());
|
||||
getQueue().add(request);
|
||||
}
|
||||
|
||||
private RequestQueue getQueue() {
|
||||
return getQueue(context);
|
||||
}
|
||||
|
||||
private static RequestQueue getQueue(Context context) {
|
||||
if (REQUEST_QUEUE == null) {
|
||||
REQUEST_QUEUE = Volley.newRequestQueue(context.getApplicationContext());
|
||||
}
|
||||
return REQUEST_QUEUE;
|
||||
}
|
||||
|
||||
private static class LogResponseListener<T> implements Response.Listener<T> {
|
||||
private static final String TAG = LogResponseListener.class.getName();
|
||||
|
||||
@Override
|
||||
public void onResponse(T response) {
|
||||
Log.d(TAG, response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static class LogResponseErrorListener implements Response.ErrorListener {
|
||||
private static final String TAG = LogResponseErrorListener.class.getName();
|
||||
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
Log.e(TAG, error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static class QueryRequest extends JsonRequest<QueryResponse> {
|
||||
private static final String TAG = QueryRequest.class.getName();
|
||||
|
||||
public QueryRequest(String url,
|
||||
Response.Listener<QueryResponse> listener,
|
||||
Response.ErrorListener errorListener) {
|
||||
super(Request.Method.GET, url, null, listener, errorListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Response<QueryResponse> parseNetworkResponse(NetworkResponse response) {
|
||||
String json = parseString(response);
|
||||
//Log.d(TAG, "json=" + json);
|
||||
QueryResponse queryResponse = GSON.fromJson(json, QueryResponse.class);
|
||||
return Response.success(queryResponse, cacheEntry(response));
|
||||
}
|
||||
|
||||
|
||||
private Cache.Entry cacheEntry(NetworkResponse response) {
|
||||
return HttpHeaderParser.parseCacheHeaders(response);
|
||||
}
|
||||
|
||||
|
||||
private String parseString(NetworkResponse response) {
|
||||
try {
|
||||
return new String(response.data, HttpHeaderParser.parseCharset(response.headers));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return new String(response.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class QueryResponse {
|
||||
private Query query;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "query=" + query.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Query {
|
||||
private Page [] pages;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder("pages=" + "\n");
|
||||
for (Page page : pages) {
|
||||
builder.append(page.toString());
|
||||
builder.append("\n");
|
||||
}
|
||||
builder.replace(builder.length() - 1, builder.length(), "");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Page {
|
||||
private int pageid;
|
||||
private int ns;
|
||||
private String title;
|
||||
private Category[] categories;
|
||||
private Category category;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
StringBuilder builder = new StringBuilder("PAGEID=" + pageid + " ns=" + ns + " title=" + title + "\n" + " CATEGORIES= ");
|
||||
if (categories != null) {
|
||||
for (Category category : categories) {
|
||||
builder.append(category.toString());
|
||||
builder.append("\n");
|
||||
}
|
||||
}
|
||||
else {
|
||||
builder.append("no categories exist");
|
||||
builder.append("\n");
|
||||
}
|
||||
|
||||
builder.replace(builder.length() - 1, builder.length(), "");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class Category {
|
||||
private int ns;
|
||||
private String title;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return " ns=" + ns + " title=" + title;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -175,13 +175,24 @@ public class ShareActivity
|
|||
mediaUriString = mediaUri.toString();
|
||||
Log.d("Image", "Uri: " + mediaUriString);
|
||||
|
||||
//convert image Uri to file path
|
||||
FilePathConverter uriObj = new FilePathConverter(this, mediaUri);
|
||||
String filePath = uriObj.getFilePath();
|
||||
|
||||
//extract the coordinates of image in decimal degrees
|
||||
GPSExtractor imageObj = new GPSExtractor(filePath);
|
||||
String coords = imageObj.getCoords();
|
||||
Log.d("Image", "Coords of image: " + coords);
|
||||
|
||||
//build URL with image coords for MediaWiki API calls
|
||||
String apiUrl = UrlBuilder.buildUrl(coords);
|
||||
Log.d("Image", "URL: " + apiUrl);
|
||||
|
||||
|
||||
//asynchronous calls to MediaWiki Commons API to match image coords with nearby Commons categories
|
||||
MwVolleyApi apiCall = new MwVolleyApi(this);
|
||||
apiCall.request(apiUrl);
|
||||
|
||||
|
||||
ImageLoader.getInstance().displayImage(mediaUriString, backgroundImageView);
|
||||
|
||||
|
|
@ -192,6 +203,7 @@ public class ShareActivity
|
|||
requestAuthToken();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
|
@ -208,4 +220,35 @@ public class ShareActivity
|
|||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds URL with image coords for MediaWiki API calls
|
||||
* Example URL: https://commons.wikimedia.org/w/api.php?action=query&prop=categories|coordinates|pageprops&format=json&clshow=!hidden&coprop=type|name|dim|country|region|globe&codistancefrompoint=38.11386944444445|13.356263888888888&
|
||||
* generator=geosearch&redirects=&ggscoord=38.11386944444445|13.356263888888888&ggsradius=100&ggslimit=10&ggsnamespace=6&ggsprop=type|name|dim|country|region|globe&ggsprimary=all&formatversion=2
|
||||
*/
|
||||
public static class UrlBuilder {
|
||||
private static String buildUrl (String coords){
|
||||
|
||||
Uri.Builder builder = Uri.parse("https://commons.wikimedia.org/").buildUpon();
|
||||
|
||||
builder.appendPath("w")
|
||||
.appendPath("api.php")
|
||||
.appendQueryParameter("action", "query")
|
||||
.appendQueryParameter("prop", "categories|coordinates|pageprops")
|
||||
.appendQueryParameter("format", "json")
|
||||
.appendQueryParameter("clshow", "!hidden")
|
||||
.appendQueryParameter("coprop", "type|name|dim|country|region|globe")
|
||||
.appendQueryParameter("codistancefrompoint", coords)
|
||||
.appendQueryParameter("generator", "geosearch")
|
||||
.appendQueryParameter("ggscoord", coords)
|
||||
.appendQueryParameter("ggsradius", "100")
|
||||
.appendQueryParameter("ggslimit", "10")
|
||||
.appendQueryParameter("ggsnamespace", "6")
|
||||
.appendQueryParameter("ggsprop", "type|name|dim|country|region|globe")
|
||||
.appendQueryParameter("ggsprimary", "all")
|
||||
.appendQueryParameter("formatversion", "2");
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
BIN
lib/gson-1.1-javadoc.jar
Normal file
BIN
lib/gson-1.1-javadoc.jar
Normal file
Binary file not shown.
BIN
lib/gson-1.1-sources.jar
Normal file
BIN
lib/gson-1.1-sources.jar
Normal file
Binary file not shown.
BIN
lib/gson-1.1.jar
Normal file
BIN
lib/gson-1.1.jar
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue