-
Notifications
You must be signed in to change notification settings - Fork 2
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
Backend for Heat Map #75
base: master
Are you sure you want to change the base?
Changes from all commits
60f0682
cc80ea3
d3dfb85
9c7c25a
c9f12b4
b1f603e
096bd60
0866497
4734a3e
2ea7481
44c8c52
41b45e0
f024c16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.Gson; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.lang.Math; | ||
|
@@ -22,6 +23,9 @@ public class YoutubeGenres { | |
public HashMap<String, Integer> genreData = new HashMap<String, Integer>(); | ||
public int totalMusic = 0; | ||
public int maxGenreCount = 0; | ||
// element of value x means xth latest video is music | ||
// needed for heat map | ||
public ArrayList<Integer> likedMusicHistory = new ArrayList<Integer>(); | ||
|
||
public YoutubeGenres() { | ||
|
||
|
@@ -31,9 +35,12 @@ public YoutubeGenres() { | |
* parses through youtube liked videos json array, | ||
* updates hash map to contain frequency count of each music genre | ||
* @param videos json array of youtube liked videos | ||
* @param firstVideoCount the number of videos retrieved before this http call | ||
* @return number of videos retrieved in this call | ||
*/ | ||
protected void calculateMusicCount(JsonArray videos) { | ||
for (int i = 0; i < videos.size(); i++) { | ||
protected int calculateMusicCount(JsonArray videos, int firstVideoCount) { | ||
int videosSize = videos.size(); | ||
for (int i = 0; i < videosSize; i++) { | ||
JsonObject video = videos.get(i).getAsJsonObject(); | ||
JsonObject topicDetails = video.getAsJsonObject("topicDetails"); | ||
|
||
|
@@ -68,12 +75,17 @@ protected void calculateMusicCount(JsonArray videos) { | |
} | ||
} | ||
|
||
if (isMusic && totalSubgenres == 0) { | ||
if (isMusic) { | ||
// likedMusicHistory records video numbers that are music | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this comment, you already have a comment explaining what |
||
likedMusicHistory.add(firstVideoCount + i); | ||
|
||
if (totalSubgenres == 0) { | ||
// video only classified as Music so we update as "Other music" | ||
this.updateGenre("Other music"); | ||
this.updateGenre("Other music"); | ||
} | ||
} | ||
} | ||
return; | ||
return videosSize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can get this info from the parent function. No need to return it here, and that in fact obscures what this function is doing, since it analyzes the videos but also returns the total number. Someone interacting with this function will likely write:
This means 2 independent things are happening on one line that aren't strictly tied together. More clear is:
|
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,9 +96,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) | |
JsonObject likedVideoRes; | ||
JsonArray videos; | ||
YoutubeGenres genreAnalysis = new YoutubeGenres(); | ||
// needed to keep track of likedMusicHistory for heat map | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment doesn't really make sense to me, since it's not "keeping track" of |
||
int videosRetrieved = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider renaming to |
||
|
||
// next Page Token must be an empty string for first http call | ||
String nextPageToken = ""; | ||
// Make multiple paginated calls to youtube API. | ||
// Each call has a new page token | ||
while (nextPageToken != null) { | ||
youtubeResBody = getYoutubeRes(API_KEY, | ||
accessToken.toString(), | ||
|
@@ -111,7 +115,9 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) | |
} | ||
|
||
videos = likedVideoRes.getAsJsonArray("items"); | ||
genreAnalysis.calculateMusicCount(videos); | ||
// videosRetrieved keeps track of music video order in genreAnalysis | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this comment since you already have a comment defining what this variable is when you initiated it |
||
videosRetrieved += genreAnalysis | ||
.calculateMusicCount(videos, videosRetrieved); | ||
Comment on lines
+119
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. This should be split into 2 separate lines. |
||
|
||
nextPageToken = getNextPageToken(likedVideoRes); | ||
} | ||
|
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.
consider renaming
firstVideoCount
toinitialNumVideos
.firstVideoCount
sounds like it could be the index of the first video, or some count associated with the first video in the listThere 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.
+1. Probably something like "currentVideoIndex" will be clearest