Skip to content

Instantly share code, notes, and snippets.

@nicolausYes
Created October 4, 2018 14:22
Show Gist options
  • Save nicolausYes/37189184a3ff8deae6c4af52bdb7fa04 to your computer and use it in GitHub Desktop.
Save nicolausYes/37189184a3ff8deae6c4af52bdb7fa04 to your computer and use it in GitHub Desktop.
Parse genres/subgenres from ITunes
final StringBuilder builder = new StringBuilder();
final String pattern = "([\\d])\\w+";
Pattern compiledPattern = Pattern.compile(pattern);
try {
Document doc = Jsoup.connect("https://itunes.apple.com/us/genre/podcasts/id26?mt=2").get();
Elements links = doc.select("a.top-level-genre[href],.top-level-subgenres a[href]");
for (Element link : links) {
String url = link.attr("href");
String text = link.text();
String id = "";
String enumName = text.replace(" ", "_")
.replace("-", "_")
.replace("&", "and")
.toUpperCase();;
String result = "";
if (link.hasClass("top-level-genre"))
result += "\n";
Matcher m = compiledPattern.matcher(url);
if (m.find())
id = m.group(0);
result += String.format(Locale.ENGLISH, "%s(%s, \"%s\"),\n",
enumName, id, text);
builder.append(result);
}
} catch (IOException e) {
builder.append("Error : ").append(e.getMessage()).append("\n");
}
Log.d("TAG", builder.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment