Update of "api-action"
Overview
Artifact ID: | 8befbb868435c7899957d5e96ea0b358a7ba7e32 |
---|---|
Page Name: | api-action |
Date: | 2017-02-20 15:15:56 |
Original User: | mario |
Mimetype: | text/html |
Parent: | ada8ea7ce243e93cc9e16c4186a30ea5cd4ce410 (diff) |
Content
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> <tr bgcolor="#7799ee"> <td valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>action</strong></big></big></font></td ><td align=right valign=bottom ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="streamtuner2/action.py"> streamtuner2/action.py</a></font></td></tr></table> <p><tt># encoding: UTF-8<br> # api: streamtuner2<br> # type: functions<br> # category: io<br> # title: play/record actions<br> # description: Starts audio applications, guesses MIME types for URLs<br> # version: 1.2.1<br> # priority: core<br> #<br> # Multimedia interface for starting audio players, recording app,<br> # or web browser (listed as "url/http" association in players).<br> # It maps audio MIME types, and extracts/converts playlist types<br> # (PLS, M3U, XSPF, SMIL, JSPF, ASX, raw urls).<br> #<br> # Each channel plugin has a .listtype which defines the linked<br> # audio playlist format. It's "pls", seldomly "m3u", or "xspf".<br> # Some channels list raw "srv" addresses, while Youtube "href"<br> # entries point to Flash videos.<br> #<br> # As fallback the playlist URL is retrieved and its MIME type<br> # checked, then its content regexped to guess the list format.<br> # Lastly a playlist format suitable for audio players recreated.<br> # Which is somewhat of a security feature; playlists get cleaned<br> # up this way. The conversion is not strictly necessary, because<br> # baseline PLS/M3U is understood by most players.<br> #<br> # And finally this module is also used by exporting and playlist<br> # importing features (e.g. by the drag'n'drop module).<br> #<br> # Still needs some rewrites to transition off the [url] lists,<br> # and work with full [rows] primarily. (And perhaps it should be<br> # renamed to "playlist" module now).</tt></p> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#aa55cc"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> <tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="api-ahttp">ahttp</a><br> <a href="https://docs.python.org/3/library/copy.html">copy</a><br> <a href="https://docs.python.org/3/library/json.html">json</a><br> </td><td width="25%" valign=top><a href="os.html">os</a><br> <a href="https://docs.python.org/3/library/pipes.html">pipes</a><br> <a href="https://docs.python.org/3/library/platform.html">platform</a><br> </td><td width="25%" valign=top><a href="re.html">re</a><br> <a href="https://docs.python.org/3/library/subprocess.html">subprocess</a><br> <a href="https://docs.python.org/3/library/sys.html">sys</a><br> </td><td width="25%" valign=top></td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ee77aa"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="__builtin__.html#object">__builtin__.object</a> </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="action.html#extract_playlist">extract_playlist</a> </font></dt><dt><font face="helvetica, arial"><a href="action.html#save_playlist">save_playlist</a> </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom> <br> <font color="#000000" face="helvetica, arial"><a name="extract_playlist">class <strong>extract_playlist</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr> <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> <td colspan=2><tt># Extract URLs and meta infos (titles) from playlist formats<br> # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾<br> # It's mostly regex-based at the moment, because that's more<br> # resilient against mailformed XSPF or JSON. But specialized<br> # import helpers can be added as needed.<br> </tt></td></tr> <tr><td> </td> <td width="100%">Methods defined here:<br> <dl><dt><a name="extract_playlist-__init__"><strong>__init__</strong></a>(self, text<font color="#909090">=None</font>, fn<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="extract_playlist-decode"><strong>decode</strong></a>(self, val, unesc)</dt><dd><tt># String decoding</tt></dd></dl> <dl><dt><a name="extract_playlist-field"><strong>field</strong></a>(self, name, rules, src_part)</dt><dd><tt># Single field</tt></dd></dl> <dl><dt><a name="extract_playlist-jamj"><strong>jamj</strong></a>(self)</dt><dd><tt># Jamendo JAMJAMJSON playlists</tt></dd></dl> <dl><dt><a name="extract_playlist-mime_guess"><strong>mime_guess</strong></a>(self, url)</dt><dd><tt># Probe url "extensions" for common media types<br> # (only care about the common audio formats, don't need an exact match or pre-probing in practice)</tt></dd></dl> <dl><dt><a name="extract_playlist-mkrow"><strong>mkrow</strong></a>(self, row, title<font color="#909090">=None</font>)</dt><dd><tt># Add placeholder fields to extracted row</tt></dd></dl> <dl><dt><a name="extract_playlist-pls"><strong>pls</strong></a>(self)</dt><dd><tt># More exact PLS extraction (for the unlikely case entries were misordered)</tt></dd></dl> <dl><dt><a name="extract_playlist-probe_ext"><strong>probe_ext</strong></a>(self, url)</dt><dd><tt># Test URL/path "extension" for ".pls" / ".m3u" etc.</tt></dd></dl> <dl><dt><a name="extract_playlist-probe_fmt"><strong>probe_fmt</strong></a>(self)</dt><dd><tt># Probe MIME type and content per regex</tt></dd></dl> <dl><dt><a name="extract_playlist-rows"><strong>rows</strong></a>(self, fmt<font color="#909090">=None</font>)</dt><dd><tt># Extract only URLs from given source type</tt></dd></dl> <dl><dt><a name="extract_playlist-uniq"><strong>uniq</strong></a>(self, rows)</dt><dd><tt># Filter out duplicate urls</tt></dd></dl> <dl><dt><a name="extract_playlist-urls"><strong>urls</strong></a>(self, fmt)</dt><dd><tt># Return just URL list from extracted playlist</tt></dd></dl> <hr> Data descriptors defined here:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary for instance variables (if defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list of weak references to the object (if defined)</tt></dd> </dl> <hr> Data and other attributes defined here:<br> <dl><dt><strong>extr_urls</strong> = {'asf': {'unesc': 'xml', 'url': r' (?m) ^ \s*Ref\d+ = (\w+://[^\s]+) '}, 'asx': {'split': ' (?ix) <entry[^>]*> ', 'title': ' (?ix) <title> ([^<>]+) ', 'unesc': 'xml', 'url': ' (?ix) <ref <font color="#c040c0">\\</font>b[^>]+<font color="#c040c0">\\</font>b href <font color="#c040c0">\\</font>s*=<font color="#c040c0">\\</font>s* [<font color="#c040c0">\\\'\\</font>"] (<font color="#c040c0">\\</font>w+://[^<font color="#c040c0">\\</font>s<font color="#c040c0">\\</font>"<font color="#c040c0">\\\'</font>]+) [<font color="#c040c0">\\\'\\</font>"] '}, 'desktop': {'genre': '(?m) ^Categories=(.+)', 'playing': '(?m) ^Comment=(.+)', 'title': '(?m) ^Name=(.+)', 'url': r'(?m) ^URL=(\w+://.+)'}, 'jamj': {'unesc': 'json', 'url': r' (?x) \"audio\" \s*:\s* \"(\w+:\\?/\\?/[^\"\s]+)\" '}, 'json': {'genre': r' (?x) \"(?:genre|keywords|category)\" \s*:\s* \"([^\"]+)\" ', 'homepage': r' (?x) \"(?:homepage|website|info)\" \s*:\s* \"([^\"]+)\" ', 'playing': r' (?x) \"(?:playing|current|description)\" \s*:\s* \"([^\"]+)\" ', 'title': r' (?x) \"(?:title|name|station)\" \s*:\s* \"([^\"]+)\" ', 'unesc': 'json', 'url': r' (?x) \"(?:url|audio|stream)\" \s*:\s* \"(\w+:\\?/\\?/[^\"\s]+)\" '}, 'jspf': {'split': r'(?s) \"track\":\s*\{ >', 'unesc': 'json', 'url': r'(?s) \"location\" \s*:\s* \"(\w+://[^\"\s]+)\" '}, 'm3u': {'split': r'(?m) (?=^\#)', 'title': r'(?m) ^ \#EXTINF [-:\d,]* (.+)', 'url': r'(?m) ^( \w+:// [^#\n]+ )'}, 'pls': {'title': r'(?m) ^Title\d* \s*=\s*(.+)', 'url': r'(?m) ^File\d* \s*=\s* (\w+://[^\s]+) '}, 'qtl': {'unesc': 'xml', 'url': ' <embed<font color="#c040c0">\\</font>s+src=[<font color="#c040c0">\\</font>"<font color="#c040c0">\\\'</font>]([^<font color="#c040c0">\\</font>"<font color="#c040c0">\\\'</font>]+)[<font color="#c040c0">\\</font>"<font color="#c040c0">\\\'</font>]<font color="#c040c0">\\</font>s*/>'}, 'raw': {'title': r'(?i)Title[\W]+(.+)', 'unesc': '*', 'url': ' (?i) ( [<font color="#c040c0">\\</font>w+]+:// [^<font color="#c040c0">\\</font>s<font color="#c040c0">\\</font>"<font color="#c040c0">\\\'\\</font>><font color="#c040c0">\\</font>#]+ ) '}, ...}</dl> <dl><dt><strong>fn</strong> = ''</dl> <dl><dt><strong>src</strong> = ''</dl> </td></tr></table> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom> <br> <font color="#000000" face="helvetica, arial"><a name="save_playlist">class <strong>save_playlist</strong></a>(<a href="__builtin__.html#object">__builtin__.object</a>)</font></td></tr> <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> <td colspan=2><tt># Save rows[] in one of the export formats<br> # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾<br> # → The <a href="#save_playlist-export">export</a>() version uses urls[] and a template row{} as input,<br> # converts it into a list of complete rows{} beforehand. It's mostly<br> # utilized to expand a source playlist, merge in alternative streaming<br> # server addresses.<br> #<br> # → With <a href="#save_playlist-store">store</a>() a full set of rows[] is required to begin with, as<br> # it performs a complete serialization. Can save directly to a file.<br> # Which is often used directly by export functions, when no internal<br> # .pls/.m3u urls should be expanded or converted.<br> #<br> # Note that this can chain to <a href="#-convert_playlist">convert_playlist</a>() itself. So there's<br> # some danger for neverending loops in here. Never happened, but some<br> # careful source= and dest= parameter use is advised. Use source="asis"<br> # or "srv" to leave addresses alone, or "href" for input probing.<br> </tt></td></tr> <tr><td> </td> <td width="100%">Methods defined here:<br> <dl><dt><a name="save_playlist-__init__"><strong>__init__</strong></a>(self, source<font color="#909090">='asis'</font>, multiply<font color="#909090">=False</font>)</dt><dd><tt># constructor</tt></dd></dl> <dl><dt><a name="save_playlist-asx"><strong>asx</strong></a>(self, rows)</dt><dd><tt># ASX</tt></dd></dl> <dl><dt><a name="save_playlist-desktop"><strong>desktop</strong></a>(self, rows)</dt><dd><tt># .DESKTOP links</tt></dd></dl> <dl><dt><a name="save_playlist-export"><strong>export</strong></a>(self, urls<font color="#909090">=[]</font>, row<font color="#909090">={}</font>, dest<font color="#909090">='pls'</font>, title<font color="#909090">=None</font>)</dt><dd><tt># Used by playlist_convert(), to transform a list of extracted URLs<br> # into a local .pls/.m3u collection again. Therefore injects the<br> # `title` back into each of the URL rows / or uses row{} template.</tt></dd></dl> <dl><dt><a name="save_playlist-file"><strong>file</strong></a>(self, rows, dest, fn)</dt><dd><tt># save directly</tt></dd></dl> <dl><dt><a name="save_playlist-json"><strong>json</strong></a>(self, rows)</dt><dd><tt># JSON (native lists of streamtuner2)</tt></dd></dl> <dl><dt><a name="save_playlist-jspf"><strong>jspf</strong></a>(self, rows)</dt><dd><tt># JSPF</tt></dd></dl> <dl><dt><a name="save_playlist-m3u"><strong>m3u</strong></a>(self, rows)</dt><dd><tt># M3U</tt></dd></dl> <dl><dt><a name="save_playlist-pls"><strong>pls</strong></a>(self, rows)</dt><dd><tt># PLS</tt></dd></dl> <dl><dt><a name="save_playlist-qtl"><strong>qtl</strong></a>(self, rows)</dt><dd><tt># QTL</tt></dd></dl> <dl><dt><a name="save_playlist-smil"><strong>smil</strong></a>(self, rows)</dt><dd><tt># SMIL</tt></dd></dl> <dl><dt><a name="save_playlist-store"><strong>store</strong></a>(self, rows<font color="#909090">=None</font>, dest<font color="#909090">='pls'</font>)</dt><dd><tt># Export a playlist from rows{}</tt></dd></dl> <dl><dt><a name="save_playlist-url"><strong>url</strong></a>(self, rows)</dt><dd><tt># .URL shortcuts</tt></dd></dl> <dl><dt><a name="save_playlist-xspf"><strong>xspf</strong></a>(self, rows)</dt><dd><tt># XSPF</tt></dd></dl> <dl><dt><a name="save_playlist-xspf_row"><strong>xspf_row</strong></a>(self, row, map)</dt><dd><tt># individual tracks</tt></dd></dl> <hr> Data descriptors defined here:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary for instance variables (if defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list of weak references to the object (if defined)</tt></dd> </dl> <hr> Data and other attributes defined here:<br> <dl><dt><strong>multiply</strong> = True</dl> <dl><dt><strong>source</strong> = 'pls'</dl> <dl><dt><strong>xspf_map</strong> = {'description': 'info', 'homepage': 'info', 'playing': 'annotation', 'title': 'title', 'url': 'location'}</dl> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#eeaa77"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr> <tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td> <td width="100%"><dl><dt><a name="-browser"><strong>browser</strong></a>(url)</dt><dd><tt># Start web browser</tt></dd></dl> <dl><dt><a name="-cleanup_tmp_files"><strong>cleanup_tmp_files</strong></a>()</dt><dd><tt># Callback from main / after gtk_main_quit</tt></dd></dl> <dl><dt><a name="-convert_playlist"><strong>convert_playlist</strong></a>(url, source, dest, local_file<font color="#909090">=True</font>, row<font color="#909090">={}</font>)</dt><dd><tt># Substitute streaming address with desired playlist format<br> # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾<br> # Converts input rows/urls, probes for playlist format, fetches them<br> # and possibly converts remote .pls to local .m3u/.xpsf filename or<br> # just returns direct "srv" urls.<br> #<br> # · Takes a single input `url` (and original row{} as template).<br> # · But returns a list of [urls] after playlist extraction.<br> # · If repackaging as .m3u/.pls/.xspf, returns the local [fn].</tt></dd></dl> <dl><dt><a name="-help"><strong>help</strong></a>(*args)</dt><dd><tt># Open help browser, chm, or streamtuner2 pages</tt></dd></dl> <dl><dt><a name="-http_probe_get"><strong>http_probe_get</strong></a>(url)</dt><dd><tt># Tries to fetch a resource, aborts on ICY responses.</tt></dd></dl> <dl><dt><a name="-interpol"><strong>interpol</strong></a>(cmd, source<font color="#909090">='pls'</font>, row<font color="#909090">={}</font>, add_default<font color="#909090">=True</font>)</dt><dd><tt># Replaces instances of %m3u, %pls, %srv in a command string<br> # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾<br> # · Also understands short aliases %l, %f, %d.<br> # · And can embed %title or $genre placeholders (may use either % or $).<br> # · Replace .pls URL with local .m3u file depending on map.</tt></dd></dl> <dl><dt><a name="-listfmt"><strong>listfmt</strong></a>(t<font color="#909090">='pls'</font>)</dt><dd><tt># Convert e.g. "text/x-scpls" MIME types to just "pls" monikers</tt></dd></dl> <dl><dt><a name="-mime_app"><strong>mime_app</strong></a>(fmt, cmd_list)</dt><dd><tt># Convert MIME type into list of ["audio/xyz", "audio/*", "*/*"]<br> # for comparison against configured record/play association.</tt></dd></dl> <dl><dt><a name="-play"><strong>play</strong></a>(row<font color="#909090">={}</font>, audioformat<font color="#909090">='audio/mpeg'</font>, source<font color="#909090">='pls'</font>)</dt><dd><tt># Calls player for stream url and format</tt></dd></dl> <dl><dt><a name="-quote"><strong>quote</strong></a>(ins)</dt><dd><tt># OS shell command escaping</tt></dd></dl> <dl><dt><a name="-record"><strong>record</strong></a>(row<font color="#909090">={}</font>, audioformat<font color="#909090">='audio/mpeg'</font>, source<font color="#909090">='href'</font>, append<font color="#909090">=None</font>)</dt><dd><tt># Call streamripper / youtube-dl / wget</tt></dd></dl> <dl><dt><a name="-resolve_urn"><strong>resolve_urn</strong></a>(row)</dt><dd><tt># Is called upon rows containing an url starting with "urn:service:#id",<br> # calls the handler from the channel plugin to look up the page and find<br> # the actual streaming url</tt></dd></dl> <dl><dt><a name="-run"><strong>run</strong></a>(cmd)</dt><dd><tt># Exec wrapper</tt></dd></dl> <dl><dt><a name="-run_fmt_url"><strong>run_fmt_url</strong></a>(row<font color="#909090">={}</font>, audioformat<font color="#909090">='audio/mpeg'</font>, source<font color="#909090">='pls'</font>, assoc<font color="#909090">={}</font>, append<font color="#909090">=None</font>, cmd<font color="#909090">=None</font>, add_default<font color="#909090">=True</font>)</dt><dd><tt># Invokes player/recorder for stream url and format</tt></dd></dl> <dl><dt><a name="-tmp_fn"><strong>tmp_fn</strong></a>(row, ext<font color="#909090">='pls'</font>)</dt><dd><tt># Generate filename for temporary .pls/m3u, with unique id</tt></dd></dl> </td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#55aa55"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> <td width="100%"><strong>conf</strong> = {u'filter_walledgardens': True, u'radionomy_page...e/mario/.config/streamtuner2', u'history': u'20'}<br> <strong>handler</strong> = {}<br> <strong>listfmt_t</strong> = {'*/*': 'href', 'application/json': 'json', 'application/smil': 'smil', 'application/vnd.apple.mpegurl': 'm3u', 'application/vnd.ms-wpl': 'smil', 'application/x-shockwave-flash': 'href', 'application/xspf+xml': 'xspf', 'audio/mpegurl': 'm3u', 'audio/x-mpegurl': 'm3u', 'audio/x-ms-wax': 'asx', ...}<br> <strong>log</strong> = <config.log_printer object><br> <strong>main</strong> = None<br> <strong>mediafmt_t</strong> = {'audio/aac': 'aac', 'audio/aacp': 'aac', 'audio/it+zip': 'mod', 'audio/midi': 'midi', 'audio/mod': 'mod', 'audio/mpeg': 'mp3', 'audio/ogg': 'ogg', 'audio/s3+zip': 'mod', 'audio/xm+zip': 'mod'}<br> <strong>placeholder_map</strong> = {'asx': '(%asx)', 'jspf': '(%jspf | %j)', 'm3u': '(%m3u | %f | %g | %m)', 'pls': '(%url | %pls | %u | %l | %r)', 'smil': '(%smil)', 'srv': '(%srv | %d | %s)', 'xspf': '(%xspf | %xpsf | %x)'}<br> <strong>playlist_content_map</strong> = [('pls', r' (?i)\[playlist\].*NumberOfEntries '), ('xspf', r' <\?xml .* <playlist .* ((?i)http://xspf\.org)/ns/0/ '), ('m3u', r' ^ \s* \#(EXT)?M3U '), ('asx', r' (?i) <asx\b '), ('smil', ' <smil[^>]*> .* <seq> '), ('html', ' (?i)<(audio|video)<font color="#c040c0">\\</font>b[^>]+<font color="#c040c0">\\</font>bsrc<font color="#c040c0">\\</font>s*=<font color="#c040c0">\\</font>s*["<font color="#c040c0">\'</font>]?https?:// '), ('wpl', r' <\?wpl \s+ version="1\.0" \s* \?> '), ('b4s', ' <WinampXML> '), ('qtl', r' <?quicktime\d+type="application/x-quicktime-media-link"\d*?> '), ('jspf', r' ^ \s* \{ \s* "playlist": \s* \{ '), ('asf', r' ^ \[Reference\] .*? ^Ref\d+= '), ('url', r' ^ \[InternetShortcut\] .*? ^URL= '), ('desktop', r' ^ \[Desktop Entry\] .*? ^Link= '), ('json', r' "url": \s* "\w+:\\?/\\?/ '), ('jamj', r' "audio": \s* "\w+:\\?/\\?/ '), ('gvp', r' ^gvp_version:1\.\d+$ '), ('href', ' .* ')]<br> <strong>playlist_fmt_prio</strong> = ['pls', 'xspf', 'asx', 'smil', 'jamj', 'json', 'm3u', 'asf', 'raw']<br> <strong>tmp_files</strong> = []</td></tr></table>