use of com.frostwire.regex.Matcher in project frostwire by frostwire.
the class JsFunction method extract_function.
private static LambdaN extract_function(final JsContext ctx, String funcname) {
String func_mRegex = String.format("(%1$s" + WS + "*=" + WS + "*function|function" + WS + "+%1$s|[\\{;,]%1$s" + WS + "*=" + WS + "*function|var" + WS + "+%1$s" + WS + "*=" + WS + "*function)" + WS + "*", escape(funcname)) + "\\((?<args>[a-z,]+)\\)\\" + CODE;
final Matcher func_m = Pattern.compile(func_mRegex).matcher(ctx.jscode);
if (!func_m.find()) {
throw new JsError("JsFunction.extract_function(): Could not find JS function " + funcname);
}
final String[] argnames = mscpy(func_m.group("args").split(","));
return build_function(ctx, argnames, func_m.group("code"));
}
use of com.frostwire.regex.Matcher in project frostwire by frostwire.
the class JsFunction method interpret_statement.
private static Object interpret_statement(final JsContext ctx, String stmt, final Map<String, Object> local_vars, final int allow_recursion) {
if (allow_recursion < 0) {
throw new JsError("Recursion limit reached");
}
if (stmt.startsWith("var ")) {
stmt = stmt.substring("var ".length());
}
final Matcher ass_m = Pattern.compile("^(?<out>[a-z]+)(\\[(?<index>.+?)\\])?=(?<expr>.*)$").matcher(stmt);
Lambda1 assign;
String expr;
if (ass_m.find()) {
if (ass_m.group("index") != null) {
final Object lvar = local_vars.get(ass_m.group("out"));
final Object idx = interpret_expression(ctx, ass_m.group("index"), local_vars, allow_recursion);
assert idx instanceof Integer;
assign = new Lambda1() {
@SuppressWarnings("unchecked")
@Override
public Object eval(Object val) {
((List<Object>) lvar).set((Integer) idx, val);
return val;
}
};
expr = ass_m.group("expr");
} else {
final String var = ass_m.group("out");
assign = new Lambda1() {
@Override
public Object eval(Object val) {
local_vars.put(var, val);
return val;
}
};
expr = ass_m.group("expr");
}
} else if (stmt.startsWith("return ")) {
assign = new Lambda1() {
@Override
public Object eval(Object v) {
return v;
}
};
expr = stmt.substring("return ".length());
} else {
// Try interpreting it as an expression
expr = stmt;
assign = new Lambda1() {
@Override
public Object eval(Object v) {
return v;
}
};
}
Object v = interpret_expression(ctx, expr, local_vars, allow_recursion);
return assign.eval(v);
}
use of com.frostwire.regex.Matcher in project frostwire by frostwire.
the class JsFunction method interpret_expression.
@SuppressWarnings("unchecked")
private static Object interpret_expression(final JsContext ctx, String expr, Map<String, Object> local_vars, int allow_recursion) {
if (isdigit(expr)) {
return Integer.valueOf(expr);
}
if (isalpha(expr)) {
return local_vars.get(expr);
}
// try:
// return json.loads(expr)
// except ValueError:
// pass
Object jsl = json_loads(expr);
if (jsl != null) {
return jsl;
}
Matcher m = Pattern.compile("^(?<var>" + VAR + ")\\.(?<member>[^\\(]+)(\\((?<args>[^\\(\\)]*)\\))?$").matcher(expr);
boolean mFind = m.find();
if (!mFind) {
// maybe it's the new pattern?
// oE["do"](a,67)
m = Pattern.compile("^(?<var>" + VAR + ")\\[\"(?<member>[^\\(]+)\"\\](\\((?<args>[^\\(\\)]*)\\))?$").matcher(expr);
mFind = m.find();
}
if (mFind) {
String variable = m.group("var");
String member = m.group("member");
String arg_str = m.group("args");
Object obj = null;
if (local_vars.containsKey(variable)) {
obj = local_vars.get(variable);
} else {
if (!ctx.objects.containsKey(variable)) {
ctx.objects.put(variable, extract_object(ctx, variable));
}
obj = ctx.objects.get(variable);
}
if (arg_str == null) {
// Member access
if (member.equals("length")) {
return len(obj);
}
return ((JsObject) obj).functions.get(member).eval(new Object[] {});
}
if (!expr.endsWith(")")) {
throw new JsError("Error parsing js code");
}
List<Object> argvals = null;
if (arg_str.equals("")) {
argvals = new ArrayList<Object>();
} else {
argvals = new ArrayList<Object>();
for (String v : arg_str.split(",")) {
argvals.add(interpret_expression(ctx, v, local_vars, 20));
}
}
if (member.equals("split")) {
// assert argvals == ('',)
return list(obj);
}
if (member.equals("join")) {
// assert len(argvals) == 1
return join((List<Object>) obj, argvals.get(0));
}
if (member.equals("reverse")) {
// assert len(argvals) == 0
reverse(obj);
return obj;
}
if (member.equals("slice")) {
// assert len(argvals) == 1
return slice(obj, (Integer) argvals.get(0));
}
if (member.equals("splice")) {
// assert isinstance(obj, list)
int index = (Integer) argvals.get(0);
int howMany = (Integer) argvals.get(1);
List<Object> res = new ArrayList<Object>();
List<Object> list = (List<Object>) obj;
for (int i = index; i < Math.min(index + howMany, len(obj)); i++) {
res.add(list.remove(index));
}
return res.toArray();
}
return ((JsObject) obj).functions.get(member).eval(argvals.toArray());
}
m = Pattern.compile("^(?<in>[a-z]+)\\[(?<idx>.+)\\]$").matcher(expr);
if (m.find()) {
Object val = local_vars.get(m.group("in"));
Object idx = interpret_expression(ctx, m.group("idx"), local_vars, allow_recursion - 1);
return ((List<?>) val).get((Integer) idx);
}
m = Pattern.compile("^(?<a>.+?)(?<op>[%])(?<b>.+?)$").matcher(expr);
if (m.find()) {
Object a = interpret_expression(ctx, m.group("a"), local_vars, allow_recursion);
Object b = interpret_expression(ctx, m.group("b"), local_vars, allow_recursion);
return (Integer) a % (Integer) b;
}
m = Pattern.compile("^(?<func>[a-zA-Z]+)\\((?<args>[a-z0-9,]*)\\)$").matcher(expr);
if (m.find()) {
String fname = m.group("func");
if (!ctx.functions.containsKey(fname) && ctx.jscode.length() > 0) {
ctx.functions.put(fname, extract_function(ctx, fname));
}
List<Object> argvals = new ArrayList<Object>();
for (String v : m.group("args").split(",")) {
if (isdigit(v)) {
argvals.add(Integer.valueOf(v));
} else {
argvals.add(local_vars.get(v));
}
}
return ctx.functions.get(fname).eval(argvals.toArray());
}
throw new JsError(String.format("Unsupported JS expression %s", expr));
}
use of com.frostwire.regex.Matcher in project frostwire by frostwire.
the class SeasonalContentSearchSuggestion method attemptToAddSeasonalContentSearchSuggestion.
static void attemptToAddSeasonalContentSearchSuggestion(JMenu menu, JPopupMenu popupMenu, List<String> searchTokens) {
int i = 0;
String nextEpisodeSearchToken = null;
for (String token : searchTokens) {
final Matcher matcher = SEA_EPI_PATTERN.matcher(token.toLowerCase());
if (matcher.find()) {
String season = matcher.group(1);
String episodeStr = matcher.group(2);
int nextEpisode = Integer.parseInt(episodeStr) + 1;
String nextEpisodeStr = (nextEpisode < 10) ? "0" + String.valueOf(nextEpisode) : String.valueOf(nextEpisode);
nextEpisodeSearchToken = "s" + season + "e" + nextEpisodeStr;
break;
}
i++;
}
if (nextEpisodeSearchToken != null) {
StringBuilder buffer = new StringBuilder();
for (int j = 0; j < searchTokens.size(); j++) {
if (j != i) {
buffer.append(searchTokens.get(j));
buffer.append(" ");
} else {
buffer.append(nextEpisodeSearchToken);
buffer.append(" ");
}
}
String suggestedSearch = buffer.toString().trim();
if (menu != null) {
menu.add(new SkinMenuItem(new SeasonalContentSearchSuggestion(suggestedSearch)));
}
if (popupMenu != null) {
popupMenu.add(new SeasonalContentSearchSuggestion(suggestedSearch));
}
}
}
use of com.frostwire.regex.Matcher in project frostwire by frostwire.
the class TorrentRegexSearchPerformer method crawlResult.
protected List<? extends SearchResult> crawlResult(CrawlableSearchResult sr, byte[] data, boolean detectAlbums) throws Exception {
List<SearchResult> list = new LinkedList<>();
if (data == null) {
return list;
}
if (sr instanceof TorrentCrawlableSearchResult) {
// in case we fetched a torrent's info (magnet, or the .torrent itself) to obtain
list.addAll(PerformersHelper.crawlTorrent(this, (TorrentCrawlableSearchResult) sr, data, detectAlbums));
} else {
String unreducedHtml = new String(data, "UTF-8");
if (!isValidHtml(unreducedHtml)) {
LOG.warn("invalid html from " + sr.getClass().getSimpleName() + ".\n" + unreducedHtml);
return list;
}
String html = PerformersHelper.reduceHtml(unreducedHtml, htmlPrefixOffset(unreducedHtml), htmlSuffixOffset(unreducedHtml));
if (html != null) {
Matcher matcher = htmlDetailPagePattern.matcher(html);
try {
// BOOKMARK: this is a good spot to put a break point in-order to test your search performer's regex
if (matcher.find()) {
T searchResult = fromHtmlMatcher(sr, SearchMatcher.from(matcher));
if (searchResult != null) {
list.add(searchResult);
}
} else {
LOG.error("TorrentRegexSearchPerformer.crawlSearchResult(" + sr.getClass().getPackage().getName() + "): Update Necessary: Search broken.\n(please notify dev-team on twitter @frostwire or write to contact@frostwire.com if you keep seeing this message.)\n" + "pattern: " + htmlDetailPagePattern.toString() + "\n" + sr.getDetailsUrl() + "\n\n");
// comment this when in production
LOG.info("================================================================\n\n" + html);
}
} catch (Throwable e) {
throw new Exception("URL:" + sr.getDetailsUrl() + " (" + e.getMessage() + ")", e);
}
} else {
LOG.error("Update Necessary: HTML could not be reduced for optimal search. Search broken for " + sr.getClass().getPackage().getName() + " (please notify dev-team on twitter @frostwire or write to contact@frostwire.com if you keep seeing this message.)");
}
}
return list;
}
Aggregations