package com.sferadev.danacast.servers;

import android.os.SystemClock;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Streamcloud {
    public String getVideoPath(String url) {
        Pattern pattern = Pattern.compile("http:\\/\\/streamcloud\\.eu\\/(.*)\\/(.*)\\.html");
        Matcher matcher = pattern.matcher(url);
        if (matcher.find() && matcher.groupCount() == 2) {
            String link = parseLink(url, matcher.group(1), matcher.group(2));
            if (link != null) return link; //1st Attempt
            SystemClock.sleep(11 * 1000);
            link = parseLink(url, matcher.group(1), matcher.group(2)); //2nd Attempt
            return link;
        }
        return null;
    }

    private String parseLink(String url, String fileId, String fileName){
        try {
            Document doc = Jsoup.connect(url)
                    .data("op", "download1")
                    .data("id", fileId)
                    .data("fname", fileName)
                    .data("imhuman", "Watch+video+now")
                    .data("usr_login", "")
                    .data("referer", "")
                    .data("hash", "")
                    .timeout(3000)
                    .post();
            Pattern pattern = Pattern.compile("file: \\\"(.*)\\/video\\.mp4\\\",");
            Matcher matcher = pattern.matcher(doc.html());
            if (matcher.find()) return matcher.group(1) + "/video.mp4";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -254,16 +254,23 @@
  public 
+static 
 String g
@@ -604,16 +604,34 @@
 != null)
+ %7B%0A               
  return 
@@ -642,32 +642,46 @@
 ; //1st Attempt%0A
+            %7D%0A
             Syst
@@ -866,16 +866,23 @@
 private 
+static 
 String p
@@ -890,16 +890,22 @@
 rseLink(
+final 
 String u
@@ -907,16 +907,22 @@
 ing url,
+ final
  String 
@@ -928,16 +928,22 @@
  fileId,
+ final
  String 
@@ -951,18 +951,175 @@
 ileName)
+ 
 %7B%0A
+        final String%5B%5D response = %7Bnull%7D;%0A        Thread thread = new Thread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         
@@ -1136,16 +1136,24 @@
         
+ 
+       
 Document
@@ -1194,24 +1194,32 @@
             
+        
 .data(%22op%22, 
@@ -1243,32 +1243,40 @@
                 
+   
+     
 .data(%22id%22, file
@@ -1279,16 +1279,24 @@
 fileId)%0A
+        
         
@@ -1344,32 +1344,40 @@
                 
+     
+   
 .data(%22imhuman%22,
@@ -1412,24 +1412,32 @@
             
+        
 .data(%22usr_l
@@ -1443,24 +1443,32 @@
 login%22, %22%22)%0A
+        
             
@@ -1512,24 +1512,32 @@
             
+        
 .data(%22hash%22
@@ -1562,16 +1562,24 @@
         
+        
 .timeout
@@ -1605,16 +1605,24 @@
         
+  
+      
 .post();
@@ -1618,24 +1618,32 @@
    .post();%0A
+        
             
@@ -1718,32 +1718,40 @@
 %22);%0A            
+    
+    
 Matcher matcher 
@@ -1785,32 +1785,40 @@
 ));%0A            
+        
 if (matcher.find
@@ -1821,22 +1821,55 @@
 find()) 
-return
+%7B%0A                        response%5B0%5D =
  matcher
@@ -1894,16 +1894,46 @@
 o.mp4%22;%0A
+                    %7D%0A        
         
@@ -1970,16 +1970,24 @@
         
+ 
+       
 e.printS
@@ -1999,34 +1999,245 @@
 race();%0A        
+  
+      
 %7D%0A
+            %7D%0A        %7D);%0A        thread.start();%0A        try %7B%0A            thread.join();%0A            return response%5B0%5D;%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A    
         return n
@@ -2237,20 +2237,30 @@
 eturn null;%0A
+        %7D%0A
     %7D%0A%7D%0A

Make Streamcloud to work