Skip to content

Commit

Permalink
Play SBS video
Browse files Browse the repository at this point in the history
  • Loading branch information
xwz committed Sep 16, 2015
1 parent 69fd014 commit e7e2ca8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 19 deletions.
3 changes: 1 addition & 2 deletions sbs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ android {

productFlavors {
app {
buildConfigField 'String', 'AUTH_TOKEN', getAppProp('SBS_AUTH_TOKEN')
buildConfigField 'String', 'API_URL', getAppProp('SBS_API_URL')
buildConfigField 'String', 'RELATED_URL', getAppProp('SBS_RELATED_URL')
buildConfigField 'String', 'SMIL_URL', getAppProp('SBS_SMIL_URL')
buildConfigField 'String', 'VIDEO_URL', getAppProp('SBS_VIDEO_URL')
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions sbs/src/main/java/io/github/xwz/sbs/api/SBSApiBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.xwz.base.ImmutableMap;
import io.github.xwz.base.api.HttpApiBase;
import io.github.xwz.base.models.IEpisodeModel;
import io.github.xwz.sbs.BuildConfig;
Expand All @@ -26,15 +29,10 @@ abstract class SBSApiBase extends HttpApiBase {

private static final String API_URL = BuildConfig.API_URL;
private static final String RELATED_URL = BuildConfig.RELATED_URL;
private static final String SMIL_URL = BuildConfig.SMIL_URL;
private static final String AUTH_TOKEN = BuildConfig.AUTH_TOKEN;

// http://www.sbs.com.au/api/video_feed/related?context=android&form=json&id=517455939941
// http://www.sbs.com.au/api/video_feed/smil?context=android&form=xml&id=517455939941

// Basic YW5kcm9pZDliYTNmMTgzYmM1MjY5MzI4YTM1YTBlNmQzYmJmMGYwMmY5ZTFhZWE6YW5kcm9pZA==
private static final String VIDEO_URL = BuildConfig.VIDEO_URL;

private static final String CACHE_PATH = "sbs-api";
private static final Pattern ID_PATTERN = Pattern.compile("/(\\d+)$");

private int lastEntryCount = 0;

Expand All @@ -54,8 +52,14 @@ Uri buildRelatedUrl(Map<String, String> params) {
return buildUrl(RELATED_URL, params);
}

Uri buildVideoUrl(Map<String, String> params) {
return buildUrl(SMIL_URL, params);
protected Uri createStreamUrl(String href) {
String id = getIdFromUrl(href);
if (id != null) {
Uri.Builder uri = Uri.parse(VIDEO_URL).buildUpon();
uri.appendPath(id);
return uri.build();
}
return null;
}

private Uri buildUrl(String path, Map<String, String> params) {
Expand All @@ -68,6 +72,14 @@ private Uri buildUrl(String path, Map<String, String> params) {
return uri.build();
}

String getIdFromUrl(String url) {
Matcher m = ID_PATTERN.matcher(url);
if (m.find()) {
return m.group(1);
}
return null;
}

protected int getLastEntryCount() {
return lastEntryCount;
}
Expand Down
114 changes: 114 additions & 0 deletions sbs/src/main/java/io/github/xwz/sbs/api/SBSAuthApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package io.github.xwz.sbs.api;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.xwz.sbs.content.ContentManager;

public class SBSAuthApi extends SBSApiBase {
private static final String TAG = "SBSAuthApi";
private final String id;
private static final Pattern SRC_PATTERN = Pattern.compile("<video src=\"([^\"]+)\"");

public SBSAuthApi(Context context, String id) {
super(context);
setEnableCache(false);
this.id = id;
}

@Override
protected Void doInBackground(String... urls) {
if (urls.length > 0) {
buildAuth(urls[0]);
}
return null;
}

private void buildAuth(String href) {
Uri url = createStreamUrl(href);
if (url != null) {
String content = fetchUrlSkipLocalCache(url, 0);
if (content != null) {
parseContent(content);
} else {
ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_ERROR, ContentManager.AUTH_FAILED_NETWORK, id);
}
}
}

private void parseContent(String content) {
String f = "var playerParams = {";
int pos = content.indexOf(f);
if (pos >= 0) {
int start = pos + f.length();
int end = content.indexOf("};", start);
if (end > pos) {
String data = content.substring(start - 1, end + 1);
Gson gson = new Gson();
try {
PlayerParams params = gson.fromJson(data, PlayerParams.class);
Log.d(TAG, "params: " + params);
if (params != null && params.releaseUrls != null) {
if (params.releaseUrls.html != null && params.releaseUrls.html.length() > 0) {
Uri.Builder builder = Uri.parse(params.releaseUrls.html).buildUpon();
Uri url = builder.build();
loadPlayList(url);
return;
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}
}
ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_ERROR, ContentManager.AUTH_FAILED_TOKEN, id);
}

private void loadPlayList(Uri url) {
String content = fetchUrlSkipLocalCache(url, 0);
if (content != null) {
parsePlayList(content);
} else {
ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_ERROR, ContentManager.AUTH_FAILED_NETWORK, id);
}
}

private void parsePlayList(String content) {
Matcher m = SRC_PATTERN.matcher(content);
if (m.find()) {
Uri.Builder builder = Uri.parse(m.group(1)).buildUpon();
Uri url = builder.build();
Log.d(TAG, "Stream URL:" + url);
ContentManager.cache().putStreamUrl(id, url);
ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_DONE, id);
} else {
ContentManager.getInstance().broadcastChange(ContentManager.CONTENT_AUTH_ERROR, ContentManager.AUTH_FAILED_TOKEN, id);
}
}

static class ReleaseUrl {
private String progressive;
private String html;
private String standard;

public String toString() {
return progressive + " | " + html + " | " + standard;
}

}

static class PlayerParams {
private ReleaseUrl releaseUrls;

public String toString() {
return "url:" + releaseUrls;
}
}
}
8 changes: 2 additions & 6 deletions sbs/src/main/java/io/github/xwz/sbs/api/SBSRelatedApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.github.xwz.base.ImmutableMap;
import io.github.xwz.base.content.IContentManager;
Expand All @@ -18,7 +16,6 @@
public class SBSRelatedApi extends SBSApiBase {
private static final String TAG = "SBSRelatedApi";
private static final int CACHE_EXPIRY = 3600; // 1h
private static final Pattern ID_PATTERN = Pattern.compile("/(\\d+)$");

private final String id;
private boolean success = false;
Expand Down Expand Up @@ -63,9 +60,8 @@ private boolean updateEpisode(String url) {
}

private List<IEpisodeModel> fetchRelated(String url) {
Matcher m = ID_PATTERN.matcher(url);
if (m.find()) {
String id = m.group(1);
String id = getIdFromUrl(url);
if (id != null) {
List<IEpisodeModel> all = new ArrayList<>();
List<IEpisodeModel> related = fetchContent(getRelatedUrl(id), CACHE_EXPIRY);
for (IEpisodeModel ep : related) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.xwz.base.content.IContentManager;
import io.github.xwz.base.models.IEpisodeModel;
import io.github.xwz.sbs.api.SBSApi;
import io.github.xwz.sbs.api.SBSAuthApi;
import io.github.xwz.sbs.api.SBSRelatedApi;

public class ContentManager implements IContentManager {
Expand Down Expand Up @@ -103,12 +104,13 @@ public List<IEpisodeModel> searchShows(String query) {

@Override
public void fetchAuthToken(IEpisodeModel episode) {

mCache.broadcastChange(CONTENT_AUTH_FETCHING, episode.getHref());
new SBSAuthApi(mContext, episode.getHref()).execute(episode.getHref());
}

@Override
public Uri getEpisodeStreamUrl(IEpisodeModel episode) {
return null;
return mCache.getEpisodeStreamUrl(episode.getHref());
}

@Override
Expand Down

0 comments on commit e7e2ca8

Please sign in to comment.