Advertisement
Ham62

LastfmArtistImages.java

Dec 19th, 2019
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.Scanner;
  4. import java.nio.charset.StandardCharsets;
  5.  
  6. public class getURL {
  7.   public static void main(String[] args) {
  8.     String artistOrMbid = "";
  9.    
  10.     System.out.print("Enter artist name>");
  11.     Scanner input = new Scanner(System.in);
  12.     artistOrMbid = input.nextLine();
  13.     input.close();
  14.    
  15.     // Pull the artist image url from the last.fm artist page
  16.     String sImageURL = "", sArtistURL = "";
  17.     try {
  18.       sArtistURL = "https://www.last.fm/music/"+URLEncoder.encode(artistOrMbid, StandardCharsets.UTF_8.toString());
  19.     } catch (UnsupportedEncodingException e) {
  20.       // This should never happen for us
  21.     }
  22.     String sHTML = getWebPage(sArtistURL);
  23.     if (sHTML != null) {
  24.       int iImgUrlIndex = sHTML.indexOf("+images/");
  25.       if (iImgUrlIndex > 0) {
  26.         int iStartTrim = iImgUrlIndex+8; // ("+images/").length = 8
  27.         int iEndTrim = sHTML.indexOf("\"", iStartTrim);
  28.         String sImageID = sHTML.substring(iStartTrim, iEndTrim);
  29.         sImageURL = "https://lastfm.freetls.fastly.net/i/u/770x0/"+
  30.           sImageID+".jpg";
  31.       }
  32.     }
  33.    
  34.     System.out.println(sImageURL);
  35.   }
  36.  
  37.   // Pull an html page and return HTML in string
  38.   private static String getWebPage(String sURL) {
  39.     HttpURLConnection connection;
  40.     int iResponse;
  41.    
  42.     try {
  43.       URL requestURL = new URL(sURL);
  44.       connection = (HttpURLConnection)requestURL.openConnection();
  45.       connection.setRequestMethod("GET");
  46.       iResponse = connection.getResponseCode();
  47.     } catch (Exception e) {
  48.       if (e instanceof MalformedURLException) {
  49.         System.out.println(e.getMessage());
  50.       } else if (e instanceof UnknownHostException) {
  51.         System.out.println("Unknown host: "+e.getMessage());
  52.       } else if (e instanceof ConnectException) {
  53.         System.out.println(e.getMessage());
  54.       } else {
  55.         System.out.println(e);
  56.       }
  57.       return null;
  58.     }
  59.    
  60.     // Non-200 response means we did something wrong
  61.     if (iResponse != 200) {
  62.       System.out.println("Server returned error code: "+iResponse);
  63.       return null;
  64.     }
  65.    
  66.     // Constructed string from HTTP stream
  67.     StringBuffer response = new StringBuffer();
  68.    
  69.     try {
  70.       // Download the response from the server
  71.       InputStream stream = connection.getInputStream();
  72.       BufferedReader in = new BufferedReader(new InputStreamReader(stream));
  73.      
  74.       String readLine;
  75.       while ((readLine = in.readLine()) != null)
  76.         response.append(readLine);
  77.      
  78.       in.close();
  79.     } catch (Exception e) {
  80.       System.out.println("Error downloading responce from server!");
  81.     }
  82.    
  83.     return response.toString();
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement