Created
December 19, 2013 03:31
-
-
Save lzsucceed/8034020 to your computer and use it in GitHub Desktop.
This file contains 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.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.net.URLDecoder; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.Header; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.ProtocolException; | |
import org.apache.http.client.CookieStore; | |
import org.apache.http.client.config.RequestConfig; | |
import org.apache.http.client.config.RequestConfig.Builder; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpHead; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.protocol.HttpClientContext; | |
import org.apache.http.cookie.Cookie; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.DefaultRedirectStrategy; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.protocol.HttpContext; | |
public class NicoGet { | |
protected String finalLocation; | |
NicoGet() throws Exception { | |
HttpClientBuilder builder = HttpClients.custom(); | |
String finalLocation = null; | |
builder.setRedirectStrategy(new DefaultRedirectStrategy() { | |
@Override | |
public URI getLocationURI(HttpRequest arg0, HttpResponse arg1, | |
HttpContext arg2) throws ProtocolException { | |
URI localUri = null; | |
try { | |
localUri = super.getLocationURI(arg0, arg1, arg2); | |
} catch (Exception e) { | |
} finally { | |
if (arg1.getFirstHeader("Location") != null) { | |
NicoGet.this.finalLocation = localUri.toString(); | |
} | |
} | |
if (localUri == null) { | |
try { | |
return new URI(NicoGet.this.finalLocation); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
return localUri; | |
} | |
@Override | |
public boolean isRedirected(HttpRequest request, | |
HttpResponse response, HttpContext context) | |
throws ProtocolException { | |
int code = response.getStatusLine().getStatusCode(); | |
System.out.println("code:" + code); | |
if (code == 200 || code == 206) { | |
return false; | |
} | |
return true; | |
} | |
}); | |
CloseableHttpClient httpclient = builder.build(); | |
HttpPost post = new HttpPost( | |
"https://secure.nicovideo.jp/secure/login?site=niconico"); | |
List<NameValuePair> paramsList = new ArrayList<NameValuePair>(); | |
paramsList.add(new BasicNameValuePair("mail", "YOURMAILADDRESS")); | |
paramsList.add(new BasicNameValuePair("password", "YOURPASSWORD")); | |
HttpEntity entity = new UrlEncodedFormEntity(paramsList, "UTF-8"); | |
post.setEntity(entity); | |
HttpClientContext context = HttpClientContext.create(); | |
CloseableHttpResponse response = httpclient.execute(post, context); | |
int httpStatus = response.getStatusLine().getStatusCode(); | |
if (httpStatus != 200) { | |
throw new Exception(); | |
} | |
HttpEntity responseEntity = response.getEntity(); | |
if (responseEntity.getContentLength() != -1) { | |
long length = responseEntity.getContentLength(); | |
} | |
BufferedReader stream = new BufferedReader(new InputStreamReader( | |
responseEntity.getContent())); | |
StringBuilder content = new StringBuilder(); | |
char[] contentChank = new char[4096]; | |
int i = 0; | |
int readByte = 0; | |
while (i < (readByte = stream | |
.read(contentChank, 0, contentChank.length))) { | |
content.append(String.valueOf(contentChank, 0, readByte)); | |
} | |
stream.close(); | |
response.close(); | |
SessionHolder holder = new SessionHolder(); | |
CookieStore store = context.getCookieStore(); | |
boolean hasSession = false; | |
for (Cookie cookie : store.getCookies()) { | |
if (cookie.getName().equalsIgnoreCase("user_session") | |
&& (!cookie.getValue().equals("deleted"))) { | |
holder.user_session = cookie.getValue(); | |
holder.cookie = cookie; | |
hasSession = true; | |
break; | |
} | |
} | |
if (!hasSession) { | |
throw new Exception(); | |
} | |
Header hd1 = response.getFirstHeader("x-niconico-id"); | |
if (hd1 != null) { | |
holder.userId = Long.parseLong(hd1.getValue()); | |
} | |
Header hd2 = response.getFirstHeader("x-niconico-authflag"); | |
if (hd2 != null) { | |
if (Integer.parseInt(hd2.getValue()) != 0) { | |
holder.isPremiumUser = true; | |
} | |
} else { | |
throw new Exception(); | |
} | |
String prefix = "sm"; | |
int num = 21000000; //FAVORITEDOUGA | |
httpclient = HttpClients.createDefault(); | |
HttpHead headreq = new HttpHead("http://www.nicovideo.jp/watch/" | |
+ prefix + num); | |
headreq.addHeader( | |
"http.useragent", | |
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); | |
response = httpclient.execute(headreq, context); | |
if (response.getStatusLine().getStatusCode() == 400 | |
|| response.getStatusLine().getStatusCode() == 503) { | |
response.close(); | |
throw new Exception(); | |
} | |
response.close(); | |
builder = HttpClients.custom(); | |
builder.setRedirectStrategy(new DefaultRedirectStrategy() { | |
@Override | |
public URI getLocationURI(HttpRequest arg0, HttpResponse arg1, | |
HttpContext arg2) throws ProtocolException { | |
URI localUri = null; | |
try { | |
localUri = super.getLocationURI(arg0, arg1, arg2); | |
} catch (Exception e) { | |
} finally { | |
if (arg1.getFirstHeader("Location") != null) { | |
NicoGet.this.finalLocation = localUri.toString(); | |
} | |
} | |
if (localUri == null) { | |
try { | |
return new URI(NicoGet.this.finalLocation); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
return localUri; | |
} | |
@Override | |
public boolean isRedirected(HttpRequest request, | |
HttpResponse response, HttpContext context) | |
throws ProtocolException { | |
int code = response.getStatusLine().getStatusCode(); | |
if (code == 200) { | |
return false; | |
} | |
return true; | |
} | |
}); | |
httpclient = builder.build(); | |
Builder requestConfig = RequestConfig.custom().setSocketTimeout( | |
Integer.MAX_VALUE); | |
HttpGet get = new HttpGet("http://flapi.nicovideo.jp/api/getflv/" | |
+ prefix + num); | |
context.setCookieStore(store); | |
get.addHeader( | |
"http.useragent", | |
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); | |
get.setConfig(requestConfig.build()); | |
response = null; | |
response = httpclient.execute(get, context); | |
if (response.getStatusLine().getStatusCode() == 503) { | |
throw new Exception(); | |
} | |
responseEntity = response.getEntity(); | |
if (responseEntity.getContentLength() != -1) { | |
System.out.println("contentLength : " + responseEntity.getContentLength()); | |
} | |
stream = new BufferedReader(new InputStreamReader( | |
responseEntity.getContent())); | |
content = new StringBuilder(); | |
contentChank = new char[4096]; | |
i = 0; | |
readByte = 0; | |
while (i < (readByte = stream | |
.read(contentChank, 0, contentChank.length))) { | |
content.append(contentChank); | |
} | |
stream.close(); | |
response.close(); | |
String str3 = content.toString(); | |
if (str3.length() < 1) { | |
throw new Exception(); | |
} | |
System.out.println(str3); | |
String urlString = str3.substring(str3.indexOf("url") + 4); | |
urlString = urlString.substring(0, urlString.indexOf("&")); | |
System.out.println(urlString); | |
builder = HttpClients.custom(); | |
builder.setRedirectStrategy(new DefaultRedirectStrategy() { | |
@Override | |
public URI getLocationURI(HttpRequest arg0, HttpResponse arg1, | |
HttpContext arg2) throws ProtocolException { | |
URI localUri = null; | |
try { | |
localUri = super.getLocationURI(arg0, arg1, arg2); | |
} catch (Exception e) { | |
} finally { | |
if (arg1.getFirstHeader("Location") != null) { | |
NicoGet.this.finalLocation = localUri.toString(); | |
} | |
} | |
if (localUri == null) { | |
try { | |
return new URI(NicoGet.this.finalLocation); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
return localUri; | |
} | |
@Override | |
public boolean isRedirected(HttpRequest request, | |
HttpResponse response, HttpContext context) | |
throws ProtocolException { | |
int code = response.getStatusLine().getStatusCode(); | |
if (code == 200 || code == 400) { | |
return false; | |
} | |
return true; | |
} | |
}); | |
httpclient = builder.build(); | |
requestConfig = RequestConfig.custom().setSocketTimeout( | |
Integer.MAX_VALUE); | |
String decodedString = URLDecoder.decode(urlString, "UTF-8"); | |
String query = decodedString.substring(decodedString.indexOf("m=") + 2); | |
paramsList = new ArrayList<NameValuePair>(); | |
paramsList.add(new BasicNameValuePair("m", query)); | |
entity = new UrlEncodedFormEntity(paramsList, "UTF-8"); | |
post.setEntity(entity); | |
// httpclient = HttpClients.createDefault(); | |
get = new HttpGet(decodedString); | |
context.setCookieStore(store); | |
get.setHeader( | |
"User-Agent", | |
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"); | |
/ | |
List<Cookie> es = context.getCookieStore().getCookies(); | |
for (Cookie cc : es) { | |
String cString = cc.toString(); | |
System.out.println(cc.toString()); | |
} | |
Header[] hostsString = get.getAllHeaders(); | |
get.setConfig(requestConfig.build()); | |
response = httpclient.execute(get, context); | |
switch (response.getStatusLine().getStatusCode()) { | |
case 200: | |
case 206: | |
break; | |
case 400: | |
throw new Exception(); | |
} | |
responseEntity = response.getEntity(); | |
if (responseEntity.getContentLength() != -1) { | |
System.out.println("contentLength : " + responseEntity.getContentLength()); | |
} | |
String filename = "FAVORITE.flv"; | |
BufferedInputStream istream = new BufferedInputStream( | |
responseEntity.getContent()); | |
FileOutputStream outputStream = new FileOutputStream(new File( | |
"YOURPATH"+ filename)); | |
content = new StringBuilder(); | |
byte[] chank = new byte[4096]; | |
readByte = 0; | |
while ((readByte = istream.read(chank,0,chank.length)) != -1) { | |
System.out.println("received"); | |
outputStream.write(chank,0,readByte); | |
} | |
outputStream.flush(); | |
outputStream.close(); | |
istream.close(); | |
response.close(); | |
} | |
public static void main(String[] args) throws Exception { | |
new NicoGet(); | |
} | |
} | |
class SessionHolder { | |
String user_session; | |
Cookie cookie; | |
Long userId; | |
boolean isPremiumUser = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment