Search in sources :

Example 1 with HttpClient

use of com.frostwire.util.http.HttpClient in project frostwire by frostwire.

the class YouTubeExtractor method extractLinksFromDashManifest.

private List<LinkInfo> extractLinksFromDashManifest(String dashManifestUrl, YouTubeSig ytSig, String filename, Date date, String videoId, String userName, String channelName, ThumbnailLinks thumbnailLinks) throws IOException, ParserConfigurationException, SAXException {
    dashManifestUrl = dashManifestUrl.replace("\\/", "/");
    Pattern p = Pattern.compile("/s/([a-fA-F0-9\\.]+)/");
    Matcher m = p.matcher(dashManifestUrl);
    if (m.find()) {
        String sig = m.group(1);
        String signature = ytSig.calc(sig);
        dashManifestUrl = dashManifestUrl.replaceAll("/s/([a-fA-F0-9\\.]+)/", "/signature/" + signature + "/");
    } else if (dashManifestUrl.contains("/signature/")) {
    // dashManifestUrl as it is, empty block to review
    } else {
        return Collections.emptyList();
    }
    HttpClient httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.SEARCH);
    String dashDoc = httpClient.get(dashManifestUrl);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(dashDoc)));
    NodeList nodes = doc.getElementsByTagName("BaseURL");
    List<LinkInfo> infos = new ArrayList<LinkInfo>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node item = nodes.item(i);
        String url = item.getTextContent();
        int contentLength = -1;
        try {
            contentLength = Integer.parseInt(item.getAttributes().item(0).getTextContent());
        } catch (Throwable e) {
        // ignore
        }
        int fmt = Integer.parseInt(new Regex(url, "itag=(\\d+)").getMatch(0));
        Format format = FORMATS.get(fmt);
        if (format == null) {
            continue;
        }
        LinkInfo info = new LinkInfo(url, fmt, filename, contentLength, date, videoId, userName, channelName, thumbnailLinks, format);
        infos.add(info);
    }
    return infos;
}
Also used : Pattern(java.util.regex.Pattern) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Matcher(java.util.regex.Matcher) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SimpleDateFormat(java.text.SimpleDateFormat) Regex(com.frostwire.search.youtube.jd.Regex) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HttpClient(com.frostwire.util.http.HttpClient) StringReader(java.io.StringReader)

Example 2 with HttpClient

use of com.frostwire.util.http.HttpClient in project frostwire by frostwire.

the class YouTubeExtractor method getYouTubeSig.

private YouTubeSig getYouTubeSig(String html5playerUrl) {
    // concurrency issues are not important in this point
    YouTubeSig sig = null;
    if (!YT_SIG_MAP.containsKey(html5playerUrl)) {
        String jscode = "";
        try {
            html5playerUrl = html5playerUrl.replace("\\", "");
            HttpClient httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.SEARCH);
            jscode = httpClient.get(html5playerUrl);
            sig = new YouTubeSig(jscode);
            YT_SIG_MAP.put(html5playerUrl, sig);
        } catch (Throwable t) {
            LOG.error("Could not getYouTubeSig");
        // LOG.error("jscode:\n" + jscode);
        }
    } else {
        // cache hit, it worked with this url.
        sig = YT_SIG_MAP.get(html5playerUrl);
    }
    return sig;
}
Also used : HttpClient(com.frostwire.util.http.HttpClient)

Example 3 with HttpClient

use of com.frostwire.util.http.HttpClient in project frostwire by frostwire.

the class CreateTorrentDialog method checkWebSeedMirror.

/**
 * Sends HEAD request to the mirror location along with the test path to see if the file exists.
 * Read http://getright.com/seedtorrent.html to find out how mirror urls are interpreted
 */
private boolean checkWebSeedMirror(String mirror, create_torrent torrent, boolean isMultiFile) {
    String urlPath = getWebSeedTestPath(mirror, torrent, isMultiFile);
    HttpClient browser = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.MISC);
    int responseCode = 500;
    try {
        responseCode = browser.head(urlPath, 2000, null);
        System.out.println(responseCode + ": " + urlPath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseCode == 200;
}
Also used : HttpClient(com.frostwire.util.http.HttpClient) IOException(java.io.IOException)

Example 4 with HttpClient

use of com.frostwire.util.http.HttpClient in project frostwire by frostwire.

the class ShareTorrentDialog method performAsyncURLShortening.

private void performAsyncURLShortening(final URLShortenerHttpClientListener listener) {
    final HttpClient browser = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.MISC);
    browser.setListener(listener);
    ThreadExecutor.startThread(listener.getHttpRequestRunnable(browser), "ShareTorrentDialog-performAsyncURLShortening");
}
Also used : HttpClient(com.frostwire.util.http.HttpClient)

Example 5 with HttpClient

use of com.frostwire.util.http.HttpClient in project frostwire by frostwire.

the class ImageCache method loadFromUrl.

private void loadFromUrl(final URL url, final OnLoadedListener listener) {
    ThreadExecutor.startThread(new Thread(new Runnable() {

        public void run() {
            try {
                BufferedImage image = null;
                HttpClient newInstance = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.MISC);
                byte[] data = newInstance.getBytes(url.toString());
                if (data == null) {
                    throw new IOException("ImageCache.loadUrl() got nothing at " + url.toString());
                }
                if (data != null) {
                    image = ImageIO.read(new ByteArrayInputStream(data));
                    saveToCache(url, image, System.currentTimeMillis());
                }
                if (listener != null && image != null) {
                    listener.onLoaded(url, image, false, false);
                }
            } catch (Throwable e) {
                LOG.error("Failed to load image from: " + url, e);
                listener.onLoaded(url, null, false, true);
            }
        }
    }), "ImageCache.loadFromUrl");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(com.frostwire.util.http.HttpClient) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

HttpClient (com.frostwire.util.http.HttpClient)9 IOException (java.io.IOException)4 List (java.util.List)2 SoundcloudSearchResult (com.frostwire.search.soundcloud.SoundcloudSearchResult)1 Regex (com.frostwire.search.youtube.jd.Regex)1 HttpRangeException (com.frostwire.util.http.HttpClient.HttpRangeException)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 StringReader (java.io.StringReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1