Search in sources :

Example 6 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class ModificationsSyncAdapter method onPerformSync.

@Override
public void onPerformSync(Account account, Bundle bundle, String s, ContentProviderClient contentProviderClient, SyncResult syncResult) {
    // This code is fraught with possibilities of race conditions, but lalalalala I can't hear you!
    Cursor allModifications;
    try {
        allModifications = contentProviderClient.query(ModificationsContentProvider.BASE_URI, null, null, null, null);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    }
    // Exit early if nothing to do
    if (allModifications == null || allModifications.getCount() == 0) {
        Timber.d("No modifications to perform");
        return;
    }
    String authCookie;
    try {
        authCookie = AccountManager.get(getContext()).blockingGetAuthToken(account, "", false);
    } catch (OperationCanceledException | AuthenticatorException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Timber.d("Could not authenticate :(");
        return;
    }
    if (Utils.isNullOrWhiteSpace(authCookie)) {
        Timber.d("Could not authenticate :(");
        return;
    }
    MWApi api = CommonsApplication.getInstance().getMWApi();
    api.setAuthCookie(authCookie);
    String editToken;
    ApiResult requestResult, responseResult;
    try {
        editToken = api.getEditToken();
    } catch (IOException e) {
        Timber.d("Can not retreive edit token!");
        return;
    }
    allModifications.moveToFirst();
    Timber.d("Found %d modifications to execute", allModifications.getCount());
    ContentProviderClient contributionsClient = null;
    try {
        contributionsClient = getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY);
        while (!allModifications.isAfterLast()) {
            ModifierSequence sequence = ModifierSequence.fromCursor(allModifications);
            sequence.setContentProviderClient(contentProviderClient);
            Contribution contrib;
            Cursor contributionCursor;
            try {
                contributionCursor = contributionsClient.query(sequence.getMediaUri(), null, null, null, null);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
            contributionCursor.moveToFirst();
            contrib = Contribution.fromCursor(contributionCursor);
            if (contrib.getState() == Contribution.STATE_COMPLETED) {
                try {
                    requestResult = api.action("query").param("prop", "revisions").param("rvprop", "timestamp|content").param("titles", contrib.getFilename()).get();
                } catch (IOException e) {
                    Timber.d("Network fuckup on modifications sync!");
                    continue;
                }
                Timber.d("Page content is %s", Utils.getStringFromDOM(requestResult.getDocument()));
                String pageContent = requestResult.getString("/api/query/pages/page/revisions/rev");
                String processedPageContent = sequence.executeModifications(contrib.getFilename(), pageContent);
                try {
                    responseResult = api.action("edit").param("title", contrib.getFilename()).param("token", editToken).param("text", processedPageContent).param("summary", sequence.getEditSummary()).post();
                } catch (IOException e) {
                    Timber.d("Network fuckup on modifications sync!");
                    continue;
                }
                Timber.d("Response is %s", Utils.getStringFromDOM(responseResult.getDocument()));
                String result = responseResult.getString("/api/edit/@result");
                if (!result.equals("Success")) {
                    // FIXME: Log this somewhere else
                    Timber.d("Non success result! %s", result);
                } else {
                    sequence.delete();
                }
            }
            allModifications.moveToNext();
        }
    } finally {
        if (contributionsClient != null) {
            contributionsClient.release();
        }
    }
}
Also used : OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) Cursor(android.database.Cursor) ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient) Contribution(fr.free.nrw.commons.contributions.Contribution)

Example 7 with MWApi

use of fr.free.nrw.commons.MWApi in project apps-android-commons by commons-app.

the class ExistingFileAsync method doInBackground.

@Override
protected Boolean doInBackground(Void... voids) {
    MWApi api = CommonsApplication.getInstance().getMWApi();
    ApiResult result;
    // https://commons.wikimedia.org/w/api.php?action=query&list=allimages&format=xml&aisha1=801957214aba50cb63bb6eb1b0effa50188900ba
    try {
        result = api.action("query").param("format", "xml").param("list", "allimages").param("aisha1", fileSHA1).get();
        Timber.d("Searching Commons API for existing file: %s", result);
    } catch (IOException e) {
        Timber.e(e, "IO Exception: ");
        return false;
    }
    ArrayList<ApiResult> resultNodes = result.getNodes("/api/query/allimages/img");
    Timber.d("Result nodes: %s", resultNodes);
    boolean fileExists = !resultNodes.isEmpty();
    Timber.d("File already exists in Commons: %s", fileExists);
    return fileExists;
}
Also used : ApiResult(org.mediawiki.api.ApiResult) MWApi(fr.free.nrw.commons.MWApi) IOException(java.io.IOException)

Aggregations

MWApi (fr.free.nrw.commons.MWApi)7 IOException (java.io.IOException)6 ApiResult (org.mediawiki.api.ApiResult)6 ArrayList (java.util.ArrayList)4 RemoteException (android.os.RemoteException)2 AuthenticatorException (android.accounts.AuthenticatorException)1 OperationCanceledException (android.accounts.OperationCanceledException)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentValues (android.content.ContentValues)1 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Contribution (fr.free.nrw.commons.contributions.Contribution)1 Date (java.util.Date)1