Advertisement
_Tobias

AudioAddict listen key validator

Aug 14th, 2013
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.65 KB | None | 0 0
  1. package tobiass.audioaddict.api;
  2.  
  3. import java.net.HttpURLConnection;
  4. import java.net.Proxy;
  5. import java.net.URL;
  6. import java.util.Random;
  7.  
  8. /**
  9.  * ListenKeyValRequest.java
  10.  * @author _Tobias
  11.  */
  12.  
  13. public class ListenKeyValRequest {
  14.     private String key = null;
  15.     private boolean async = true;
  16.     private onFinishListener mFinishListener = null;
  17.     private Proxy mProxy = null;
  18.     private Random mRandom = new Random();
  19.    
  20.     public ListenKeyValRequest(String listenkey) {
  21.         key = listenkey;
  22.     }
  23.    
  24.     public ListenKeyValRequest setAsync(boolean arg) {
  25.         async = arg;
  26.         return this;
  27.     }
  28.    
  29.     public ListenKeyValRequest setListenKey(String arg) {
  30.         key = arg;
  31.         return this;
  32.     }
  33.    
  34.     public ListenKeyValRequest setProxy(Proxy arg) {
  35.         mProxy = arg;
  36.         return this;
  37.     }
  38.    
  39.     public ListenKeyValRequest setOnFinishListener(onFinishListener arg) {
  40.         mFinishListener = arg;
  41.         return this;
  42.     }
  43.    
  44.     public ListenKeyValRequest start() {
  45.         if(key == null) {
  46.             return this;
  47.         }
  48.         Runnable r = new Runnable() {
  49.             public void run() {
  50.                 int res = getStatusCode(getStreamUrl()+"?"+key);
  51.                 if(res == -1) {
  52.                     callFinish(KeyState.FAILURE);
  53.                 }
  54.                 else if(res == 200) {
  55.                     callFinish(KeyState.PREMIUM);
  56.                 }
  57.                 else if(res == 401) {
  58.                     res = getStatusCode("http://listen.di.fm/premium/favorites?"+key);
  59.                     if(res == 200) {
  60.                         callFinish(KeyState.FREE);
  61.                     }
  62.                     else if(res == 403) {
  63.                         callFinish(KeyState.INVALID);
  64.                     }
  65.                     else if(res == -1) {
  66.                         callFinish(KeyState.FAILURE);
  67.                     }
  68.                 }
  69.             }
  70.         };
  71.         if(async) {
  72.             new Thread(r).start();
  73.         }
  74.         else {
  75.             r.run();
  76.         }
  77.         return this;
  78.     }
  79.    
  80.     private int getStatusCode(String url) {
  81.         try {
  82.             HttpURLConnection conn;
  83.             if(mProxy != null) {
  84.                 conn = (HttpURLConnection) new URL(url).openConnection(mProxy);
  85.             }
  86.             else {
  87.                 conn = (HttpURLConnection) new URL(url).openConnection();
  88.             }
  89.             int code = conn.getResponseCode();
  90.             conn.disconnect();
  91.             return code;
  92.         } catch (Exception e) {
  93.             return -1;
  94.         }
  95.     }
  96.    
  97.     private String getStreamUrl() {
  98.         String[] urls = {"http://prem2.di.fm:80/chillhop","http://prem4.di.fm:80/chillhop","http://prem1.di.fm:80/chillhop","http://prem2.di.fm:80/downtempolounge","http://prem1.di.fm:80/downtempolounge","http://prem4.di.fm:80/downtempolounge","http://prem2.di.fm:80/trap","http://prem1.di.fm:80/trap","http://prem4.di.fm:80/trap","http://prem2.di.fm:80/sankeys","http://prem4.di.fm:80/sankeys","http://prem1.di.fm:80/sankeys","http://prem2.di.fm:80/trance","http://prem1.di.fm:80/trance","http://prem4.di.fm:80/trance","http://prem2.di.fm:80/vocaltrance","http://prem1.di.fm:80/vocaltrance","http://prem4.di.fm:80/vocaltrance","http://prem2.di.fm:80/lounge","http://prem1.di.fm:80/lounge","http://prem4.di.fm:80/lounge","http://prem2.di.fm:80/chillout","http://prem1.di.fm:80/chillout","http://prem4.di.fm:80/chillout","http://prem2.di.fm:80/vocalchillout","http://prem1.di.fm:80/vocalchillout","http://prem4.di.fm:80/vocalchillout","http://prem2.di.fm:80/progressive","http://prem1.di.fm:80/progressive","http://prem4.di.fm:80/progressive","http://prem2.di.fm:80/house","http://prem1.di.fm:80/house","http://prem4.di.fm:80/house","http://prem2.di.fm:80/mainstage","http://prem1.di.fm:80/mainstage","http://prem4.di.fm:80/mainstage","http://prem2.di.fm:80/umfradio","http://prem4.di.fm:80/umfradio","http://prem1.di.fm:80/umfradio","http://prem2.di.fm:80/minimal","http://prem1.di.fm:80/minimal","http://prem4.di.fm:80/minimal","http://prem2.di.fm:80/harddance","http://prem1.di.fm:80/harddance","http://prem4.di.fm:80/harddance","http://prem2.di.fm:80/eurodance","http://prem1.di.fm:80/eurodance","http://prem4.di.fm:80/eurodance","http://prem2.di.fm:80/techhouse","http://prem1.di.fm:80/techhouse","http://prem4.di.fm:80/techhouse","http://prem2.di.fm:80/electrohouse","http://prem1.di.fm:80/electrohouse","http://prem4.di.fm:80/electrohouse","http://prem2.di.fm:80/bigroomhouse","http://prem4.di.fm:80/bigroomhouse","http://prem1.di.fm:80/bigroomhouse","http://prem2.di.fm:80/psychill","http://prem1.di.fm:80/psychill","http://prem4.di.fm:80/psychill","http://prem2.di.fm:80/goapsy","http://prem1.di.fm:80/goapsy","http://prem4.di.fm:80/goapsy","http://prem2.di.fm:80/progressivepsy","http://prem4.di.fm:80/progressivepsy","http://prem1.di.fm:80/progressivepsy","http://prem2.di.fm:80/hardcore","http://prem1.di.fm:80/hardcore","http://prem4.di.fm:80/hardcore","http://prem2.di.fm:80/djmixes","http://prem1.di.fm:80/djmixes","http://prem4.di.fm:80/djmixes","http://prem2.di.fm:80/russianclubhits","http://prem1.di.fm:80/russianclubhits","http://prem4.di.fm:80/russianclubhits","http://prem2.di.fm:80/ambient","http://prem1.di.fm:80/ambient","http://prem4.di.fm:80/ambient","http://prem2.di.fm:80/drumandbass","http://prem1.di.fm:80/drumandbass","http://prem4.di.fm:80/drumandbass","http://prem2.di.fm:80/eclectronica","http://prem1.di.fm:80/eclectronica","http://prem4.di.fm:80/eclectronica","http://prem2.di.fm:80/classicelectronica","http://prem1.di.fm:80/classicelectronica","http://prem4.di.fm:80/classicelectronica","http://prem2.di.fm:80/epictrance","http://prem4.di.fm:80/epictrance","http://prem1.di.fm:80/epictrance","http://prem2.di.fm:80/ukgarage","http://prem4.di.fm:80/ukgarage","http://prem1.di.fm:80/ukgarage","http://prem2.di.fm:80/breaks","http://prem1.di.fm:80/breaks","http://prem4.di.fm:80/breaks","http://prem2.di.fm:80/cosmicdowntempo","http://prem1.di.fm:80/cosmicdowntempo","http://prem4.di.fm:80/cosmicdowntempo","http://prem2.di.fm:80/techno","http://prem1.di.fm:80/techno","http://prem4.di.fm:80/techno","http://prem2.di.fm:80/soulfulhouse","http://prem1.di.fm:80/soulfulhouse","http://prem4.di.fm:80/soulfulhouse","http://prem2.di.fm:80/deephouse","http://prem4.di.fm:80/deephouse","http://prem1.di.fm:80/deephouse","http://prem2.di.fm:80/deeptech","http://prem1.di.fm:80/deeptech","http://prem4.di.fm:80/deeptech","http://prem2.di.fm:80/tribalhouse","http://prem1.di.fm:80/tribalhouse","http://prem4.di.fm:80/tribalhouse","http://prem2.di.fm:80/funkyhouse","http://prem1.di.fm:80/funkyhouse","http://prem4.di.fm:80/funkyhouse","http://prem2.di.fm:80/deepnudisco","http://prem1.di.fm:80/deepnudisco","http://prem4.di.fm:80/deepnudisco","http://prem2.di.fm:80/spacemusic","http://prem1.di.fm:80/spacemusic","http://prem4.di.fm:80/spacemusic","http://prem2.di.fm:80/hardstyle","http://prem1.di.fm:80/hardstyle","http://prem4.di.fm:80/hardstyle","http://prem2.di.fm:80/chilloutdreams","http://prem1.di.fm:80/chilloutdreams","http://prem4.di.fm:80/chilloutdreams","http://prem2.di.fm:80/liquiddnb","http://prem1.di.fm:80/liquiddnb","http://prem4.di.fm:80/liquiddnb","http://prem2.di.fm:80/darkdnb","http://prem4.di.fm:80/darkdnb","http://prem1.di.fm:80/darkdnb","http://prem2.di.fm:80/classiceurodance","http://prem1.di.fm:80/classiceurodance","http://prem4.di.fm:80/classiceurodance","http://prem2.di.fm:80/handsup","http://prem1.di.fm:80/handsup","http://prem4.di.fm:80/handsup","http://prem2.di.fm:80/club","http://prem1.di.fm:80/club","http://prem4.di.fm:80/club","http://prem2.di.fm:80/classictrance","http://prem1.di.fm:80/classictrance","http://prem4.di.fm:80/classictrance","http://prem2.di.fm:80/classicvocaltrance","http://prem1.di.fm:80/classicvocaltrance","http://prem4.di.fm:80/classicvocaltrance","http://prem2.di.fm:80/clubdubstep","http://prem4.di.fm:80/clubdubstep","http://prem1.di.fm:80/clubdubstep","http://prem2.di.fm:80/dubstep","http://prem1.di.fm:80/dubstep","http://prem4.di.fm:80/dubstep","http://prem2.di.fm:80/liquiddubstep","http://prem1.di.fm:80/liquiddubstep","http://prem4.di.fm:80/liquiddubstep","http://prem2.di.fm:80/glitchhop","http://prem1.di.fm:80/glitchhop","http://prem4.di.fm:80/glitchhop","http://prem2.di.fm:80/discohouse","http://prem1.di.fm:80/discohouse","http://prem4.di.fm:80/discohouse","http://prem2.di.fm:80/classiceurodisco","http://prem1.di.fm:80/classiceurodisco","http://prem4.di.fm:80/classiceurodisco","http://prem2.di.fm:80/futuresynthpop","http://prem1.di.fm:80/futuresynthpop","http://prem4.di.fm:80/futuresynthpop","http://prem2.di.fm:80/latinhouse","http://prem1.di.fm:80/latinhouse","http://prem4.di.fm:80/latinhouse","http://prem2.di.fm:80/oldschoolacid","http://prem1.di.fm:80/oldschoolacid","http://prem4.di.fm:80/oldschoolacid","http://prem2.di.fm:80/chiptunes","http://prem1.di.fm:80/chiptunes","http://prem4.di.fm:80/chiptunes"};
  99.         return urls[getRandomBetween(0, urls.length-1, mRandom)];
  100.     }
  101.    
  102.     private int getRandomBetween(int a, int b, Random r) {
  103.         return a+r.nextInt(b-a+1);
  104.     }
  105.    
  106.     private void callFinish(KeyState arg) {
  107.         if(mFinishListener != null) {
  108.             mFinishListener.on(arg);
  109.         }
  110.     }
  111.    
  112.     public interface onFinishListener {
  113.         public void on(KeyState arg);
  114.     }
  115.    
  116.     public enum KeyState {
  117.         PREMIUM, FREE, INVALID, FAILURE
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement