Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.shahnami.fetch.Controller; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.model.ResourceId; import com.google.api.services.youtube.model.SearchListResponse; import com.google.api.services.youtube.model.SearchResult; import com.google.api.services.youtube.model.Thumbnail; import com.shahnami.fetch.Model.Song; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.JSONException; import org.json.JSONObject; /** * * @author nami */ public class FetchMusic { List<Song> _songs; static YouTube youtube; static final long NUMBER_OF_VIDEOS_RETURNED = 50; int RECURSE; public FetchMusic() { _songs = new ArrayList<>(); RECURSE = 0; } public boolean isYTLink(String query) { String pattern = "https?:\\/\\/(?:[0-9A-Z-]+\\.)?(?:youtu\\.be\\/|youtube\\.com\\S*[^\\w\\-\\s])([\\w\\-]{11})(?=[^\\w\\-]|$)(?![?=&+%\\w]*(?:['\"][^<>]*>|<\\/a>))[?=&+%\\w]*"; Pattern compiledPattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE); Matcher matcher = compiledPattern.matcher(query); while (matcher.matches()) { System.out.println(matcher.group()); return true; } return false; } public List<Song> getSongs(String query) { try { youtube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) throws IOException { } }).setApplicationName("Fetch").build(); YouTube.Search.List search = youtube.search().list("id,snippet"); String apiKey = "AIzaSyBgHBggmLq7Zr5HWk0-gL8SyLDPZl6m6gQ"; search.setKey(apiKey); search.setQ(query); search.setType("video"); search.setFields( "items(id/kind,id/videoId,snippet/title,snippet/channelTitle,snippet/publishedAt,snippet/thumbnails/default/url)"); search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED); // Call the API and print results. SearchListResponse searchResponse = search.execute(); List<SearchResult> searchResultList = searchResponse.getItems(); Iterator<SearchResult> it = searchResultList.iterator(); if (!it.hasNext()) { System.out.println(" There aren't any results for your query."); } while (it.hasNext()) { SearchResult singleVideo = it.next(); ResourceId rId = singleVideo.getId(); if (rId.getKind().equals("youtube#video")) { Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault(); Song s = new Song(); s.setTitle(singleVideo.getSnippet().getTitle()); s.setThumbnail(thumbnail.getUrl()); s.setLink("https://www.youtube.com/watch?v=" + rId.getVideoId()); s.setAuthor(singleVideo.getSnippet().getChannelTitle()); s.setPubDate(singleVideo.getSnippet().getPublishedAt().toString().split("T")[0]); _songs.add(s); } } } catch (IOException ex) { Logger.getLogger(FetchMusic.class.getName()).log(Level.SEVERE, null, ex); } return _songs; } public String getDownloadLink(String link) { String downloadUrl = ""; try { JSONObject json = JsonReader .readJsonFromUrl("http://youtubeinmp3.com/fetch/?&format=JSON&video=" + link); downloadUrl = json.getString("link"); } catch (IOException | JSONException ex) { RECURSE++; if (RECURSE < 5) return getDownloadLink(link); else Logger.getLogger(FetchMusic.class.getName()).log(Level.SEVERE, null, ex); } return downloadUrl; } }