Skip to content
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

Consume TBA API media URLs #890

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Database extends SQLiteOpenHelper {
public static final String ALL_EVENTS_LOADED_TO_DATABASE_FOR_YEAR = "all_events_loaded_for_year_";
public static final String ALL_DISTRICTS_LOADED_TO_DATABASE_FOR_YEAR = "all_districts_loaded_for_year_";

static final int DATABASE_VERSION = 35;
static final int DATABASE_VERSION = 36;
private static final String DATABASE_NAME = "the-blue-alliance-android-database";
static final @Deprecated String TABLE_API = "api";
public static final String TABLE_TEAMS = "teams",
Expand Down Expand Up @@ -111,6 +111,8 @@ public class Database extends SQLiteOpenHelper {
+ MediasTable.TEAMKEY + " TEXT DEFAULT '', "
+ MediasTable.DETAILS + " TEXT DEFAULT '', "
+ MediasTable.YEAR + " INTEGER DEFAULT -1, "
+ MediasTable.VIEW_URL + " TEXT DEFAULT '', "
+ MediasTable.DIRECT_URL + " TEXT DEFAULT '', "
+ MediasTable.LAST_MODIFIED + " TIMESTAMP"
+ ")";
public static final String CREATE_EVENTTEAMS = "CREATE TABLE IF NOT EXISTS "
Expand Down Expand Up @@ -553,6 +555,20 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.endTransaction();
}
break;
case 36:
db.beginTransaction();
try {
if (!columnExists(db, TABLE_MEDIAS, MediasTable.DIRECT_URL)) {
db.execSQL("ALTER TABLE " + TABLE_MEDIAS + " ADD COLUMN " + MediasTable.DIRECT_URL + " TEXT DEFAULT '' ");
}
if (!columnExists(db, TABLE_MEDIAS, MediasTable.VIEW_URL)) {
db.execSQL("ALTER TABLE " + TABLE_MEDIAS + " ADD COLUMN " + MediasTable.VIEW_URL + " TEXT DEFAULT '' ");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
break;
}
upgradeTo++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ public static Media inflateMedia(Cursor data) {
case MediasTable.DETAILS:
media.setDetails(data.getString(i));
break;
case MediasTable.DIRECT_URL:
media.setDirectUrl(data.getString(i));
break;
case MediasTable.VIEW_URL:
media.setViewUrl(data.getString(i));
break;
case MediasTable.LAST_MODIFIED:
media.setLastModified(data.getLong(i));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MediasTable extends ModelTable<Media> {
TEAMKEY = "teamKey",
DETAILS = "details",
YEAR = "year",
DIRECT_URL = "directUrl",
VIEW_URL = "viewUrl",
LAST_MODIFIED = "last_modified";

public MediasTable(SQLiteDatabase db, Gson gson){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public Media deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
media.setDetails(object.get("details").toString());
}

if (object.has("direct_url")) {
media.setDirectUrl(object.get("direct_url").getAsString());
}

if (object.has("view_url")) {
media.setViewUrl(object.get("view_url").getAsString());
}

return media;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ public View getView(final Context c, LayoutInflater inflater, View convertView)
holder = (ViewHolder) convertView.getTag();
}

Picasso picasso = Picasso.with(c);
picasso.load(imageUrl).into(holder.image);
if (imageUrl != null) {
Picasso picasso = Picasso.with(c);
picasso.load(imageUrl).into(holder.image);
}

if (isVideo) {
holder.youtubePlayIcon.setVisibility(View.VISIBLE);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class Media implements IMedia, TbaDatabaseModel, RenderableModel<Media> {
private JsonObject details;
private String teamKey;
private int year;
private String directUrl;
private String viewUrl;

public Media() {
}
Expand Down Expand Up @@ -100,6 +102,22 @@ public void setYear(int year) {
this.year = year;
}

public String getDirectUrl() {
return directUrl;
}

public void setDirectUrl(String directUrl) {
this.directUrl = directUrl;
}

public String getViewUrl() {
return viewUrl;
}

public void setViewUrl(String viewUrl) {
this.viewUrl = viewUrl;
}

@Override
public ContentValues getParams(Gson gson) {
ContentValues data = new ContentValues();
Expand All @@ -108,6 +126,8 @@ public ContentValues getParams(Gson gson) {
data.put(MediasTable.TEAMKEY, getTeamKey());
data.put(MediasTable.DETAILS, getDetails());
data.put(MediasTable.YEAR, getYear());
data.put(MediasTable.DIRECT_URL, getDirectUrl());
data.put(MediasTable.VIEW_URL, getViewUrl());
data.put(MediasTable.LAST_MODIFIED, getLastModified());
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@

import android.support.annotation.Nullable;

import com.google.gson.JsonObject;
import com.thebluealliance.androidclient.listitems.ImageListElement;
import com.thebluealliance.androidclient.listitems.ListElement;
import com.thebluealliance.androidclient.models.Media;
import com.thebluealliance.androidclient.types.MediaType;
import com.thebluealliance.androidclient.types.ModelType;

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

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class MediaRenderer implements ModelRenderer<Media, Void> {

private static final Pattern YOUTUBE_KEY_PATTERN = Pattern.compile("^([a-zA-Z0-9_-]*)");

@Inject
public MediaRenderer() {

Expand All @@ -33,37 +27,7 @@ public MediaRenderer() {

@Override
public @Nullable ImageListElement renderFromModel(Media media, Void aVoid) {
String imageUrl;
MediaType mediaType = MediaType.fromString(media.getType());
String foreignKey = media.getForeignKey();
String keyForUrl = foreignKey;

/* Build the link of the remote image based on foreign key */
switch (mediaType) {
case CD_PHOTO_THREAD:
JsonObject details = media.getDetailsJson();
imageUrl = String.format(mediaType.getImageUrlPattern(),
details.get("image_partial").getAsString()
.replace("_l.jpg", "_m.jpg"));
break;
case YOUTUBE:
/* Need to account for timestamps in youtube foreign key
* Can be like <key>?start=1h15m3s or <key>?t=time or <key>#t=time
* Since foreign key is first param in yt.com/watch?v=blah, others need to be &
*/
keyForUrl = foreignKey.replace('?', '&').replace('#', '&');
Matcher m = YOUTUBE_KEY_PATTERN.matcher(foreignKey);
String cleanKey = m.find() ? m.group(1) : foreignKey;
imageUrl = String.format(mediaType.getImageUrlPattern(), cleanKey);
break;
case IMGUR:
imageUrl = String.format(mediaType.getImageUrlPattern(), foreignKey);
break;
default:
imageUrl = "";
}
Boolean isVideo = mediaType == MediaType.YOUTUBE;
String linkUrl = String.format(mediaType.getLinkUrlPattern(), keyForUrl);
return new ImageListElement(imageUrl, linkUrl, isVideo);
return new ImageListElement(media.getDirectUrl(), media.getViewUrl(), mediaType.isVideo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,6 @@ public String toString() {
return "";
}

public String getImageUrlPattern() {
switch (this) {
case CD_PHOTO_THREAD:
return "https://www.chiefdelphi.com/media/img/%s";
case YOUTUBE:
return "https://img.youtube.com/vi/%s/hqdefault.jpg";
case IMGUR:
return "https://i.imgur.com/%sl.jpg";
default:
return "";
}
}

public String getLinkUrlPattern() {
switch (this) {
case CD_PHOTO_THREAD:
return "https://www.chiefdelphi.com/media/photos/%s";
case YOUTUBE:
return "https://www.youtube.com/watch?v=%s";
case IMGUR:
return "https://imgur.com/%s";
default:
return "";
}
}

public boolean isImage() {
return this != NONE && this != YOUTUBE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.thebluealliance.androidclient.database.tables.EventsTable;
import com.thebluealliance.androidclient.database.tables.FavoritesTable;
import com.thebluealliance.androidclient.database.tables.MatchesTable;
import com.thebluealliance.androidclient.database.tables.MediasTable;
import com.thebluealliance.androidclient.database.tables.SubscriptionsTable;
import com.thebluealliance.androidclient.database.tables.TeamsTable;

Expand Down Expand Up @@ -214,4 +215,19 @@ public void testUpdateTo33() {
mDbHelper.onUpgrade(mDb, 32, 33);
assertTrue(mDbHelper.columnExists(mDb, TABLE_EVENTS, EventsTable.CITY));
}

@Test
public void testUpdateTo36() {
mDb.execSQL(String.format(BASE_TABLE_CREATE, TABLE_MEDIAS));
assertFalse(mDbHelper.columnExists(mDb, TABLE_MEDIAS, MediasTable.DIRECT_URL));
assertFalse(mDbHelper.columnExists(mDb, TABLE_MEDIAS, MediasTable.VIEW_URL));
mDbHelper.onUpgrade(mDb, 35, 36);
assertTrue(mDbHelper.columnExists(mDb, TABLE_MEDIAS, MediasTable.DIRECT_URL));
assertTrue(mDbHelper.columnExists(mDb, TABLE_MEDIAS, MediasTable.VIEW_URL));
}

@Test
public void testTeamQueryWithDoubleQuote() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,8 @@ public void testRenderFromModel() {
ImageListElement listItem = mRenderer.renderFromModel(mMedia, null);
assertNotNull(listItem);
assertEquals(listItem.isVideo, mMediaType.isVideo());
assertEquals(
listItem.linkUrl,
String.format(mMediaType.getLinkUrlPattern(), mMedia.getForeignKey()));
if (mMediaType != MediaType.CD_PHOTO_THREAD) {
assertEquals(
listItem.imageUrl,
String.format(mMediaType.getImageUrlPattern(), mMedia.getForeignKey()));
} else {
assertEquals(listItem.imageUrl, "https://www.chiefdelphi"
+ ".com/media/img/fe3/fe38d320428adf4f51ac969efb3db32c_m.jpg");
}
assertEquals(listItem.linkUrl, mMedia.getViewUrl());
assertEquals(listItem.imageUrl, mMedia.getDirectUrl());
}

}
4 changes: 3 additions & 1 deletion android/src/test/resources/media_cdphotothread.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"details": {
"image_partial": "fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"
},
"foreign_key": "39894"
"foreign_key": "39894",
"direct_url": "https://web.archive.org/web/https://www.chiefdelphi.com/media/img/fe3/fe38d320428adf4f51ac969efb3db32c_m.jpg",
"view_url": "https://web.archive.org/web/https://www.chiefdelphi.com/media/img/fe3/fe38d320428adf4f51ac969efb3db32c_l.jpg"
}
4 changes: 3 additions & 1 deletion android/src/test/resources/media_imgur.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"details": {},
"foreign_key": "aF8T5ZE",
"type": "imgur"
"type": "imgur",
"view_url": "http://imgur.com/img123",
"direct_url": "http://imgur.com/img123.jpg"
}
4 changes: 3 additions & 1 deletion android/src/test/resources/media_youtube.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"type": "youtube",
"details": {},
"foreign_key": "RpSgUrsghv4"
"foreign_key": "RpSgUrsghv4",
"view_url": "https://youtu.be/RpSgUrsghv4",
"direct_url": "http://img.youtube.com/vi/RpSgUrsghv4/hqdefault.jpg"
}
4 changes: 3 additions & 1 deletion doc/api/Media.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Name | Type | Description | Notes
**foreignKey** | **String** | The key used to indentify this media element on the remote site (e.g YouTube video key) |
**lastModified** | **Long** | Timestamp this model was last modified | [optional]
**preferred** | **Boolean** | Is this a high quality robot picture |
**type** | **String** | The string type of the media element |
**type** | **String** | The string type of the media element |
**direct_url** | **String** | The direct URL to the media |
**view_url** | **String** | The url to view the media on a web page |