-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Integrate API for displaying featured images #1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
neslihanturan
merged 14 commits into
commons-app:master
from
maskaravivek:featuredImageImpl
May 7, 2018
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c02141e
Integrate API for displaying featured images
790cf96
Add pagination and refactor code so that it can be reused for categor…
31abde9
Add license info to the images
679602e
Fix author view
d8c7ba2
Remove unused values
afd5bc5
Fix minor issues with featured images
3ef7c40
Fix null license url issue
14b9353
Remove some log lines
e23006b
Fix back navigation issue
8106d3c
fix tests
09fe2fe
fix test inits
3bdaf9c
Gracefully handling various error situations
5e33d3f
Added java docs
689df02
Merge branch 'master' into featuredImageImpl
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/main/java/fr/free/nrw/commons/category/CategoryImageController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package fr.free.nrw.commons.category; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import fr.free.nrw.commons.Media; | ||
| import fr.free.nrw.commons.mwapi.MediaWikiApi; | ||
|
|
||
| @Singleton | ||
| public class CategoryImageController { | ||
|
|
||
| private MediaWikiApi mediaWikiApi; | ||
|
|
||
| @Inject | ||
| public CategoryImageController(MediaWikiApi mediaWikiApi) { | ||
| this.mediaWikiApi = mediaWikiApi; | ||
| } | ||
|
|
||
| /** | ||
| * Takes a category name as input and calls the API to get a list of images for that category | ||
| * @param categoryName | ||
| * @return | ||
| */ | ||
| public List<Media> getCategoryImages(String categoryName) { | ||
| return mediaWikiApi.getCategoryImages(categoryName); | ||
| } | ||
| } |
225 changes: 225 additions & 0 deletions
225
app/src/main/java/fr/free/nrw/commons/category/CategoryImageUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| package fr.free.nrw.commons.category; | ||
|
|
||
| import org.jsoup.Jsoup; | ||
| import org.w3c.dom.Element; | ||
| import org.w3c.dom.Node; | ||
| import org.w3c.dom.NodeList; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import fr.free.nrw.commons.Media; | ||
| import timber.log.Timber; | ||
|
|
||
| public class CategoryImageUtils { | ||
|
|
||
| /** | ||
| * The method iterates over the child nodes to return a list of Media objects | ||
| * @param childNodes | ||
| * @return | ||
| */ | ||
| public static List<Media> getMediaList(NodeList childNodes) { | ||
| List<Media> categoryImages = new ArrayList<>(); | ||
| for (int i = 0; i < childNodes.getLength(); i++) { | ||
| Node node = childNodes.item(i); | ||
| categoryImages.add(getMediaFromPage(node)); | ||
| } | ||
|
|
||
| return categoryImages; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new Media object from the XML response as received by the API | ||
| * @param node | ||
| * @return | ||
| */ | ||
| private static Media getMediaFromPage(Node node) { | ||
| Media media = new Media(null, | ||
| getImageUrl(node), | ||
| getFileName(node), | ||
| getDescription(node), | ||
| getDataLength(node), | ||
| getDateCreated(node), | ||
| getDateCreated(node), | ||
| getCreator(node) | ||
| ); | ||
|
|
||
| media.setLicense(getLicense(node)); | ||
|
|
||
| return media; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the filename of the uploaded image | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static String getFileName(Node document) { | ||
| Element element = (Element) document; | ||
| return element.getAttribute("title"); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the image description for that particular upload | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static String getDescription(Node document) { | ||
| return getMetaDataValue(document, "ImageDescription"); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts license information from the image meta data | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static String getLicense(Node document) { | ||
| return getMetaDataValue(document, "License"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the parsed value of artist from the response | ||
| * The artist information is returned as a HTML string from the API. Jsoup library parses the HTML string | ||
| * to extract just the text value | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static String getCreator(Node document) { | ||
| String artist = getMetaDataValue(document, "Artist"); | ||
| if (artist != null) { | ||
| return Jsoup.parse(artist).text(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the parsed date of creation of the image | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static Date getDateCreated(Node document) { | ||
| String dateTime = getMetaDataValue(document, "DateTime"); | ||
| if (dateTime != null && !dateTime.equals("")) { | ||
| SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
| try { | ||
| return format.parse(dateTime); | ||
| } catch (ParseException e) { | ||
| Timber.d("Error occurred while parsing date %s", dateTime); | ||
| return new Date(); | ||
| } | ||
| } | ||
| return new Date(); | ||
| } | ||
|
|
||
| /** | ||
| * @param document | ||
| * @return Returns the url attribute from the imageInfo node | ||
| */ | ||
| private static String getImageUrl(Node document) { | ||
| Element element = (Element) getImageInfo(document); | ||
| if (element != null) { | ||
| return element.getAttribute("url"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Takes the node document and gives out the attribute length from the node document | ||
| * @param document | ||
| * @return | ||
| */ | ||
| private static long getDataLength(Node document) { | ||
| Element element = (Element) document; | ||
| if (element != null) { | ||
| String length = element.getAttribute("length"); | ||
| if (length != null && !length.equals("")) { | ||
| return Long.parseLong(length); | ||
| } | ||
| } | ||
| return 0L; | ||
| } | ||
|
|
||
| /** | ||
| * Generic method to get the value of any meta as returned by the getMetaData function | ||
| * @param document node document as returned by API | ||
| * @param metaName the name of meta node to be returned | ||
| * @return | ||
| */ | ||
| private static String getMetaDataValue(Node document, String metaName) { | ||
| Element metaData = getMetaData(document, metaName); | ||
| if (metaData != null) { | ||
| return metaData.getAttribute("value"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Generic method to return an element taking the node document and metaName as input | ||
| * @param document node document as returned by API | ||
| * @param metaName the name of meta node to be returned | ||
| * @return | ||
| */ | ||
| @Nullable | ||
| private static Element getMetaData(Node document, String metaName) { | ||
| Node extraMetaData = getExtraMetaData(document); | ||
| if (extraMetaData != null) { | ||
| Node node = getNode(extraMetaData, metaName); | ||
| if (node != null) { | ||
| return (Element) node; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts extmetadata from the response XML | ||
| * @param document | ||
| * @return | ||
| */ | ||
| @Nullable | ||
| private static Node getExtraMetaData(Node document) { | ||
| Node imageInfo = getImageInfo(document); | ||
| if (imageInfo != null) { | ||
| return getNode(imageInfo, "extmetadata"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the ii node from the imageinfo node | ||
| * @param document | ||
| * @return | ||
| */ | ||
| @Nullable | ||
| private static Node getImageInfo(Node document) { | ||
| Node imageInfo = getNode(document, "imageinfo"); | ||
| if (imageInfo != null) { | ||
| return getNode(imageInfo, "ii"); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Takes a parent node as input and returns a child node if present | ||
| * @param node parent node | ||
| * @param nodeName child node name | ||
| * @return | ||
| */ | ||
| @Nullable | ||
| public static Node getNode(Node node, String nodeName) { | ||
| NodeList childNodes = node.getChildNodes(); | ||
| for (int i = 0; i < childNodes.getLength(); i++) { | ||
| Node nodeItem = childNodes.item(i); | ||
| Element item = (Element) nodeItem; | ||
| if (item.getTagName().equals(nodeName)) { | ||
| return nodeItem; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @maskaravivek , what is the reason for needing to add this library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This library parses the artist name. The API returns the artist as a HTML string.