Search in sources :

Example 6 with WebEngine

use of com.biglybt.core.metasearch.impl.web.WebEngine in project BiglyBT by BiglySoftware.

the class SearchUtils method showProperties.

private static void showProperties(Engine engine) {
    String engine_str;
    String auth_str = String.valueOf(false);
    engine_str = engine.getNameEx();
    String url_str = null;
    if (engine instanceof WebEngine) {
        WebEngine web_engine = (WebEngine) engine;
        if (web_engine.isNeedsAuth()) {
            auth_str = String.valueOf(true) + ": cookies=" + toString(web_engine.getRequiredCookies());
        }
        url_str = web_engine.getSearchUrl();
    }
    String[] keys = { "subs.prop.template", "subs.prop.auth", "subs.prop.query", "label.anon", "subs.prop.version" };
    String[] values = { engine_str, auth_str, url_str, String.valueOf(engine.isAnonymous()), String.valueOf(engine.getVersion()) };
    new PropertiesWindow(engine.getName(), keys, values);
}
Also used : PropertiesWindow(com.biglybt.ui.swt.PropertiesWindow) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine)

Example 7 with WebEngine

use of com.biglybt.core.metasearch.impl.web.WebEngine in project BiglyBT by BiglySoftware.

the class SubscriptionManagerUI method createMenus.

protected static void createMenus(final MenuManager menu_manager, final MenuCreator menu_creator, final Subscription[] all_subs) {
    if (all_subs.length > 1) {
        MenuItem menuItem = menu_creator.createMenu("menu.set.parent");
        menuItem.addMultiListener(new SubsMenuItemListener() {

            @Override
            public void selected(final Subscription[] subs) {
                UISWTInputReceiver entry = new SimpleTextEntryWindow();
                if (subs.length == 1) {
                    entry.setPreenteredText(subs[0].getParent(), false);
                }
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(true);
                entry.setLocalisedTitle(MessageText.getString("label.set.parent"));
                entry.prompt(new UIInputReceiverListener() {

                    @Override
                    public void UIInputReceiverClosed(UIInputReceiver entry) {
                        if (!entry.hasSubmittedInput()) {
                            return;
                        }
                        String input = entry.getSubmittedInput().trim();
                        if (input.length() == 0) {
                            input = null;
                        }
                        for (Subscription sub : subs) {
                            sub.setParent(input);
                        }
                    }
                });
            }
        });
        return;
    }
    final Subscription subs = all_subs[0];
    boolean is_search_template = subs.isSearchTemplate();
    if (!is_search_template) {
        MenuItem menuItem = menu_creator.createMenu("Subscription.menu.forcecheck");
        menuItem.setText(MessageText.getString("Subscription.menu.forcecheck"));
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                try {
                    subs.getManager().getScheduler().downloadAsync(subs, true);
                } catch (SubscriptionException e) {
                    Debug.out(e);
                }
            }
        });
        menuItem = menu_creator.createMenu("Subscription.menu.clearall");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                subs.getHistory().markAllResultsRead();
                menu_creator.refreshView();
            }
        });
        menuItem = menu_creator.createMenu("Subscription.menu.dirtyall");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                subs.getHistory().markAllResultsUnread();
                menu_creator.refreshView();
            }
        });
        menuItem = menu_creator.createMenu("Subscription.menu.deleteall");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                subs.getHistory().deleteAllResults();
                menu_creator.refreshView();
            }
        });
        menuItem = menu_creator.createMenu("Subscription.menu.reset");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                subs.getHistory().reset();
                try {
                    subs.getEngine().reset();
                } catch (Throwable e) {
                    Debug.printStackTrace(e);
                }
                try {
                    subs.getManager().getScheduler().downloadAsync(subs, true);
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        });
        try {
            Engine engine = subs.getEngine();
            if (engine instanceof WebEngine) {
                if (((WebEngine) engine).isNeedsAuth()) {
                    menuItem = menu_creator.createMenu("Subscription.menu.resetauth");
                    menuItem.addListener(new SubsMenuItemListener() {

                        @Override
                        public void selected(Subscription subs) {
                            try {
                                Engine engine = subs.getEngine();
                                if (engine instanceof WebEngine) {
                                    ((WebEngine) engine).setCookies(null);
                                }
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                            }
                            try {
                                subs.getManager().getScheduler().downloadAsync(subs, true);
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    });
                    menuItem = menu_creator.createMenu("Subscription.menu.setcookies");
                    menuItem.addListener(new SubsMenuItemListener() {

                        @Override
                        public void selected(final Subscription subs) {
                            try {
                                Engine engine = subs.getEngine();
                                if (engine instanceof WebEngine) {
                                    final WebEngine we = (WebEngine) engine;
                                    UISWTInputReceiver entry = new SimpleTextEntryWindow();
                                    String[] req = we.getRequiredCookies();
                                    String req_str = "";
                                    for (String r : req) {
                                        req_str += (req_str.length() == 0 ? "" : ";") + r + "=?";
                                    }
                                    entry.setPreenteredText(req_str, true);
                                    entry.maintainWhitespace(false);
                                    entry.allowEmptyInput(false);
                                    entry.setTitle("general.enter.cookies");
                                    entry.prompt(new UIInputReceiverListener() {

                                        @Override
                                        public void UIInputReceiverClosed(UIInputReceiver entry) {
                                            if (!entry.hasSubmittedInput()) {
                                                return;
                                            }
                                            try {
                                                String input = entry.getSubmittedInput().trim();
                                                if (input.length() > 0) {
                                                    we.setCookies(input);
                                                    subs.getManager().getScheduler().downloadAsync(subs, true);
                                                }
                                            } catch (Throwable e) {
                                                Debug.printStackTrace(e);
                                            }
                                        }
                                    });
                                }
                            } catch (Throwable e) {
                                Debug.printStackTrace(e);
                            }
                        }
                    });
                }
            }
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
        // sep
        menu_creator.createMenu("s1").setStyle(MenuItem.STYLE_SEPARATOR);
        // category
        menuItem = menu_creator.createMenu("MyTorrentsView.menu.setCategory");
        menuItem.setStyle(MenuItem.STYLE_MENU);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                addCategorySubMenu(menu_manager, menu, subs);
            }
        });
        // tag
        menuItem = menu_creator.createMenu("label.tag");
        menuItem.setStyle(MenuItem.STYLE_MENU);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                addTagSubMenu(menu_manager, menu, subs);
            }
        });
        // parent
        menuItem = menu_creator.createMenu("menu.set.parent");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(final Subscription subs) {
                UISWTInputReceiver entry = new SimpleTextEntryWindow();
                entry.setPreenteredText(subs.getParent(), false);
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(true);
                entry.setLocalisedTitle(MessageText.getString("label.set.parent", new String[] { subs.getName() }));
                entry.prompt(new UIInputReceiverListener() {

                    @Override
                    public void UIInputReceiverClosed(UIInputReceiver entry) {
                        if (!entry.hasSubmittedInput()) {
                            return;
                        }
                        String input = entry.getSubmittedInput().trim();
                        if (input.length() == 0) {
                            input = null;
                        }
                        subs.setParent(input);
                    }
                });
            }
        });
        // view options
        menuItem = menu_creator.createMenu("menu.view.options");
        menuItem.setStyle(MenuItem.STYLE_MENU);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.removeAllChildItems();
                int vo = subs.getViewOptions();
                MenuItem m = menu_manager.addMenuItem(menu, "label.full");
                m.setStyle(MenuItem.STYLE_RADIO);
                m.setData(Boolean.valueOf(vo == Subscription.VO_FULL));
                m.addListener(new SubsMenuItemListener() {

                    @Override
                    public void selected(final Subscription subs) {
                        subs.setViewOptions(Subscription.VO_FULL);
                    }
                });
                m = menu_manager.addMenuItem(menu, "label.no.header");
                m.setStyle(MenuItem.STYLE_RADIO);
                m.setData(Boolean.valueOf(vo == Subscription.VO_HIDE_HEADER));
                m.addListener(new SubsMenuItemListener() {

                    @Override
                    public void selected(final Subscription subs) {
                        subs.setViewOptions(Subscription.VO_HIDE_HEADER);
                    }
                });
            }
        });
        // chat
        final String key = SubscriptionUtils.getSubscriptionChatKey(subs);
        if (key != null) {
            menuItem = menu_creator.createMenu("label.chat");
            menuItem.setHeaderCategory(MenuItem.HEADER_SOCIAL);
            MenuBuildUtils.addChatMenu(menu_manager, menuItem, new MenuBuildUtils.ChatKeyResolver() {

                @Override
                public String getChatKey(Object object) {
                    return (key);
                }
            });
        }
        if (subs.isUpdateable()) {
            menuItem = menu_creator.createMenu("MyTorrentsView.menu.rename");
            menuItem.addListener(new SubsMenuItemListener() {

                @Override
                public void selected(final Subscription subs) {
                    UISWTInputReceiver entry = new SimpleTextEntryWindow();
                    entry.setPreenteredText(subs.getName(), false);
                    entry.maintainWhitespace(false);
                    entry.allowEmptyInput(false);
                    entry.setLocalisedTitle(MessageText.getString("label.rename", new String[] { subs.getName() }));
                    entry.prompt(new UIInputReceiverListener() {

                        @Override
                        public void UIInputReceiverClosed(UIInputReceiver entry) {
                            if (!entry.hasSubmittedInput()) {
                                return;
                            }
                            String input = entry.getSubmittedInput().trim();
                            if (input.length() > 0) {
                                try {
                                    subs.setName(input);
                                } catch (Throwable e) {
                                    Debug.printStackTrace(e);
                                }
                            }
                        }
                    });
                }
            });
        }
        menuItem = menu_creator.createMenu("Subscription.menu.upgrade");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                subs.resetHighestVersion();
            }
        });
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.setVisible(subs.getHighestVersion() > subs.getVersion());
            }
        });
    }
    MenuItem menuItem = menu_creator.createMenu("label.copy.uri.to.clip");
    menuItem.addListener(new SubsMenuItemListener() {

        @Override
        public void selected(Subscription subs) {
            ClipboardCopy.copyToClipBoard(subs.getURI());
        }
    });
    menuItem = menu_creator.createMenu("Subscription.menu.export");
    menuItem.addListener(new SubsMenuItemListener() {

        @Override
        public void selected(Subscription subs) {
            export(subs);
        }
    });
    // sep
    menu_creator.createMenu("s2").setStyle(MenuItem.STYLE_SEPARATOR);
    if (!is_search_template) {
        try {
            Engine engine = subs.getEngine();
            if (engine instanceof WebEngine) {
                menuItem = menu_creator.createMenu("menu.change.url");
                menuItem.addListener(new SubsMenuItemListener() {

                    @Override
                    public void selected(final Subscription subs) {
                        UISWTInputReceiver entry = new SimpleTextEntryWindow();
                        try {
                            WebEngine web_engine = (WebEngine) subs.getEngine();
                            entry.setPreenteredText(web_engine.getSearchUrl(true), false);
                            entry.maintainWhitespace(false);
                            entry.allowEmptyInput(false);
                            entry.setLocalisedTitle(MessageText.getString("change.url.msg.title", new String[] { subs.getName() }));
                            entry.setMessage("change.url.msg.desc");
                            entry.prompt(new UIInputReceiverListener() {

                                @Override
                                public void UIInputReceiverClosed(UIInputReceiver entry) {
                                    if (!entry.hasSubmittedInput()) {
                                        return;
                                    }
                                    String input = entry.getSubmittedInput().trim();
                                    if (input.length() > 0) {
                                        try {
                                            WebEngine web_engine = (WebEngine) subs.getEngine();
                                            web_engine.setSearchUrl(input);
                                            subs.cloneWithNewEngine(web_engine);
                                        } catch (Throwable e) {
                                            Debug.out(e);
                                        }
                                    }
                                }
                            });
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                });
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
        // public
        menuItem = menu_creator.createMenu("subs.prop.is_public");
        menuItem.setStyle(MenuItem.STYLE_CHECK);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.setData(subs.isPublic());
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                try {
                    subs.setPublic(!subs.isPublic());
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        });
        // enabled
        menuItem = menu_creator.createMenu("subs.prop.enabled");
        menuItem.setStyle(MenuItem.STYLE_CHECK);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.setData(subs.getHistory().isEnabled());
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                try {
                    subs.getHistory().setEnabled(!subs.getHistory().isEnabled());
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        });
        if (subs.isAutoDownloadSupported()) {
            // auto-dl
            menuItem = menu_creator.createMenu("subs.prop.is_auto");
            menuItem.setStyle(MenuItem.STYLE_CHECK);
            menuItem.addFillListener(new MenuItemFillListener() {

                @Override
                public void menuWillBeShown(MenuItem menu, Object data) {
                    menu.setData(subs.getHistory().isAutoDownload());
                }
            });
            menuItem.addListener(new SubsMenuItemListener() {

                @Override
                public void selected(Subscription subs) {
                    try {
                        subs.getHistory().setAutoDownload(!subs.getHistory().isAutoDownload());
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                }
            });
        }
        // refresh period
        menuItem = menu_creator.createMenu("subs.prop.update_period");
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                int check_freq = subs.getHistory().getCheckFrequencyMins();
                String text = MessageText.getString("subs.prop.update_period");
                if (check_freq != Integer.MAX_VALUE) {
                    text += " (" + check_freq + " " + MessageText.getString("ConfigView.text.minutes") + ")";
                }
                menu.setText(text + "...");
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(final Subscription subs) {
                UISWTInputReceiver entry = new SimpleTextEntryWindow();
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(false);
                int check_freq = subs.getHistory().getCheckFrequencyMins();
                entry.setPreenteredText(check_freq == Integer.MAX_VALUE ? "" : String.valueOf(check_freq), false);
                entry.setLocalisedTitle(MessageText.getString("subscriptions.enter.freq"));
                entry.prompt(new UIInputReceiverListener() {

                    @Override
                    public void UIInputReceiverClosed(UIInputReceiver entry) {
                        if (!entry.hasSubmittedInput()) {
                            return;
                        }
                        String input = entry.getSubmittedInput().trim();
                        if (input.length() > 0) {
                            try {
                                subs.getHistory().setCheckFrequencyMins(Integer.parseInt(input));
                            } catch (Throwable e) {
                            }
                        }
                    }
                });
            }
        });
        // dl is anon
        menuItem = menu_creator.createMenu("subs.prop.is_dl_anon");
        menuItem.setStyle(MenuItem.STYLE_CHECK);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.setData(subs.getHistory().getDownloadNetworks() != null);
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                try {
                    boolean is_anon = subs.getHistory().getDownloadNetworks() != null;
                    subs.getHistory().setDownloadNetworks(is_anon ? null : AENetworkClassifier.AT_NON_PUBLIC);
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        });
        // post notification
        menuItem = menu_creator.createMenu("subs.noti.post");
        menuItem.setStyle(MenuItem.STYLE_CHECK);
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                menu.setData(subs.getHistory().getNotificationPostEnabled());
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(Subscription subs) {
                try {
                    subs.getHistory().setNotificationPostEnabled(!subs.getHistory().getNotificationPostEnabled());
                } catch (Throwable e) {
                    Debug.out(e);
                }
            }
        });
        // max results
        menuItem = menu_creator.createMenu("label.set.max.results");
        menuItem.addFillListener(new MenuItemFillListener() {

            @Override
            public void menuWillBeShown(MenuItem menu, Object data) {
                int max_results = subs.getHistory().getMaxNonDeletedResults();
                if (max_results < 0) {
                    SubscriptionManager subs_man = SubscriptionManagerFactory.getSingleton();
                    max_results = subs_man.getMaxNonDeletedResults();
                }
                String max_results_str = (max_results == 0 ? MessageText.getString("ConfigView.unlimited") : String.valueOf(max_results));
                String text = MessageText.getString("label.set.max.results");
                text += " (" + max_results_str + ")";
                menu.setText(text + "...");
            }
        });
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(final Subscription subs) {
                UISWTInputReceiver entry = new SimpleTextEntryWindow();
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(true);
                int max_results = subs.getHistory().getMaxNonDeletedResults();
                if (max_results < 0) {
                    SubscriptionManager subs_man = SubscriptionManagerFactory.getSingleton();
                    max_results = subs_man.getMaxNonDeletedResults();
                }
                if (max_results > 0) {
                    entry.setPreenteredText(String.valueOf(max_results), false);
                }
                entry.setLocalisedTitle(MessageText.getString("subscriptions.enter.max.results"));
                entry.prompt(new UIInputReceiverListener() {

                    @Override
                    public void UIInputReceiverClosed(UIInputReceiver entry) {
                        if (!entry.hasSubmittedInput()) {
                            return;
                        }
                        String input = entry.getSubmittedInput().trim();
                        try {
                            subs.getHistory().setMaxNonDeletedResults(input.length() == 0 ? -1 : Math.abs(Integer.parseInt(input)));
                        } catch (Throwable e) {
                            Debug.out(e);
                        }
                    }
                });
            }
        });
        // rename
        menuItem = menu_creator.createMenu("MyTorrentsView.menu.rename");
        menuItem.addListener(new SubsMenuItemListener() {

            @Override
            public void selected(final Subscription subs) {
                UISWTInputReceiver entry = new SimpleTextEntryWindow();
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(false);
                entry.setPreenteredText(subs.getName(), false);
                entry.maintainWhitespace(false);
                entry.allowEmptyInput(false);
                entry.setLocalisedTitle(MessageText.getString("label.rename", new String[] { subs.getName() }));
                entry.prompt(new UIInputReceiverListener() {

                    @Override
                    public void UIInputReceiverClosed(UIInputReceiver entry) {
                        if (!entry.hasSubmittedInput()) {
                            return;
                        }
                        String input = entry.getSubmittedInput().trim();
                        if (input.length() > 0) {
                            subs.setLocalName(input);
                        }
                    }
                });
            }
        });
    }
    menuItem = menu_creator.createMenu("Subscription.menu.remove");
    Utils.setMenuItemImage(menuItem, "delete");
    menuItem.addListener(new SubsMenuItemListener() {

        @Override
        public void selected(Subscription subs) {
            removeWithConfirm(subs);
        }
    });
    menu_creator.createMenu("s3").setStyle(MenuItem.STYLE_SEPARATOR);
    menuItem = menu_creator.createMenu("Subscription.menu.properties");
    menuItem.addListener(new SubsMenuItemListener() {

        @Override
        public void selected(Subscription subs) {
            showProperties(subs);
        }
    });
}
Also used : MenuItem(com.biglybt.pif.ui.menus.MenuItem) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) MenuItemFillListener(com.biglybt.pif.ui.menus.MenuItemFillListener) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine) RSSEngine(com.biglybt.core.metasearch.impl.web.rss.RSSEngine)

Example 8 with WebEngine

use of com.biglybt.core.metasearch.impl.web.WebEngine in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public void download(final Subscription subs, final SubscriptionResult original_result) {
    String download_link = original_result.getDownloadLink();
    if (download_link == null) {
        log(subs.getName() + ": can't download " + original_result.getID() + " as no direct download link available");
        return;
    }
    final String key = subs.getID() + ":" + original_result.getID();
    final String dl = download_link;
    synchronized (active_result_downloaders) {
        if (active_result_downloaders.contains(key)) {
            return;
        }
        log(subs.getName() + ": queued result for download - " + original_result.getID() + "/" + download_link);
        active_result_downloaders.add(key);
        result_downloader.run(new AERunnable() {

            @Override
            public void runSupport() {
                // need to fix up to the latest history due to the lazy nature of things :(
                SubscriptionResult result = subs.getHistory().getResult(original_result.getID());
                boolean success = false;
                try {
                    if (result == null) {
                        log(subs.getName() + ": result has been deleted - " + original_result.getID());
                        success = true;
                    } else if (result.getRead()) {
                        log(subs.getName() + ": result already marked as read, skipping - " + result.getID());
                        success = true;
                    } else {
                        boolean retry = true;
                        boolean use_ref = subs.getHistory().getDownloadWithReferer();
                        boolean tried_ref_switch = false;
                        while (retry) {
                            retry = false;
                            try {
                                TorrentUtils.setTLSDescription("Subscription: " + subs.getName());
                                URL original_url = new URL(dl);
                                PluginProxy plugin_proxy = null;
                                if (dl.startsWith("tor:")) {
                                    String target_resource = dl.substring(4);
                                    original_url = new URL(target_resource);
                                    Map<String, Object> options = new HashMap<>();
                                    options.put(AEProxyFactory.PO_PEER_NETWORKS, new String[] { AENetworkClassifier.AT_TOR });
                                    plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download of '" + target_resource + "'", original_url, options, true);
                                    if (plugin_proxy == null) {
                                        throw (new Exception("No Tor plugin proxy available for '" + dl + "'"));
                                    }
                                }
                                URL current_url = plugin_proxy == null ? original_url : plugin_proxy.getURL();
                                Torrent torrent = null;
                                try {
                                    while (true) {
                                        try {
                                            ResourceDownloaderFactory rdf = StaticUtilities.getResourceDownloaderFactory();
                                            ResourceDownloader url_rd = rdf.create(current_url, plugin_proxy == null ? null : plugin_proxy.getProxy());
                                            if (plugin_proxy != null) {
                                                url_rd.setProperty("URL_HOST", plugin_proxy.getURLHostRewrite() + (current_url.getPort() == -1 ? "" : (":" + current_url.getPort())));
                                            }
                                            String referer = use_ref ? subs.getReferer() : null;
                                            UrlUtils.setBrowserHeaders(url_rd, referer);
                                            Engine engine = subs.getEngine();
                                            if (engine instanceof WebEngine) {
                                                WebEngine we = (WebEngine) engine;
                                                if (we.isNeedsAuth()) {
                                                    String cookies = we.getCookies();
                                                    if (cookies != null && cookies.length() > 0) {
                                                        url_rd.setProperty("URL_Cookie", cookies);
                                                    }
                                                }
                                            }
                                            ResourceDownloader mr_rd = rdf.getMetaRefreshDownloader(url_rd);
                                            InputStream is = mr_rd.download();
                                            torrent = new TorrentImpl(TOTorrentFactory.deserialiseFromBEncodedInputStream(is));
                                            break;
                                        } catch (Throwable e) {
                                            if (plugin_proxy == null) {
                                                plugin_proxy = AEProxyFactory.getPluginProxy("Subscription result download", original_url);
                                                if (plugin_proxy != null) {
                                                    current_url = plugin_proxy.getURL();
                                                    continue;
                                                }
                                            }
                                            throw (e);
                                        }
                                    }
                                } finally {
                                    if (plugin_proxy != null) {
                                        plugin_proxy.setOK(torrent != null);
                                    }
                                }
                                byte[] hash = torrent.getHash();
                                // PlatformTorrentUtils.setContentTitle(torrent, torr );
                                DownloadManager dm = PluginInitializer.getDefaultInterface().getDownloadManager();
                                Download download;
                                // if we're assigning a tag/networks then we need to add it stopped in case the tag has any pre-start actions (e.g. set initial save location)
                                // this is because the assignments are done in SubscriptionManagerImpl on the download(willbe)added event
                                boolean stop_override = subs.getTagID() >= 0 || subs.getHistory().getDownloadNetworks() != null;
                                boolean auto_start = manager.shouldAutoStart(torrent);
                                manager.addPrepareTrigger(hash, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                try {
                                    if (auto_start && !stop_override) {
                                        download = dm.addDownload(torrent);
                                    } else {
                                        download = dm.addDownloadStopped(torrent, null, null);
                                    }
                                } finally {
                                    manager.removePrepareTrigger(hash);
                                }
                                log(subs.getName() + ": added download " + download.getName() + ": auto-start=" + auto_start);
                                // maybe remove this as should be actioned in the trigger?
                                manager.prepareDownload(download, new Subscription[] { subs }, new SubscriptionResult[] { result });
                                subs.addAssociation(hash);
                                if (auto_start && stop_override) {
                                    download.restart();
                                }
                                result.setRead(true);
                                success = true;
                                if (tried_ref_switch) {
                                    subs.getHistory().setDownloadWithReferer(use_ref);
                                }
                            } catch (Throwable e) {
                                log(subs.getName() + ": Failed to download result " + dl, e);
                                if (e instanceof TOTorrentException && !tried_ref_switch) {
                                    use_ref = !use_ref;
                                    tried_ref_switch = true;
                                    retry = true;
                                    log(subs.getName() + ": Retrying " + (use_ref ? "with referer" : "without referer"));
                                }
                            } finally {
                                TorrentUtils.setTLSDescription(null);
                            }
                        }
                    }
                } finally {
                    try {
                        if (!success) {
                            if (dl.startsWith("azplug:") || dl.startsWith("chat:")) {
                                // whatever the outcome these have been handled async
                                result.setRead(true);
                            } else {
                                int rad = manager.getAutoDownloadMarkReadAfterDays();
                                if (rad > 0) {
                                    long rad_millis = rad * 24 * 60 * 60 * 1000L;
                                    long time_found = result.getTimeFound();
                                    if (time_found > 0 && time_found + rad_millis < SystemTime.getCurrentTime()) {
                                        log(subs.getName() + ": result expired, marking as read - " + result.getID());
                                        result.setRead(true);
                                    }
                                }
                            }
                        }
                    } catch (Throwable e) {
                        Debug.out(e);
                    }
                    synchronized (active_result_downloaders) {
                        active_result_downloaders.remove(key);
                    }
                    calculateSchedule();
                }
            }
        });
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) TorrentImpl(com.biglybt.pifimpl.local.torrent.TorrentImpl) InputStream(java.io.InputStream) PluginProxy(com.biglybt.core.proxy.AEProxyFactory.PluginProxy) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) DownloadManager(com.biglybt.pif.download.DownloadManager) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ResourceDownloaderFactory(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderFactory) Download(com.biglybt.pif.download.Download) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Example 9 with WebEngine

use of com.biglybt.core.metasearch.impl.web.WebEngine in project BiglyBT by BiglySoftware.

the class SubscriptionSchedulerImpl method download.

@Override
public boolean download(Subscription subs, boolean is_auto) throws SubscriptionException {
    SubscriptionDownloader downloader;
    AESemaphore sem = null;
    String rate_limits = manager.getRateLimits().trim();
    synchronized (active_subscription_downloaders) {
        if (rate_limits.length() > 0) {
            try {
                Engine engine = subs.getEngine();
                if (engine instanceof WebEngine) {
                    String url_str = ((WebEngine) engine).getSearchUrl(true);
                    String host = new URL(url_str).getHost();
                    String[] bits = rate_limits.split(",");
                    for (String bit : bits) {
                        String[] temp = bit.trim().split("=");
                        if (temp.length == 2) {
                            String lhs = temp[0].trim();
                            if (lhs.equals(host)) {
                                int mins = Integer.parseInt(temp[1].trim());
                                if (mins > 0) {
                                    long now = SystemTime.getMonotonousTime();
                                    Long last = rate_limit_map.get(host);
                                    if (last != null && now - last < mins * 60 * 1000) {
                                        if (is_auto) {
                                            return (false);
                                        } else {
                                            throw (new SubscriptionException("Rate limiting prevents download from " + host));
                                        }
                                    }
                                    rate_limit_map.put(host, now);
                                }
                            }
                        }
                    }
                }
            } catch (SubscriptionException e) {
                throw (e);
            } catch (Throwable e) {
                Debug.out(e);
            }
        }
        List waiting = (List) active_subscription_downloaders.get(subs);
        if (waiting != null) {
            sem = new AESemaphore("SS:waiter");
            waiting.add(sem);
            if (!is_auto) {
                active_subs_download_is_auto = false;
            }
        } else {
            active_subscription_downloaders.put(subs, new ArrayList());
            active_subs_download_is_auto = is_auto;
        }
        downloader = new SubscriptionDownloader(manager, (SubscriptionImpl) subs);
    }
    try {
        if (sem == null) {
            downloader.download();
        } else {
            sem.reserve();
        }
        return (true);
    } finally {
        boolean was_auto;
        synchronized (active_subscription_downloaders) {
            List waiting = (List) active_subscription_downloaders.remove(subs);
            if (waiting != null) {
                for (int i = 0; i < waiting.size(); i++) {
                    ((AESemaphore) waiting.get(i)).release();
                }
            }
            was_auto = active_subs_download_is_auto;
        }
        ((SubscriptionImpl) subs).fireDownloaded(was_auto);
    }
}
Also used : WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) URL(java.net.URL) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine)

Example 10 with WebEngine

use of com.biglybt.core.metasearch.impl.web.WebEngine in project BiglyBT by BiglySoftware.

the class SearchUtils method addMenus.

public static void addMenus(final MenuManager menuManager) {
    final com.biglybt.pif.ui.menus.MenuItem template_menu = menuManager.addMenuItem("sidebar.Search", "Search.menu.engines");
    template_menu.setDisposeWithUIDetach(UIInstance.UIT_SWT);
    template_menu.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_MENU);
    template_menu.addFillListener(new MenuItemFillListener() {

        @Override
        public void menuWillBeShown(com.biglybt.pif.ui.menus.MenuItem menu, Object data) {
            template_menu.removeAllChildItems();
            Engine[] engines = MetaSearchManagerFactory.getSingleton().getMetaSearch().getEngines(true, false);
            Arrays.sort(engines, new Comparator<Engine>() {

                @Override
                public int compare(Engine o1, Engine o2) {
                    return (o1.getName().compareToIgnoreCase(o2.getName()));
                }
            });
            com.biglybt.pif.ui.menus.MenuItem import_menu = menuManager.addMenuItem(template_menu, "menu.import.json.from.clipboard");
            import_menu.addListener(new MenuItemListener() {

                @Override
                public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                    importFromClipboard();
                }
            });
            com.biglybt.pif.ui.menus.MenuItem sep = menuManager.addMenuItem(template_menu, "!sep!");
            sep.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_SEPARATOR);
            for (int i = 0; i < engines.length; i++) {
                final Engine engine = engines[i];
                com.biglybt.pif.ui.menus.MenuItem engine_menu = menuManager.addMenuItem(template_menu, "!" + engine.getName() + "!");
                engine_menu.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_MENU);
                if (!(engine instanceof PluginEngine)) {
                    com.biglybt.pif.ui.menus.MenuItem mi = menuManager.addMenuItem(engine_menu, "MyTorrentsView.menu.exportmenu");
                    mi.addListener(new MenuItemListener() {

                        @Override
                        public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                            final Shell shell = Utils.findAnyShell();
                            shell.getDisplay().asyncExec(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    FileDialog dialog = new FileDialog(shell, SWT.SYSTEM_MODAL | SWT.SAVE);
                                    dialog.setFilterPath(TorrentOpener.getFilterPathData());
                                    dialog.setText(MessageText.getString("metasearch.export.select.template.file"));
                                    dialog.setFilterExtensions(VuzeFileHandler.getVuzeFileFilterExtensions());
                                    dialog.setFilterNames(VuzeFileHandler.getVuzeFileFilterExtensions());
                                    String path = TorrentOpener.setFilterPathData(dialog.open());
                                    if (path != null) {
                                        if (!VuzeFileHandler.isAcceptedVuzeFileName(path)) {
                                            path = VuzeFileHandler.getVuzeFileName(path);
                                        }
                                        try {
                                            engine.exportToVuzeFile(new File(path));
                                        } catch (Throwable e) {
                                            Debug.out(e);
                                        }
                                    }
                                }
                            });
                        }
                    });
                    com.biglybt.pif.ui.menus.MenuItem copy_mi = menuManager.addMenuItem(engine_menu, "menu.export.json.to.clipboard");
                    copy_mi.addListener(new MenuItemListener() {

                        @Override
                        public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                            final Shell shell = Utils.findAnyShell();
                            shell.getDisplay().asyncExec(new AERunnable() {

                                @Override
                                public void runSupport() {
                                    try {
                                        ClipboardCopy.copyToClipBoard(engine.exportToVuzeFile().exportToJSON());
                                    } catch (Throwable e) {
                                        Debug.out(e);
                                    }
                                }
                            });
                        }
                    });
                    final Subscription subs = engine.getSubscription();
                    if (subs != null) {
                        com.biglybt.pif.ui.menus.MenuItem copy_uri = menuManager.addMenuItem(engine_menu, "label.copy.uri.to.clip");
                        copy_uri.addListener(new MenuItemListener() {

                            @Override
                            public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                                final Shell shell = Utils.findAnyShell();
                                shell.getDisplay().asyncExec(new AERunnable() {

                                    @Override
                                    public void runSupport() {
                                        try {
                                            ClipboardCopy.copyToClipBoard(subs.getURI());
                                        } catch (Throwable e) {
                                            Debug.out(e);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (engine instanceof WebEngine) {
                        final WebEngine we = (WebEngine) engine;
                        if (we.isNeedsAuth()) {
                            String cookies = we.getCookies();
                            if (cookies != null && cookies.length() > 0) {
                                mi = menuManager.addMenuItem(engine_menu, "Subscription.menu.resetauth");
                                mi.addListener(new MenuItemListener() {

                                    @Override
                                    public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                                        we.setCookies(null);
                                    }
                                });
                            }
                        }
                    }
                }
                if (!(engine instanceof PluginEngine)) {
                    if (engine_menu.getItems().length > 0) {
                        com.biglybt.pif.ui.menus.MenuItem mi = menuManager.addMenuItem(engine_menu, "Subscription.menu.sep");
                        mi.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_SEPARATOR);
                    }
                    com.biglybt.pif.ui.menus.MenuItem mi = menuManager.addMenuItem(engine_menu, "Button.remove");
                    mi.addListener(new MenuItemListener() {

                        @Override
                        public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                            engine.setSelectionState(Engine.SEL_STATE_FORCE_DESELECTED);
                        }
                    });
                    mi = menuManager.addMenuItem(engine_menu, "Subscription.menu.sep2");
                    mi.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_SEPARATOR);
                }
                if (engine_menu.getItems().length > 0) {
                    com.biglybt.pif.ui.menus.MenuItem mi = menuManager.addMenuItem(engine_menu, "Subscription.menu.sep2");
                    mi.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_SEPARATOR);
                }
                com.biglybt.pif.ui.menus.MenuItem mi = menuManager.addMenuItem(engine_menu, "Subscription.menu.properties");
                mi.addListener(new MenuItemListener() {

                    @Override
                    public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
                        showProperties(engine);
                    }
                });
            }
        }
    });
    com.biglybt.pif.ui.menus.MenuItem chat_menu = menuManager.addMenuItem("sidebar.Search", "label.chat");
    chat_menu.setDisposeWithUIDetach(UIInstance.UIT_SWT);
    MenuBuildUtils.addChatMenu(menuManager, chat_menu, new MenuBuildUtils.ChatKeyResolver() {

        @Override
        public String getChatKey(Object object) {
            return ("Search Templates");
        }
    });
    com.biglybt.pif.ui.menus.MenuItem export_menu = menuManager.addMenuItem("sidebar.Search", "search.export.all");
    export_menu.setDisposeWithUIDetach(UIInstance.UIT_SWT);
    export_menu.setStyle(com.biglybt.pif.ui.menus.MenuItem.STYLE_PUSH);
    export_menu.addListener(new MenuItemListener() {

        @Override
        public void selected(com.biglybt.pif.ui.menus.MenuItem menu, Object target) {
            exportAll();
        }
    });
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) MenuItem(org.eclipse.swt.widgets.MenuItem) PluginEngine(com.biglybt.core.metasearch.impl.plugin.PluginEngine) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) MenuItemFillListener(com.biglybt.pif.ui.menus.MenuItemFillListener) Shell(org.eclipse.swt.widgets.Shell) MenuItemListener(com.biglybt.pif.ui.menus.MenuItemListener) Subscription(com.biglybt.core.subs.Subscription) FileDialog(org.eclipse.swt.widgets.FileDialog) VuzeFile(com.biglybt.core.vuzefile.VuzeFile) File(java.io.File) WebEngine(com.biglybt.core.metasearch.impl.web.WebEngine) Engine(com.biglybt.core.metasearch.Engine) PluginEngine(com.biglybt.core.metasearch.impl.plugin.PluginEngine) MenuBuildUtils(com.biglybt.ui.swt.MenuBuildUtils)

Aggregations

WebEngine (com.biglybt.core.metasearch.impl.web.WebEngine)11 Engine (com.biglybt.core.metasearch.Engine)8 Subscription (com.biglybt.core.subs.Subscription)5 URL (java.net.URL)4 RSSEngine (com.biglybt.core.metasearch.impl.web.rss.RSSEngine)2 SubscriptionDownloadListener (com.biglybt.core.subs.SubscriptionDownloadListener)2 SubscriptionException (com.biglybt.core.subs.SubscriptionException)2 SubscriptionResult (com.biglybt.core.subs.SubscriptionResult)2 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)2 MenuItemFillListener (com.biglybt.pif.ui.menus.MenuItemFillListener)2 File (java.io.File)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 Shell (org.eclipse.swt.widgets.Shell)2 PluginEngine (com.biglybt.core.metasearch.impl.plugin.PluginEngine)1 PluginProxy (com.biglybt.core.proxy.AEProxyFactory.PluginProxy)1 SubscriptionHistory (com.biglybt.core.subs.SubscriptionHistory)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 AERunnable (com.biglybt.core.util.AERunnable)1 VuzeFileComponent (com.biglybt.core.vuzefile.VuzeFileComponent)1 VuzeFileHandler (com.biglybt.core.vuzefile.VuzeFileHandler)1