Search in sources :

Example 1 with XMLEscapeWriter

use of com.biglybt.core.xml.util.XMLEscapeWriter in project BiglyBT by BiglySoftware.

the class CategoryManagerImpl method generate.

@Override
public boolean generate(TrackerWebPageRequest request, TrackerWebPageResponse response) throws IOException {
    URL url = request.getAbsoluteURL();
    String path = url.getPath();
    int pos = path.indexOf('?');
    if (pos != -1) {
        path = path.substring(0, pos);
    }
    path = path.substring(PROVIDER.length() + 1);
    XMLEscapeWriter pw = new XMLEscapeWriter(new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8")));
    pw.setEnabled(false);
    if (path.length() <= 1) {
        response.setContentType("text/html; charset=UTF-8");
        pw.println("<HTML><HEAD><TITLE>" + Constants.APP_NAME + " Category Feeds</TITLE></HEAD><BODY>");
        Map<String, String> lines = new TreeMap<>();
        List<CategoryImpl> cats;
        try {
            categories_mon.enter();
            cats = new ArrayList<>(categories.values());
        } finally {
            categories_mon.exit();
        }
        for (CategoryImpl c : cats) {
            if (c.getBooleanAttribute(Category.AT_RSS_GEN)) {
                String name = getDisplayName(c);
                String cat_url = PROVIDER + "/" + URLEncoder.encode(c.getName(), "UTF-8");
                lines.put(name, "<LI><A href=\"" + cat_url + "\">" + name + "</A></LI>");
            }
        }
        for (String line : lines.values()) {
            pw.println(line);
        }
        pw.println("</BODY></HTML>");
    } else {
        String cat_name = URLDecoder.decode(path.substring(1), "UTF-8");
        CategoryImpl cat;
        try {
            categories_mon.enter();
            cat = categories.get(cat_name);
        } finally {
            categories_mon.exit();
        }
        if (cat == null) {
            response.setReplyStatus(404);
            return (true);
        }
        List<DownloadManager> dms = cat.getDownloadManagers(CoreFactory.getSingleton().getGlobalManager().getDownloadManagers());
        List<Download> downloads = new ArrayList<>(dms.size());
        long dl_marker = 0;
        for (DownloadManager dm : dms) {
            TOTorrent torrent = dm.getTorrent();
            if (torrent == null) {
                continue;
            }
            if (!TorrentUtils.isReallyPrivate(torrent)) {
                dl_marker += dm.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
                downloads.add(PluginCoreUtils.wrap(dm));
            }
        }
        String config_key = "cat.rss.config." + Base32.encode(cat.getName().getBytes("UTF-8"));
        long old_marker = COConfigurationManager.getLongParameter(config_key + ".marker", 0);
        long last_modified = COConfigurationManager.getLongParameter(config_key + ".last_mod", 0);
        long now = SystemTime.getCurrentTime();
        if (old_marker == dl_marker) {
            if (last_modified == 0) {
                last_modified = now;
            }
        } else {
            COConfigurationManager.setParameter(config_key + ".marker", dl_marker);
            last_modified = now;
        }
        if (last_modified == now) {
            COConfigurationManager.setParameter(config_key + ".last_mod", last_modified);
        }
        response.setContentType("application/xml; charset=UTF-8");
        pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        pw.println("<rss version=\"2.0\" xmlns:vuze=\"http://www.vuze.com\">");
        pw.println("<channel>");
        pw.println("<title>" + escape(getDisplayName(cat)) + "</title>");
        Collections.sort(downloads, new Comparator<Download>() {

            @Override
            public int compare(Download d1, Download d2) {
                long added1 = getAddedTime(d1) / 1000;
                long added2 = getAddedTime(d2) / 1000;
                return ((int) (added2 - added1));
            }
        });
        pw.println("<pubDate>" + TimeFormatter.getHTTPDate(last_modified) + "</pubDate>");
        for (int i = 0; i < downloads.size(); i++) {
            Download download = downloads.get(i);
            DownloadManager core_download = PluginCoreUtils.unwrap(download);
            Torrent torrent = download.getTorrent();
            byte[] hash = torrent.getHash();
            String hash_str = Base32.encode(hash);
            pw.println("<item>");
            pw.println("<title>" + escape(download.getName()) + "</title>");
            pw.println("<guid>" + hash_str + "</guid>");
            String magnet_url = escape(UrlUtils.getMagnetURI(download));
            pw.println("<link>" + magnet_url + "</link>");
            long added = core_download.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME);
            pw.println("<pubDate>" + TimeFormatter.getHTTPDate(added) + "</pubDate>");
            pw.println("<vuze:size>" + torrent.getSize() + "</vuze:size>");
            pw.println("<vuze:assethash>" + hash_str + "</vuze:assethash>");
            pw.println("<vuze:downloadurl>" + magnet_url + "</vuze:downloadurl>");
            DownloadScrapeResult scrape = download.getLastScrapeResult();
            if (scrape != null && scrape.getResponseType() == DownloadScrapeResult.RT_SUCCESS) {
                pw.println("<vuze:seeds>" + scrape.getSeedCount() + "</vuze:seeds>");
                pw.println("<vuze:peers>" + scrape.getNonSeedCount() + "</vuze:peers>");
            }
            pw.println("</item>");
        }
        pw.println("</channel>");
        pw.println("</rss>");
    }
    pw.flush();
    return (true);
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) XMLEscapeWriter(com.biglybt.core.xml.util.XMLEscapeWriter) DownloadManager(com.biglybt.core.download.DownloadManager) URL(java.net.URL) TOTorrent(com.biglybt.core.torrent.TOTorrent) Download(com.biglybt.pif.download.Download) TagDownload(com.biglybt.core.tag.TagDownload) DownloadScrapeResult(com.biglybt.pif.download.DownloadScrapeResult)

Example 2 with XMLEscapeWriter

use of com.biglybt.core.xml.util.XMLEscapeWriter in project BiglyBT by BiglySoftware.

the class DeviceManagerRSSFeed method generate.

@Override
public boolean generate(TrackerWebPageRequest request, TrackerWebPageResponse response) throws IOException {
    InetSocketAddress local_address = request.getLocalAddress();
    if (local_address == null) {
        return (false);
    }
    URL url = request.getAbsoluteURL();
    String path = url.getPath();
    path = path.substring(PROVIDER.length() + 1);
    DeviceImpl[] devices = manager.getDevices();
    OutputStream os = response.getOutputStream();
    XMLEscapeWriter pw = new XMLEscapeWriter(new PrintWriter(new OutputStreamWriter(os, "UTF-8")));
    pw.setEnabled(false);
    boolean hide_generic = COConfigurationManager.getBooleanParameter(DeviceManager.CONFIG_VIEW_HIDE_REND_GENERIC, true);
    boolean show_only_tagged = COConfigurationManager.getBooleanParameter(DeviceManager.CONFIG_VIEW_SHOW_ONLY_TAGGED, false);
    if (path.length() <= 1) {
        response.setContentType("text/html; charset=UTF-8");
        pw.println("<HTML><HEAD><TITLE>" + Constants.APP_NAME + " Device Feeds</TITLE></HEAD><BODY>");
        for (DeviceImpl d : devices) {
            if (d.getType() != Device.DT_MEDIA_RENDERER || d.isHidden() || !d.isRSSPublishEnabled() || (hide_generic && d.isNonSimple()) || (show_only_tagged && !d.isTagged())) {
                continue;
            }
            String name = d.getName();
            String device_url = PROVIDER + "/" + URLEncoder.encode(name, "UTF-8");
            pw.println("<LI><A href=\"" + device_url + "\">" + name + "</A>&nbsp;&nbsp;-&nbsp;&nbsp;<font size=\"-1\"><a href=\"" + device_url + "?format=html\">html</a></font></LI>");
        }
        pw.println("</BODY></HTML>");
    } else {
        String device_name = URLDecoder.decode(path.substring(1), "UTF-8");
        DeviceImpl device = null;
        for (DeviceImpl d : devices) {
            if (d.getName().equals(device_name) && d.isRSSPublishEnabled()) {
                device = d;
                break;
            }
        }
        if (device == null) {
            response.setReplyStatus(404);
            return (true);
        }
        TranscodeFileImpl[] _files = device.getFiles();
        List<TranscodeFileImpl> files = new ArrayList<>(_files.length);
        files.addAll(Arrays.asList(_files));
        Collections.sort(files, new Comparator<TranscodeFileImpl>() {

            @Override
            public int compare(TranscodeFileImpl f1, TranscodeFileImpl f2) {
                long added1 = f1.getCreationDateMillis() / 1000;
                long added2 = f2.getCreationDateMillis() / 1000;
                return ((int) (added2 - added1));
            }
        });
        URL feed_url = url;
        // absolute url is borked as it doesn't set the host properly. hack
        String host = (String) request.getHeaders().get("host");
        if (host != null) {
            int pos = host.indexOf(':');
            if (pos != -1) {
                host = host.substring(0, pos);
            }
            feed_url = UrlUtils.setHost(url, host);
        }
        if (device instanceof DeviceMediaRendererImpl) {
            ((DeviceMediaRendererImpl) device).browseReceived();
        }
        String channel_title = "Vuze Device: " + escape(device.getName());
        boolean html = request.getURL().contains("format=html");
        if (html) {
            response.setContentType("text/html; charset=UTF-8");
            pw.println("<HTML><HEAD><TITLE>" + channel_title + "</TITLE></HEAD><BODY>");
            for (TranscodeFileImpl file : files) {
                if (!file.isComplete()) {
                    if (!file.isTemplate()) {
                        continue;
                    }
                }
                URL stream_url = file.getStreamURL(feed_url.getHost());
                if (stream_url != null) {
                    String url_ext = stream_url.toExternalForm();
                    pw.println("<p>");
                    pw.println("<a href=\"" + url_ext + "\">" + escape(file.getName()) + "</a>");
                    url_ext += url_ext.indexOf('?') == -1 ? "?" : "&";
                    url_ext += "action=download";
                    pw.println("&nbsp;&nbsp;-&nbsp;&nbsp;<font size=\"-1\"><a href=\"" + url_ext + "\">save</a></font>");
                }
            }
            pw.println("</BODY></HTML>");
        } else {
            boolean debug = request.getURL().contains("format=debug");
            if (debug) {
                response.setContentType("text/html; charset=UTF-8");
                pw.println("<HTML><HEAD><TITLE>" + channel_title + "</TITLE></HEAD><BODY>");
                pw.println("<pre>");
                pw.setEnabled(true);
            } else {
                response.setContentType("application/xml; charset=UTF-8");
            }
            try {
                pw.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                pw.println("<rss version=\"2.0\" " + "xmlns:vuze=\"http://www.vuze.com\" " + "xmlns:media=\"http://search.yahoo.com/mrss/\" " + "xmlns:atom=\"http://www.w3.org/2005/Atom\" " + "xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\">");
                pw.println("<channel>");
                pw.println("<title>" + channel_title + "</title>");
                pw.println("<link>http://vuze.com</link>");
                pw.println("<atom:link href=\"" + feed_url.toExternalForm() + "\" rel=\"self\" type=\"application/rss+xml\" />");
                pw.println("<description>" + Constants.APP_NAME + " RSS Feed for device " + escape(device.getName()) + "</description>");
                pw.println("<itunes:image href=\"http://www.vuze.com/img/vuze_icon_128.png\"/>");
                pw.println("<image><url>http://www.vuze.com/img/vuze_icon_128.png</url><title>" + channel_title + "</title><link>http://vuze.com</link></image>");
                String feed_date_key = "devices.feed_date." + device.getID();
                long feed_date = COConfigurationManager.getLongParameter(feed_date_key);
                boolean new_date = false;
                for (TranscodeFileImpl file : files) {
                    long file_date = file.getCreationDateMillis();
                    if (file_date > feed_date) {
                        new_date = true;
                        feed_date = file_date;
                    }
                }
                if (new_date) {
                    COConfigurationManager.setParameter(feed_date_key, feed_date);
                }
                pw.println("<pubDate>" + TimeFormatter.getHTTPDate(feed_date) + "</pubDate>");
                for (TranscodeFileImpl file : files) {
                    if (!file.isComplete()) {
                        if (!file.isTemplate()) {
                            continue;
                        }
                    }
                    try {
                        pw.println("<item>");
                        pw.println("<title>" + escape(file.getName()) + "</title>");
                        pw.println("<pubDate>" + TimeFormatter.getHTTPDate(file.getCreationDateMillis()) + "</pubDate>");
                        pw.println("<guid isPermaLink=\"false\">" + escape(file.getKey()) + "</guid>");
                        String[] categories = file.getCategories();
                        for (String category : categories) {
                            pw.println("<category>" + escape(category) + "</category>");
                        }
                        String[] tags = file.getTags(true);
                        for (String tag : tags) {
                            pw.println("<tag>" + escape(tag) + "</tag>");
                        }
                        String mediaContent = "";
                        URL stream_url = file.getStreamURL(feed_url.getHost());
                        if (stream_url != null) {
                            String url_ext = escape(stream_url.toExternalForm());
                            long fileSize = file.getTargetFile().getLength();
                            pw.println("<link>" + url_ext + "</link>");
                            mediaContent = "<media:content medium=\"video\" fileSize=\"" + fileSize + "\" url=\"" + url_ext + "\"";
                            String mime_type = file.getMimeType();
                            if (mime_type != null) {
                                mediaContent += " type=\"" + mime_type + "\"";
                            }
                            pw.println("<enclosure url=\"" + url_ext + "\" length=\"" + fileSize + (mime_type == null ? "" : "\" type=\"" + mime_type) + "\"></enclosure>");
                        }
                        String thumb_url = null;
                        String author = null;
                        String description = null;
                        try {
                            Torrent torrent = file.getSourceFile().getDownload().getTorrent();
                            TOTorrent toTorrent = PluginCoreUtils.unwrap(torrent);
                            long duration_secs = PlatformTorrentUtils.getContentVideoRunningTime(toTorrent);
                            if (mediaContent.length() > 0 && duration_secs > 0) {
                                mediaContent += " duration=\"" + duration_secs + "\"";
                            }
                            thumb_url = PlatformTorrentUtils.getContentThumbnailUrl(toTorrent);
                            author = PlatformTorrentUtils.getContentAuthor(toTorrent);
                            description = PlatformTorrentUtils.getContentDescription(toTorrent);
                            if (description != null) {
                                description = escapeMultiline(description);
                                /*
			  						if ( thumb_url != null ){


			  							pw.println( "<description type=\"text/html\">" +
			  								escape( "<div style=\"text-align: justify;padding: 5px;\"><img style=\"float: left;margin-right: 15px;margin-bottom: 15px;\" src=\"" + thumb_url + "\"/>" ) +
			  								description +
			  								escape( "</div>" ) +
			  								"</description>" );
			  						}else{
			  						*/
                                pw.println("<description>" + description + "</description>");
                            // }
                            }
                        } catch (Throwable e) {
                        }
                        if (mediaContent.length() > 0) {
                            pw.println(mediaContent += "></media:content>");
                        }
                        pw.println("<media:title>" + escape(file.getName()) + "</media:title>");
                        if (description != null) {
                            pw.println("<media:description>" + description + "</media:description>");
                        }
                        if (thumb_url != null) {
                            pw.println("<media:thumbnail url=\"" + thumb_url + "\"/>");
                        }
                        if (thumb_url != null) {
                            pw.println("<itunes:image href=\"" + thumb_url + "\"/>");
                        }
                        if (author != null) {
                            pw.println("<itunes:author>" + escape(author) + "</itunees:author>");
                        }
                        pw.println("<itunes:summary>" + escape(file.getName()) + "</itunes:summary>");
                        pw.println("<itunes:duration>" + TimeFormatter.formatColon(file.getDurationMillis() / 1000) + "</itunes:duration>");
                        pw.println("</item>");
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                }
                pw.println("</channel>");
                pw.println("</rss>");
            } finally {
                if (debug) {
                    pw.setEnabled(false);
                    pw.println("</pre>");
                    pw.println("</BODY></HTML>");
                }
            }
        }
    }
    pw.flush();
    return (true);
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) XMLEscapeWriter(com.biglybt.core.xml.util.XMLEscapeWriter) URL(java.net.URL) TOTorrent(com.biglybt.core.torrent.TOTorrent) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

TOTorrent (com.biglybt.core.torrent.TOTorrent)2 XMLEscapeWriter (com.biglybt.core.xml.util.XMLEscapeWriter)2 Torrent (com.biglybt.pif.torrent.Torrent)2 URL (java.net.URL)2 DownloadManager (com.biglybt.core.download.DownloadManager)1 TagDownload (com.biglybt.core.tag.TagDownload)1 Download (com.biglybt.pif.download.Download)1 DownloadScrapeResult (com.biglybt.pif.download.DownloadScrapeResult)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 InetSocketAddress (java.net.InetSocketAddress)1