use of com.biglybt.core.metasearch.Engine in project BiglyBT by BiglySoftware.
the class SubscriptionImpl method embedEngines.
protected String embedEngines(String json_in) {
// see if we need to embed private search templates
Map map = JSONUtils.decodeJSON(json_in);
long engine_id = ((Long) map.get("engine_id")).longValue();
String json_out = json_in;
if (engine_id >= Integer.MAX_VALUE || engine_id < 0) {
Engine engine = MetaSearchManagerFactory.getSingleton().getMetaSearch().getEngine(engine_id);
if (engine == null) {
log("Private search template with id '" + engine_id + "' not found!!!!");
} else {
try {
embedEngines(map, engine);
json_out = JSONUtils.encodeToJSON(map);
log("Embedded private search template '" + engine.getName() + "'");
} catch (Throwable e) {
log("Failed to embed private search template", e);
}
}
}
return (json_out);
}
use of com.biglybt.core.metasearch.Engine in project BiglyBT by BiglySoftware.
the class SubscriptionImpl method getNameEx.
@Override
public String getNameEx() {
if (name_ex == null) {
try {
Map map = JSONUtils.decodeJSON(getJSON());
String search_term = (String) map.get("search_term");
Map filters = (Map) map.get("filters");
Engine engine = manager.getEngine(this, map, true);
String engine_name = engine.getNameEx();
if (name.startsWith(engine_name)) {
name_ex = name;
} else if (engine_name.startsWith(name)) {
name_ex = engine_name;
} else {
name_ex = name + ": " + engine.getNameEx();
}
if (search_term != null && search_term.length() > 0) {
name_ex += ", query=" + search_term;
}
if (filters != null && filters.size() > 0) {
name_ex += ", filters=" + new SubscriptionResultFilterImpl(this, filters).getString();
}
} catch (Throwable e) {
name_ex = name + ": " + Debug.getNestedExceptionMessage(e);
}
}
return (name_ex);
}
use of com.biglybt.core.metasearch.Engine in project BiglyBT by BiglySoftware.
the class SubscriptionManagerImpl method createRSS.
@Override
public Subscription createRSS(String name, URL url, int check_interval_mins, boolean is_anonymous, Map user_data) throws SubscriptionException {
checkURL(url);
try {
name = getUniqueName(name);
Engine engine = MetaSearchManagerFactory.getSingleton().getMetaSearch().createRSSEngine(name, url);
String json = SubscriptionImpl.getSkeletonJSON(engine, check_interval_mins);
// engine name may have been modified so re-read it for subscription default
SubscriptionImpl subs = new SubscriptionImpl(this, engine.getName(), engine.isPublic(), is_anonymous, null, json, SubscriptionImpl.ADD_TYPE_CREATE);
if (user_data != null) {
Iterator it = user_data.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
subs.setUserData(entry.getKey(), entry.getValue());
}
}
log("Created new subscription: " + subs.getString());
subs = addSubscription(subs);
if (subs.isPublic()) {
updatePublicSubscription(subs);
}
return (subs);
} catch (Throwable e) {
throw (new SubscriptionException("Failed to create subscription", e));
}
}
use of com.biglybt.core.metasearch.Engine in project BiglyBT by BiglySoftware.
the class SubscriptionManagerImpl method removeSubscription.
protected void removeSubscription(SubscriptionImpl subs) {
synchronized (this) {
if (subscriptions.remove(subs)) {
saveConfig();
} else {
return;
}
}
try {
Engine engine = subs.getEngine(true);
if (engine.getType() == Engine.ENGINE_TYPE_RSS) {
engine.delete();
log("Removed engine " + engine.getName() + " due to subscription removal");
}
} catch (Throwable e) {
log("Failed to check for engine deletion", e);
}
Iterator<SubscriptionManagerListener> it = listeners.iterator();
while (it.hasNext()) {
try {
it.next().subscriptionRemoved(subs);
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
try {
FileUtil.deleteResilientFile(getResultsFile(subs));
synchronized (result_cache) {
result_cache.remove(subs);
}
File vuze_file = getVuzeFile(subs);
vuze_file.delete();
new File(vuze_file.getParent(), vuze_file.getName() + ".bak").delete();
} catch (Throwable e) {
log("Failed to delete results/vuze file", e);
}
}
use of com.biglybt.core.metasearch.Engine 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();
}
}
});
}
}
Aggregations