Last active
August 29, 2015 13:59
-
-
Save kvzhuang/10717596 to your computer and use it in GitHub Desktop.
DailyMotion Video URL in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.net.URLConnection; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLConnection; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.util.Map; | |
import java.util.LinkedHashMap; | |
import java.net.URLDecoder; | |
public class dm { | |
private static final String PARAMETER_SEPARATOR = "&"; | |
private static final String NAME_VALUE_SEPARATOR = "="; | |
public static void main(String args[]) { | |
BufferedReader reader = null; | |
try { | |
URL url = new URL("http://www.dailymotion.com/video/xf02xp_uffie-difficult_music"); | |
URLConnection hc = url.openConnection(); | |
String myCookie = "family_filter=off;"; | |
hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); | |
hc.setRequestProperty("Cookie", myCookie); | |
String pattern = "\"flashvars\"\\svalue=\"(.*?)\""; | |
Pattern r = Pattern.compile(pattern); | |
String pattern2 = "video_url\":\"(.*?)\""; | |
Pattern r2 = Pattern.compile(pattern2); | |
String pattern3 = "sdURL\":\"(.*?)\""; | |
Pattern r3 = Pattern.compile(pattern3); | |
reader = new BufferedReader(new InputStreamReader(hc.getInputStream(), "UTF-8")); | |
String content = ""; | |
for (String line; (line = reader.readLine()) != null;) { | |
content += line; | |
} | |
Matcher m = r.matcher(content); | |
if (m.find()) { | |
String mString = m.group(0); | |
mString = mString.substring("\"flashvars\" value=\"".length(), mString.length()-1 ); | |
mString = URLDecoder.decode(mString, "UTF-8"); | |
String[] pairs = mString.split("&"); | |
String videoUrl = ""; | |
for(int c = 0; c< pairs.length; c++) { | |
Matcher m2 = r2.matcher(pairs[c]); | |
if (m2.find()) { | |
String vString = m2.group(0); | |
videoUrl = vString.substring("video_url\":\"".length(), vString.length()-1); | |
break; | |
} | |
Matcher m3 = r3.matcher(pairs[c]); | |
if (m3.find()) { | |
String vString = m3.group(0); | |
videoUrl = vString.substring("sdURL\":\"".length(), vString.length()-1); | |
} | |
} | |
System.out.println("\n" + URLDecoder.decode(videoUrl, "UTF-8")); | |
} else { | |
System.out.println("NO MATCH"); | |
} | |
} catch (MalformedURLException e){ | |
System.out.println(e); | |
} catch (UnsupportedEncodingException e) { | |
System.out.println(e); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
finally { | |
if (reader != null) try { reader.close(); } catch (IOException ignore) {} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment