Search in sources :

Example 1 with Subscription

use of com.biglybt.pif.utils.subscriptions.Subscription in project BiglyBT by BiglySoftware.

the class SubscriptionRSSFeed 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);
    try {
        SubscriptionManager sman = plugin_interface.getUtilities().getSubscriptionManager();
        Subscription[] subs = sman.getSubscriptions();
        OutputStream os = response.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
        if (path.length() <= 1) {
            response.setContentType("text/html; charset=UTF-8");
            pw.println("<HTML><HEAD><TITLE>" + Constants.APP_NAME + " Subscription Feeds</TITLE></HEAD><BODY>");
            for (Subscription s : subs) {
                if (s.isSearchTemplate()) {
                    continue;
                }
                String name = s.getName();
                pw.println("<LI><A href=\"" + PROVIDER + "/" + s.getID() + "\">" + name + "</A></LI>");
            }
            pw.println("</BODY></HTML>");
        } else {
            String id = path.substring(1);
            Subscription subscription = null;
            for (Subscription s : subs) {
                if (s.getID().equals(id)) {
                    subscription = s;
                    break;
                }
            }
            if (subscription == null) {
                response.setReplyStatus(404);
                return (true);
            }
            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);
            }
            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\" " + "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>");
            String channel_title = "Vuze Subscription: " + escape(subscription.getName());
            pw.println("<title>" + channel_title + "</title>");
            pw.println("<link>http://vuze.com</link>");
            pw.println("<atom:link href=\"" + escape(feed_url.toExternalForm()) + "\" rel=\"self\" type=\"application/rss+xml\" />");
            pw.println("<description>" + Constants.APP_NAME + " RSS Feed for subscription " + escape(subscription.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>");
            SubscriptionResult[] results = subscription.getResults();
            String feed_date_key = "subscriptions.feed_date." + subscription.getID();
            long feed_date = COConfigurationManager.getLongParameter(feed_date_key);
            boolean new_date = false;
            for (SubscriptionResult result : results) {
                Date date = (Date) result.getProperty(SearchResult.PR_PUB_DATE);
                long millis = date.getTime();
                if (millis > feed_date) {
                    feed_date = millis;
                    new_date = true;
                }
            }
            if (new_date) {
                COConfigurationManager.setParameter(feed_date_key, feed_date);
            }
            pw.println("<pubDate>" + TimeFormatter.getHTTPDate(feed_date) + "</pubDate>");
            for (SubscriptionResult result : results) {
                try {
                    pw.println("<item>");
                    String name = (String) result.getProperty(SearchResult.PR_NAME);
                    pw.println("<title>" + escape(name) + "</title>");
                    Date date = (Date) result.getProperty(SearchResult.PR_PUB_DATE);
                    if (date != null) {
                        pw.println("<pubDate>" + TimeFormatter.getHTTPDate(date.getTime()) + "</pubDate>");
                    }
                    String uid = (String) result.getProperty(SearchResult.PR_UID);
                    if (uid != null) {
                        pw.println("<guid isPermaLink=\"false\">" + escape(uid) + "</guid>");
                    }
                    String link = (String) result.getProperty(SearchResult.PR_DOWNLOAD_LINK);
                    Long size = (Long) result.getProperty(SearchResult.PR_SIZE);
                    if (link != null) {
                        pw.println("<link>" + escape(link) + "</link>");
                        if (size != null) {
                            pw.println("<media:content fileSize=\"" + size + "\" url=\"" + escape(link) + "\"/>");
                        }
                    }
                    if (size != null) {
                        pw.println("<vuze:size>" + size + "</vuze:size>");
                    }
                    Long seeds = (Long) result.getProperty(SearchResult.PR_SEED_COUNT);
                    if (seeds != null) {
                        pw.println("<vuze:seeds>" + seeds + "</vuze:seeds>");
                    }
                    Long peers = (Long) result.getProperty(SearchResult.PR_LEECHER_COUNT);
                    if (peers != null) {
                        pw.println("<vuze:peers>" + peers + "</vuze:peers>");
                    }
                    Long rank = (Long) result.getProperty(SearchResult.PR_RANK);
                    if (rank != null) {
                        pw.println("<vuze:rank>" + rank + "</vuze:rank>");
                    }
                    pw.println("</item>");
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
            pw.println("</channel>");
            pw.println("</rss>");
        }
        pw.flush();
    } catch (Throwable e) {
        Debug.out(e);
        throw (new IOException(Debug.getNestedExceptionMessage(e)));
    }
    return (true);
}
Also used : SubscriptionResult(com.biglybt.pif.utils.subscriptions.SubscriptionResult) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) IOException(java.io.IOException) URL(java.net.URL) Date(java.util.Date) OutputStreamWriter(java.io.OutputStreamWriter) Subscription(com.biglybt.pif.utils.subscriptions.Subscription) PrintWriter(java.io.PrintWriter)

Example 2 with Subscription

use of com.biglybt.pif.utils.subscriptions.Subscription in project BiglyBT by BiglySoftware.

the class UtilitiesImpl method getSubscriptionManager.

@Override
public SubscriptionManager getSubscriptionManager() throws SubscriptionException {
    try {
        Method m = Class.forName("com.biglybt.core.subs.SubscriptionManagerFactory").getMethod("getSingleton");
        final PluginSubscriptionManager sm = (PluginSubscriptionManager) m.invoke(null);
        return (new SubscriptionManager() {

            @Override
            public void requestSubscription(URL url) {
                sm.requestSubscription(url, new HashMap<String, Object>());
            }

            @Override
            public void requestSubscription(URL url, Map<String, Object> options) {
                sm.requestSubscription(url, options);
            }

            @Override
            public void requestSubscription(SearchProvider sp, Map<String, Object> search_parameters) throws SubscriptionException {
                sm.requestSubscription(sp, search_parameters);
            }

            @Override
            public Subscription[] getSubscriptions() {
                PluginSubscription[] p_subs = sm.getSubscriptions(true);
                Subscription[] subs = new Subscription[p_subs.length];
                for (int i = 0; i < subs.length; i++) {
                    final PluginSubscription p_sub = p_subs[i];
                    subs[i] = new Subscription() {

                        @Override
                        public String getID() {
                            return (p_sub.getID());
                        }

                        @Override
                        public String getName() {
                            return (p_sub.getName());
                        }

                        @Override
                        public boolean isSearchTemplate() {
                            return (p_sub.isSearchTemplate());
                        }

                        @Override
                        public SubscriptionResult[] getResults() {
                            PluginSubscriptionResult[] p_results = p_sub.getResults(false);
                            SubscriptionResult[] results = new SubscriptionResult[p_results.length];
                            for (int i = 0; i < results.length; i++) {
                                final PluginSubscriptionResult p_res = p_results[i];
                                results[i] = new SubscriptionResult() {

                                    private Map<Integer, Object> map = p_res.toPropertyMap();

                                    @Override
                                    public Object getProperty(int property_name) {
                                        return (map.get(property_name));
                                    }

                                    @Override
                                    public boolean isRead() {
                                        return (p_res.getRead());
                                    }

                                    @Override
                                    public void setRead(boolean read) {
                                        p_res.setRead(read);
                                    }
                                };
                            }
                            return (results);
                        }
                    };
                }
                return (subs);
            }
        });
    } catch (Throwable e) {
        throw (new SubscriptionException("Subscriptions unavailable", e));
    }
}
Also used : SubscriptionException(com.biglybt.pif.utils.subscriptions.SubscriptionException) SubscriptionResult(com.biglybt.pif.utils.subscriptions.SubscriptionResult) SearchProvider(com.biglybt.pif.utils.search.SearchProvider) Method(java.lang.reflect.Method) SubscriptionManager(com.biglybt.pif.utils.subscriptions.SubscriptionManager) URL(java.net.URL) Subscription(com.biglybt.pif.utils.subscriptions.Subscription)

Aggregations

Subscription (com.biglybt.pif.utils.subscriptions.Subscription)2 SubscriptionManager (com.biglybt.pif.utils.subscriptions.SubscriptionManager)2 SubscriptionResult (com.biglybt.pif.utils.subscriptions.SubscriptionResult)2 URL (java.net.URL)2 SearchProvider (com.biglybt.pif.utils.search.SearchProvider)1 SubscriptionException (com.biglybt.pif.utils.subscriptions.SubscriptionException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Method (java.lang.reflect.Method)1 InetSocketAddress (java.net.InetSocketAddress)1 Date (java.util.Date)1