public static String fetchPage( URLConnection connection, ChannelAuth auth, String cookie, HashMap<String, String> hdr) { try { // URLConnection connection=url.openConnection(); URL url = connection.getURL(); connection.setConnectTimeout(10000); connection.setRequestProperty("User-Agent", defAgentString); if (auth != null) { Channels.debug("auth " + auth.method + " authstr " + auth.authStr); if (auth.method == ChannelLogin.STD) connection.setRequestProperty("Authorization", auth.authStr); else if (auth.method == ChannelLogin.COOKIE) cookie = append(cookie, "; ", auth.authStr); else if (auth.method == ChannelLogin.APIKEY) { url = new URL(url.toString() + auth.authStr); connection = url.openConnection(); } } Channels.debug("fpage cookie " + cookie); String c1 = ChannelCookie.getCookie(url.toString()); if (!empty(c1)) { if (!cookieContains(c1, cookie)) { cookie = append(cookie, "; ", c1); } } Channels.debug("fpage2 cookie " + cookie + " " + url); if (!empty(cookie)) connection.setRequestProperty("Cookie", cookie); if (hdr != null && hdr.size() != 0) { for (String key : hdr.keySet()) connection.setRequestProperty(key, hdr.get(key)); } // connection.setRequestProperty("Content-Length", "0"); connection.setDoInput(true); connection.setDoOutput(true); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder page = new StringBuilder(); String str; while ((str = in.readLine()) != null) { // page.append("\n"); page.append(str.trim()); page.append("\n"); } in.close(); Channels.restoreProxyDNS(); return page.toString(); } catch (Exception e) { Channels.debug("fetch exception " + e.toString()); Channels.restoreProxyDNS(); return ""; } }
public static String parseASX(String url, int type) { if (type == ChannelUtil.ASXTYPE_NONE) return url; if (type == ChannelUtil.ASXTYPE_AUTO && !isASX(url)) return url; String page; try { page = ChannelUtil.fetchPage(new URL(url).openConnection()); } catch (Exception e) { Channels.debug("asx fetch failed " + e); return url; } Channels.debug("page " + page); int first = page.indexOf("href="); if (first == -1) return url; int last = page.indexOf('\"', first + 6); if (last == -1) return url; return page.substring(first + 6, last); }
public static boolean streamType(String url, String type) { try { URI u = new URI(url); return u.getScheme().startsWith(type); } catch (Exception e) { Channels.debug("execp stream type " + e); return false; } }
public static String execute(ProcessBuilder pb, boolean verbose) { try { Channels.debug("about to execute " + pb.command()); pb.redirectErrorStream(true); Process pid = pb.start(); InputStream is = pid.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); if (verbose) Channels.debug("execute read line " + line); } pid.waitFor(); return sb.toString(); } catch (Exception e) { Channels.debug("executing external script failed " + e); } return null; }
public static String postPage( URLConnection connection, String query, String cookie, HashMap<String, String> hdr) { URL url = connection.getURL(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setDefaultUseCaches(false); // connection.setAllowUserInteraction(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("User-Agent", defAgentString); connection.setRequestProperty("Content-Length", "" + query.length()); try { String c1 = ChannelCookie.getCookie(url.toString()); if (!empty(c1)) { if (!cookieContains(c1, cookie)) cookie = append(cookie, "; ", c1); } if (!empty(cookie)) connection.setRequestProperty("Cookie", cookie); if (hdr != null && hdr.size() != 0) { for (String key : hdr.keySet()) connection.setRequestProperty(key, hdr.get(key)); } connection.setConnectTimeout(10000); connection.connect(); // open up the output stream of the connection if (!empty(query)) { DataOutputStream output = new DataOutputStream(connection.getOutputStream()); output.writeBytes(query); output.flush(); output.close(); } BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder page = new StringBuilder(); String str; while ((str = in.readLine()) != null) { // page.append("\n"); page.append(str.trim()); page.append("\n"); } in.close(); Channels.restoreProxyDNS(); return page.toString(); } catch (Exception e) { Channels.debug("post error " + e); Channels.restoreProxyDNS(); return ""; } }
public static int[] getNameIndex(String[] prop) { try { String x = ChannelUtil.getPropertyValue(prop, "name_index"); if (!empty(x)) { String[] idx = x.split("\\+"); int[] res = new int[idx.length]; for (int i = 0; i < idx.length; i++) { int j = new Integer(idx[i]).intValue(); if (j > 0) res[i] = j; } return res; } } catch (Exception e) { Channels.debug("excep " + e); } return null; }
public static void RemoveFromFavFile(String name, String url) { File f = Channels.workFavFile(); try { String str = FileUtils.readFileToString(f); Channels.debug("removing from fav file " + name); int pos = str.indexOf(url); if (pos > -1) { FileOutputStream out = new FileOutputStream(f, false); pos = str.lastIndexOf(FAV_BAR, pos); // head out.write(str.substring(0, pos).getBytes()); pos = str.indexOf(FAV_BAR, pos + 30); // tail if (pos > -1) out.write(str.substring(pos).getBytes()); out.flush(); out.close(); } } catch (Exception e) { } }
public static void addToFavFile(String data, String name) { File f = Channels.workFavFile(); try { boolean newFile = !f.exists(); Channels.debug("adding to fav file " + name); FileOutputStream out = new FileOutputStream(f, true); if (newFile) { String msg = "## Auto generated favorite file,Edit with care\n\n\n"; out.write(FAV_BAR.getBytes(), 0, FAV_BAR.length()); out.write(msg.getBytes(), 0, msg.length()); } String n = "## Name: " + name + "\r\n\n"; out.write(FAV_BAR.getBytes(), 0, FAV_BAR.length()); out.write(n.getBytes(), 0, n.length()); out.write(data.getBytes(), 0, data.length()); out.flush(); out.close(); } catch (Exception e) { } }
public static boolean downloadBin(String url, File f, boolean text) { try { URL u = new URL(url); URLConnection connection = u.openConnection(); connection.setRequestProperty("User-Agent", ChannelUtil.defAgentString); String cookie = ChannelCookie.getCookie(url); if (!empty(cookie)) connection.setRequestProperty("Cookie", cookie); connection.setDoInput(true); connection.setDoOutput(true); InputStream in = connection.getInputStream(); if (text) return downloadText(in, f); FileOutputStream out = new FileOutputStream(f); byte[] buf = new byte[4096]; int len; while ((len = in.read(buf)) != -1) out.write(buf, 0, len); out.flush(); out.close(); in.close(); return true; } catch (Exception e) { Channels.debug("Error fetching bin file " + e); } return false; }
public static Thread backgroundDownload(String name, String url, boolean cache) { String fName = ChannelUtil.guessExt(Channels.fileName(name, cache), url); if (ChannelUtil.rtmpStream(url)) { try { // URL u=new URL(url); // rtmp stream special fix if (empty(extension(fName))) // no extension. Rtmp is normally mp4 fName = fName + ".mp4"; final ProcessBuilder pb = buildPid(fName, url); if (pb == null) return null; Channels.debug("start process "); Runnable r = new Runnable() { public void run() { ChannelUtil.execute(pb); } }; return new Thread(r); } catch (Exception e) { return null; } } String subFile = ""; if (url.startsWith("http") || url.startsWith("navix") || url.startsWith("subs")) { if (url.startsWith("navix") || url.startsWith("subs")) { int pos = url.indexOf('?'); if (pos == -1) return null; String[] data = url.substring(pos + 1).split("&"); for (int i = 0; i < data.length; i++) { String[] kv = data[i].split("="); if (kv[0].equals("url")) url = ChannelUtil.unescape(kv[1]); if (kv[0].equals("subs")) subFile = unescape(kv[1]); } } if (empty(url)) return null; final String rUrl = url; final String sFile = subFile; if (empty(extension(fName))) // no ext guess mpg as ext fName = fName + ".mpg"; final String fName1 = fName; Runnable r = new Runnable() { public void run() { File f = new File(fName1); if (!empty(sFile)) { File s = new File(sFile); byte[] buf = new byte[4096]; try { FileInputStream in = new FileInputStream(s); FileOutputStream out = new FileOutputStream(new File(f.getParent() + File.separator + s.getName())); while ((in.read(buf)) != -1) out.write(buf); in.close(); out.close(); } catch (Exception e) { // ignore this Channels.debug("Error moving subtitle file " + sFile); } } // download the actaul movie, subtitles are done downloadBin(rUrl, f); } }; return new Thread(r); } return null; }
public static String createMediaUrl(HashMap<String, String> vars, int format, Channel ch) { String rUrl = vars.get("url"); if (empty(rUrl) || Channels.noPlay()) // what do we do? return null; int rtmpMet = Channels.rtmpMethod(); String type = vars.get("__type__"); Channels.debug("create media url entry " + rUrl + " format " + format + " type " + type); if (rUrl.startsWith("http")) { if ((format != Format.VIDEO) || (rtmpMet == Channels.RTMP_MAGIC_TOKEN)) return rUrl; // rUrl="navix://channel?url="+escape(rUrl); rUrl = "channel?url=" + escape(rUrl); String agent = vars.get("agent"); if (empty(agent)) agent = ChannelUtil.defAgentString; rUrl = append(rUrl, "&agent=", escape(agent)); if (!empty(vars.get("referer"))) rUrl = append(rUrl, "&referer=", escape(vars.get("referer"))); String sub = vars.get("subtitle"); if (!empty(sub)) { // we got subtitles rUrl = "subs://" + rUrl; // lot of things to append here rUrl = addSubs(rUrl, sub); } else if (!empty(type) && type.equals("navix")) rUrl = "navix://" + rUrl; else rUrl = vars.get("url"); Channels.debug("return media url " + rUrl); return rUrl; } if (!empty(type) && type.equals("RTMPDUMP")) { Channels.debug("rmtpdump spec " + rUrl); String[] args = rUrl.split("!!!"); String res = ""; for (int i = 0; i < args.length; i++) { if (empty(args[i])) continue; String[] kv = args[i].split("=", 2); if (kv.length < 2) continue; if (kv[0].equals("flv")) continue; res = append(res, "&", kv[0] + "=" + escape(kv[1])); } rUrl = "rtmpdump://channel?" + res; String sub = vars.get("subtitle"); if (!empty(sub)) { // we got subtitles // lot of things to append here rUrl = addSubs(rUrl, sub); } Channels.debug("return media url rtmpdump spec " + rUrl); return rUrl; } String[] s = rUrl.split(" "); if (s.length > 1) { // This is likely rtmp from navix rUrl = s[0]; // first one is the url for (int i = 1; i < s.length; i++) { String[] ss = s[i].split("=", 2); if (ss.length < 2) vars.put(ss[0].toLowerCase(), ""); else vars.put(ss[0].toLowerCase(), ss[1]); } } if (!rtmpStream(rUrl)) // type is sopcast etc. return rUrl; if (rUrl.startsWith("rtmpdump://")) return rUrl; switch (rtmpMet) { case Channels.RTMP_MAGIC_TOKEN: rUrl = ChannelUtil.append(rUrl, "!!!pms_ch_dash_y!!!", vars.get("playpath")); rUrl = ChannelUtil.append(rUrl, "!!!pms_ch_dash_w!!!", vars.get("swfVfy")); break; case Channels.RTMP_DUMP: Channels.debug("rtmpdump method"); rUrl = "rtmpdump://channel?-r=" + escape(rUrl); if (!empty(vars.get("live"))) rUrl = append(rUrl, "", "&-v"); rUrl = ChannelUtil.append(rUrl, "&-y=", escape(vars.get("playpath"))); rUrl = ChannelUtil.append(rUrl, "&--swfVfy=", escape(vars.get("swfVfy"))); rUrl = ChannelUtil.append(rUrl, "&--swfVfy=", escape(vars.get("swfvfy"))); rUrl = ChannelUtil.append(rUrl, "&-s=", escape(vars.get("swfplayer"))); rUrl = ChannelUtil.append(rUrl, "&-a=", escape(vars.get("app"))); rUrl = ChannelUtil.append(rUrl, "&-p=", escape(vars.get("pageurl"))); rUrl = ChannelUtil.append(rUrl, "&-s=", escape(vars.get("swfurl"))); String sub = vars.get("subtitle"); if (!empty(sub)) { // we got subtitles // lot of things to append here rUrl = addSubs(rUrl, sub); } break; default: rUrl = vars.get("url"); break; } Channels.debug("return media url " + rUrl); return rUrl; }
public static String pendData(String src, String[] props, String field, String sep) { String p = getPropertyValue(props, "prepend_" + field); String a = getPropertyValue(props, "append_" + field); Channels.debug("prepend " + p + " append " + a + " src " + src + " f " + field); return append(p, sep, append(src, sep, a)); }