3
3
import static fr .free .nrw .commons .depictions .Media .DepictedImagesFragment .PAGE_ID_PREFIX ;
4
4
5
5
import androidx .core .text .HtmlCompat ;
6
- import com .google .gson .JsonArray ;
7
- import com .google .gson .JsonObject ;
6
+ import fr .free .nrw .commons .media .Depictions ;
8
7
import fr .free .nrw .commons .media .MediaClient ;
9
8
import io .reactivex .Single ;
10
- import java .util .ArrayList ;
11
- import java .util .HashMap ;
12
- import java .util .Map ;
13
9
import javax .inject .Inject ;
14
10
import javax .inject .Singleton ;
15
11
import org .jetbrains .annotations .NotNull ;
24
20
@ Singleton
25
21
public class MediaDataExtractor {
26
22
27
- private static final int LABEL_BEGIN_INDEX = 3 ;
28
- private static final int LABEL_END_OFFSET = 3 ;
29
- private static final int ID_BEGIN_INDEX = 1 ;
30
- private static final int ID_END_OFFSET = 1 ;
31
23
private final MediaClient mediaClient ;
32
24
33
25
@ Inject
@@ -52,10 +44,10 @@ public Single<Media> fetchMediaDetails(final String filename, final String pageI
52
44
53
45
@ NotNull
54
46
private Media combineToMedia (final Media media , final Boolean deletionStatus , final String discussion ,
55
- final String caption , final JsonObject depiction ) {
47
+ final String caption , final Depictions depictions ) {
56
48
media .setDiscussion (discussion );
57
49
media .setCaption (caption );
58
- media .setDepictionList ( formatDepictions ( depiction ) );
50
+ media .setDepictions ( depictions );
59
51
if (deletionStatus ) {
60
52
media .setRequestedDeletion (true );
61
53
}
@@ -74,39 +66,12 @@ private Single<String> getCaption(final String wikibaseIdentifier) {
74
66
}
75
67
76
68
/**
77
- * From the Json Object extract depictions into an array list
78
- * @param mediaResponse
79
- * @return List containing map for depictions, the map has two keys,
80
- * first key is for the label and second is for the url of the item
81
- */
82
- private ArrayList <Map <String , String >> formatDepictions (final JsonObject mediaResponse ) {
83
- try {
84
- final JsonArray depictionArray = (JsonArray ) mediaResponse .get ("Depiction" );
85
- final ArrayList <Map <String , String >> depictedItemList = new ArrayList <>();
86
- for (int i = 0 ; i <depictionArray .size () ; i ++) {
87
- final JsonObject depictedItem = (JsonObject ) depictionArray .get (i );
88
- final Map <String , String > depictedObject = new HashMap <>();
89
- final String label = depictedItem .get ("label" ).toString ();
90
- final String id = depictedItem .get ("id" ).toString ();
91
- final String transformedLabel = label .substring (LABEL_BEGIN_INDEX , label .length ()- LABEL_END_OFFSET );
92
- final String transformedId = id .substring (ID_BEGIN_INDEX ,id .length () - ID_END_OFFSET );
93
- depictedObject .put ("label" , transformedLabel ); //remove the additional characters obtained in label and ID object to extract the relevant string (since the string also contains extra quites that are not required)
94
- depictedObject .put ("id" , transformedId );
95
- depictedItemList .add (depictedObject );
96
- }
97
- return depictedItemList ;
98
- } catch (final ClassCastException | NullPointerException ignore ) {
99
- return new ArrayList <>();
100
- }
101
- }
102
-
103
- /**
104
- * Fetch caption and depictions from the MediaWiki API
69
+ * Fetch depictions from the MediaWiki API
105
70
* @param filename the filename we will return the caption for
106
- * @return a map containing caption and depictions (empty string in the map if no caption/depictions)
71
+ * @return Depictions
107
72
*/
108
- private Single <JsonObject > getDepictions (final String filename ) {
109
- return mediaClient .getCaptionAndDepictions (filename )
73
+ private Single <Depictions > getDepictions (final String filename ) {
74
+ return mediaClient .getDepictions (filename )
110
75
.doOnError (throwable -> Timber .e (throwable , "error while fetching depictions" ));
111
76
}
112
77
0 commit comments