/docs/_build/html/_modules/pyhole/plugins/urls.html
HTML | 155 lines | 137 code | 18 blank | 0 comment | 0 complexity | 0fd29d0582ce843432e0a7ef00a32300 MD5 | raw file
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>pyhole.plugins.urls — pyhole v0.5.5-7a22d documentation</title>
- <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
- <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../../../',
- VERSION: '0.5.5-7a22d',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="../../../_static/jquery.js"></script>
- <script type="text/javascript" src="../../../_static/underscore.js"></script>
- <script type="text/javascript" src="../../../_static/doctools.js"></script>
- <link rel="top" title="pyhole v0.5.5-7a22d documentation" href="../../../index.html" />
- <link rel="up" title="Module code" href="../../index.html" />
- </head>
- <body>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../../genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="../../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../../index.html">pyhole v0.5.5-7a22d documentation</a> »</li>
- <li><a href="../../index.html" accesskey="U">Module code</a> »</li>
- </ul>
- </div>
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body">
-
- <h1>Source code for pyhole.plugins.urls</h1><div class="highlight"><pre>
- <span class="c"># Copyright 2011 Josh Kearney</span>
- <span class="c">#</span>
- <span class="c"># Licensed under the Apache License, Version 2.0 (the "License");</span>
- <span class="c"># you may not use this file except in compliance with the License.</span>
- <span class="c"># You may obtain a copy of the License at</span>
- <span class="c">#</span>
- <span class="c"># http://www.apache.org/licenses/LICENSE-2.0</span>
- <span class="c">#</span>
- <span class="c"># Unless required by applicable law or agreed to in writing, software</span>
- <span class="c"># distributed under the License is distributed on an "AS IS" BASIS,</span>
- <span class="c"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
- <span class="c"># See the License for the specific language governing permissions and</span>
- <span class="c"># limitations under the License.</span>
- <span class="sd">"""Pyhole URL Plugin"""</span>
- <span class="kn">from</span> <span class="nn">BeautifulSoup</span> <span class="kn">import</span> <span class="n">BeautifulSoup</span>
- <span class="kn">from</span> <span class="nn">pyhole</span> <span class="kn">import</span> <span class="n">plugin</span>
- <span class="kn">from</span> <span class="nn">pyhole</span> <span class="kn">import</span> <span class="n">utils</span>
- <div class="viewcode-block" id="Url"><a class="viewcode-back" href="../../../plugins.html#pyhole.plugins.urls.Url">[docs]</a><span class="k">class</span> <span class="nc">Url</span><span class="p">(</span><span class="n">plugin</span><span class="o">.</span><span class="n">Plugin</span><span class="p">):</span>
- <span class="sd">"""Provide access to URL data"""</span>
- <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">irc</span><span class="p">,</span> <span class="n">conf_file</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">irc</span> <span class="o">=</span> <span class="n">irc</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="bp">None</span>
- <span class="nd">@plugin.hook_add_command</span><span class="p">(</span><span class="s">"title"</span><span class="p">)</span>
- <span class="nd">@utils.spawn</span>
- <div class="viewcode-block" id="Url.title"><a class="viewcode-back" href="../../../plugins.html#pyhole.plugins.urls.Url.title">[docs]</a> <span class="k">def</span> <span class="nf">title</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
- <span class="sd">"""Display the title of the a URL (ex: .title <url>)"""</span>
- <span class="k">if</span> <span class="n">params</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_find_title</span><span class="p">(</span><span class="n">params</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">_find_title</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
- </div>
- <span class="nd">@plugin.hook_add_msg_regex</span><span class="p">(</span><span class="s">"https?:\/\/|www\."</span><span class="p">)</span>
- <span class="k">def</span> <span class="nf">_watch_for_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
- <span class="sd">"""Watch and keep track of the latest URL"""</span>
- <span class="k">try</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">"full_message"</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
- <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
- <span class="k">return</span>
- <span class="k">def</span> <span class="nf">_find_title</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
- <span class="sd">"""Find the title of a given URL"""</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">url</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">"http://"</span><span class="p">):</span>
- <span class="n">url</span> <span class="o">=</span> <span class="s">"http://"</span> <span class="o">+</span> <span class="n">url</span>
- <span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">irc</span><span class="o">.</span><span class="n">fetch_url</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
- <span class="k">if</span> <span class="ow">not</span> <span class="n">response</span><span class="p">:</span>
- <span class="k">return</span>
- <span class="n">soup</span> <span class="o">=</span> <span class="n">BeautifulSoup</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
- <span class="k">if</span> <span class="n">soup</span><span class="o">.</span><span class="n">head</span><span class="p">:</span>
- <span class="n">title</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">decode_entities</span><span class="p">(</span><span class="n">soup</span><span class="o">.</span><span class="n">head</span><span class="o">.</span><span class="n">title</span><span class="o">.</span><span class="n">string</span><span class="p">)</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">irc</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>
- <span class="k">else</span><span class="p">:</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">irc</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="s">"No title found for </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span></div>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar">
- <div class="sphinxsidebarwrapper">
- <div id="searchbox" style="display: none">
- <h3>Quick search</h3>
- <form class="search" action="../../../search.html" method="get">
- <input type="text" name="q" size="18" />
- <input type="submit" value="Go" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- <p class="searchtip" style="font-size: 90%">
- Enter search terms or a module, class or function name.
- </p>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../../../genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="../../../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="../../../index.html">pyhole v0.5.5-7a22d documentation</a> »</li>
- <li><a href="../../index.html" >Module code</a> »</li>
- </ul>
- </div>
- <div class="footer">
- © Copyright 2011, Josh Kearney.
- Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
- </div>
- </body>
- </html>