Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
VimeoParser now is public
Browse files Browse the repository at this point in the history
  • Loading branch information
otopba committed Nov 15, 2017
1 parent 2d7fe51 commit 212f500
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 41 deletions.
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ dependencies {
compile 'com.android.support:support-annotations:25.1.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
testCompile 'junit:junit:4.12'
testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
testCompile('org.powermock:powermock-api-mockito:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile ('org.powermock:powermock-module-junit4:1.6.2') {
testCompile('org.powermock:powermock-module-junit4:1.6.2') {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
Expand All @@ -39,7 +39,7 @@ task sourcesJar(type: Jar) {
}

task javadoc(type: Javadoc) {
failOnError false
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
Expand Down
8 changes: 5 additions & 3 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.breedrapps.vimeoextractor">
<manifest package="uk.breedrapps.vimeoextractor"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET"/>

<application android:allowBackup="true" android:label="@string/app_name"
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public interface OnVimeoExtractionListener {

/**
* Returns a {@link VimeoVideo} object relating to the extraction
*
* @param video the Vimeo video
*/
void onSuccess(VimeoVideo video);

/**
* Returns when an error occurs during extractions
*
* @param throwable the error object
*/
void onFailure(Throwable throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,40 @@ class VimeoAPIManager {

/**
* Builds an HTTP call to Vimeo, from an identifier, to extract video information
*
* @param identifier Vimeo video identifier
* @param referrer Video referrer, null if none present
* @param referrer Video referrer, null if none present
* @return an OKHttp3 Call to use asynchronously or otherwise.
* @throws IOException If a connection or other error occurs
*/
protected Call extractWithIdentifier(@NonNull String identifier, @Nullable String referrer) throws IOException {

String url = String.format(VIMEO_CONFIG_URL, identifier);

if(referrer == null){
if (referrer == null) {
//If no referrer exists, generate from base URL
referrer = String.format(VIMEO_URL, identifier);
}

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.header("Referer", referrer)
.build();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.header("Referer", referrer)
.build();

return client.newCall(request);
return client.newCall(request);
}

/**
* Generates an appropriate error for a given response
*
* @param response The response that was not successful
* @return An Exception based on the HTTP Status code of the response
*/
protected Throwable getError(Response response) {

switch (response.code()){
switch (response.code()) {
case 404:
return new IOException("Video could not be found");
case 403:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,41 @@
/**
* A class used to extract Vimeo video information
* through an all-digit video identifier or a full video URL.
*
* <p>
* Information includes stream urls, title and duration.
*
* <p>
* See {@link VimeoVideo} for full information available
*/
public class VimeoExtractor {

//Singleton
private static VimeoExtractor instance;

private VimeoExtractor(){}
private VimeoExtractor() {
}

/**
* Get singleton instance of the extractor
*
* @return singleton instance
*/
public static VimeoExtractor getInstance() {
if(instance == null){
if (instance == null) {
instance = new VimeoExtractor();
}
return instance;
}

/**
* Get Video stream information using its identifier
*
* @param identifier Non-null numeric video identifier (e.g. 123456)
* @param referrer Video referrer URL. Leaving as null provides referrer as video url by default
* @param listener Callback from extraction
* @param referrer Video referrer URL. Leaving as null provides referrer as video url by default
* @param listener Callback from extraction
*/
public void fetchVideoWithIdentifier(@NonNull String identifier, @Nullable String referrer, @NonNull final OnVimeoExtractionListener listener){
public void fetchVideoWithIdentifier(@NonNull String identifier, @Nullable String referrer, @NonNull final OnVimeoExtractionListener listener) {
//If an invalid identifier is entered, throw an error
if(identifier.length() == 0){
if (identifier.length() == 0) {
listener.onFailure(new IllegalArgumentException("Video identifier cannot be empty"));
return;
}
Expand All @@ -59,11 +62,11 @@ public void onFailure(Call call, IOException e) {
@Override
public void onResponse(Call call, Response response) throws IOException {
//Check if response is successful
if(response.isSuccessful()) {
if (response.isSuccessful()) {
//Generate video object from JSON response
VimeoVideo vimeoVideo = new VimeoVideo(response.body().string());
listener.onSuccess(vimeoVideo);
}else{
} else {
//Generate an appropriate error
listener.onFailure(manager.getError(response));
}
Expand All @@ -77,20 +80,21 @@ public void onResponse(Call call, Response response) throws IOException {

/**
* Get Video stream information from its full URL
*
* @param videoURL Video URL
* @param referrer Video referrer URL
* @param listener Callback from extraction
*/
public void fetchVideoWithURL(@NonNull String videoURL, @Nullable String referrer, @NonNull final OnVimeoExtractionListener listener){
public void fetchVideoWithURL(@NonNull String videoURL, @Nullable String referrer, @NonNull final OnVimeoExtractionListener listener) {
//Check for valid URL length
if(videoURL.length() == 0){
if (videoURL.length() == 0) {
listener.onFailure(new IllegalArgumentException("Video URL cannot be empty"));
return;
}

VimeoParser parser = new VimeoParser(videoURL);
//Determine if Vimeo URL is valid
if(!parser.isVimeoURLValid()) {
if (!parser.isVimeoURLValid()) {
listener.onFailure(new IllegalArgumentException("Vimeo URL is not valid"));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,46 @@
/**
* Parser for a given Vimeo Link
*/
class VimeoParser {
public class VimeoParser {

//Full URL of Vimeo video
private String url;

/**
* Initialise VideoParser with url
*
* @param url Vimeo Video url
*/
public VimeoParser(@NonNull String url){
public VimeoParser(@NonNull String url) {
this.url = url;
}

/**
* Check if a Vimeo URL has a valid identifier
*
* @return true if identifier is valid, false otherwise
*/
public boolean isVimeoURLValid(){
public boolean isVimeoURLValid() {
String videoID = getExtractedIdentifier();
return videoID.length() > 0 && TextUtils.isDigitsOnly(videoID);
}

/**
* Get a Vimeo identifier from the url
*
* @return Vimeo identifier if found and an empty string otherwise
*/
public String getExtractedIdentifier(){
public String getExtractedIdentifier() {
String[] urlParts = url.split("/");
if(urlParts.length == 0){
if (urlParts.length == 0) {
return "";
}
return urlParts[urlParts.length - 1];
}

/**
* Get the URL stored by parser
*
* @return the url
*/
public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public class VimeoUser {
// User Id
private long id;

private VimeoUser(){}
private VimeoUser() {
}

VimeoUser(JSONObject userObject){
VimeoUser(JSONObject userObject) {
this.accountType = userObject.optString("account_type");
this.name = userObject.optString("name");
this.imageUrl = userObject.optString("img");
Expand All @@ -41,6 +42,7 @@ private VimeoUser(){}

/**
* Get account type of user - e.g. plus, basic
*
* @return Account type of user
*/
public String getAccountType() {
Expand All @@ -49,6 +51,7 @@ public String getAccountType() {

/**
* Get full name of user
*
* @return Name of user
*/
public String getName() {
Expand All @@ -57,6 +60,7 @@ public String getName() {

/**
* Profile image of user
*
* @return Image url
*/
public String getImageUrl() {
Expand All @@ -65,6 +69,7 @@ public String getImageUrl() {

/**
* Larger profile image of user
*
* @return HQ image url
*/
public String getImage2xUrl() {
Expand All @@ -73,6 +78,7 @@ public String getImage2xUrl() {

/**
* Profile URL of the user
*
* @return url of profile
*/
public String getUrl() {
Expand All @@ -81,6 +87,7 @@ public String getUrl() {

/**
* Get the Vimeo assigned ID for the user
*
* @return id for user
*/
public long getId() {
Expand Down
16 changes: 12 additions & 4 deletions library/src/main/java/uk/breedrapps/vimeoextractor/VimeoVideo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class VimeoVideo {
private VimeoUser videoUser;

//Initialise VimeoVideo from JSON
protected VimeoVideo(@NonNull String json){
protected VimeoVideo(@NonNull String json) {
streams = new HashMap<>();
thumbs = new HashMap<>();
parseJson(json);
Expand All @@ -51,7 +51,7 @@ private void parseJson(String json) {
//Get thumbnail information
JSONObject thumbsInfo = videoInfo.getJSONObject("thumbs");
Iterator<String> iterator;
for(iterator = thumbsInfo.keys(); iterator.hasNext();) {
for (iterator = thumbsInfo.keys(); iterator.hasNext(); ) {
String key = iterator.next();
this.thumbs.put(key, thumbsInfo.getString(key));
}
Expand All @@ -77,6 +77,7 @@ private void parseJson(String json) {

/**
* Video title
*
* @return the video title
*/
public String getTitle() {
Expand All @@ -85,6 +86,7 @@ public String getTitle() {

/**
* Video duration in seconds
*
* @return the video duration
*/
public long getDuration() {
Expand All @@ -93,24 +95,27 @@ public long getDuration() {

/**
* Check if given video has stream information
*
* @return true if information is present, false otherwise
*/
public boolean hasStreams(){
public boolean hasStreams() {
return streams.size() > 0;
}

/**
* Check if video has HD stream available
*
* @return true if 1080 or 4096p streams are available, false otherwise
*/
public boolean isHD(){
public boolean isHD() {
return streams.containsKey("1080p") || streams.containsKey("4096p");
}

/**
* Get stream information in the form of a key-value map.
* Keys are the quality information of the stream (e.g. 1080p)
* Values are the corresponding stream URL
*
* @return Map of streams for video
*/
public Map<String, String> getStreams() {
Expand All @@ -119,6 +124,7 @@ public Map<String, String> getStreams() {

/**
* Check if video has associated thumbnails
*
* @return true if thumbnails are present; false otherwise
*/
public boolean hasThumbs() {
Expand All @@ -130,6 +136,7 @@ public boolean hasThumbs() {
* Keys are the quality information of the thumbnail (e.g. base, 640, 1280)
* The default key returned from Vimeo's API is "base"
* Values are the corresponding thumbnail image URL
*
* @return Map of available thumbnails for video
*/
public Map<String, String> getThumbs() {
Expand All @@ -138,6 +145,7 @@ public Map<String, String> getThumbs() {

/**
* Get information on the user that created / uploaded the video
*
* @return VimeoUser object containing information on the user
*/
public VimeoUser getVideoUser() {
Expand Down
Loading

0 comments on commit 212f500

Please sign in to comment.