PageRenderTime 114ms CodeModel.GetById 48ms RepoModel.GetById 5ms app.codeStats 0ms

/docs/_build/html/_modules/pyhole/plugins/urls.html

https://github.com/msparks/pyhole
HTML | 155 lines | 137 code | 18 blank | 0 comment | 0 complexity | 0fd29d0582ce843432e0a7ef00a32300 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>pyhole.plugins.urls &mdash; pyhole v0.5.5-7a22d documentation</title>
  7. <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../../../',
  12. VERSION: '0.5.5-7a22d',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../../../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../../../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../../../_static/doctools.js"></script>
  21. <link rel="top" title="pyhole v0.5.5-7a22d documentation" href="../../../index.html" />
  22. <link rel="up" title="Module code" href="../../index.html" />
  23. </head>
  24. <body>
  25. <div class="related">
  26. <h3>Navigation</h3>
  27. <ul>
  28. <li class="right" style="margin-right: 10px">
  29. <a href="../../../genindex.html" title="General Index"
  30. accesskey="I">index</a></li>
  31. <li class="right" >
  32. <a href="../../../py-modindex.html" title="Python Module Index"
  33. >modules</a> |</li>
  34. <li><a href="../../../index.html">pyhole v0.5.5-7a22d documentation</a> &raquo;</li>
  35. <li><a href="../../index.html" accesskey="U">Module code</a> &raquo;</li>
  36. </ul>
  37. </div>
  38. <div class="document">
  39. <div class="documentwrapper">
  40. <div class="bodywrapper">
  41. <div class="body">
  42. <h1>Source code for pyhole.plugins.urls</h1><div class="highlight"><pre>
  43. <span class="c"># Copyright 2011 Josh Kearney</span>
  44. <span class="c">#</span>
  45. <span class="c"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
  46. <span class="c"># you may not use this file except in compliance with the License.</span>
  47. <span class="c"># You may obtain a copy of the License at</span>
  48. <span class="c">#</span>
  49. <span class="c"># http://www.apache.org/licenses/LICENSE-2.0</span>
  50. <span class="c">#</span>
  51. <span class="c"># Unless required by applicable law or agreed to in writing, software</span>
  52. <span class="c"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
  53. <span class="c"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
  54. <span class="c"># See the License for the specific language governing permissions and</span>
  55. <span class="c"># limitations under the License.</span>
  56. <span class="sd">&quot;&quot;&quot;Pyhole URL Plugin&quot;&quot;&quot;</span>
  57. <span class="kn">from</span> <span class="nn">BeautifulSoup</span> <span class="kn">import</span> <span class="n">BeautifulSoup</span>
  58. <span class="kn">from</span> <span class="nn">pyhole</span> <span class="kn">import</span> <span class="n">plugin</span>
  59. <span class="kn">from</span> <span class="nn">pyhole</span> <span class="kn">import</span> <span class="n">utils</span>
  60. <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>
  61. <span class="sd">&quot;&quot;&quot;Provide access to URL data&quot;&quot;&quot;</span>
  62. <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>
  63. <span class="bp">self</span><span class="o">.</span><span class="n">irc</span> <span class="o">=</span> <span class="n">irc</span>
  64. <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>
  65. <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="bp">None</span>
  66. <span class="nd">@plugin.hook_add_command</span><span class="p">(</span><span class="s">&quot;title&quot;</span><span class="p">)</span>
  67. <span class="nd">@utils.spawn</span>
  68. <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>
  69. <span class="sd">&quot;&quot;&quot;Display the title of the a URL (ex: .title &lt;url&gt;)&quot;&quot;&quot;</span>
  70. <span class="k">if</span> <span class="n">params</span><span class="p">:</span>
  71. <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">&quot; &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
  72. <span class="k">else</span><span class="p">:</span>
  73. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">:</span>
  74. <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>
  75. </div>
  76. <span class="nd">@plugin.hook_add_msg_regex</span><span class="p">(</span><span class="s">&quot;https?:\/\/|www\.&quot;</span><span class="p">)</span>
  77. <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>
  78. <span class="sd">&quot;&quot;&quot;Watch and keep track of the latest URL&quot;&quot;&quot;</span>
  79. <span class="k">try</span><span class="p">:</span>
  80. <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">&quot;full_message&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
  81. <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
  82. <span class="k">return</span>
  83. <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>
  84. <span class="sd">&quot;&quot;&quot;Find the title of a given URL&quot;&quot;&quot;</span>
  85. <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">&quot;http://&quot;</span><span class="p">):</span>
  86. <span class="n">url</span> <span class="o">=</span> <span class="s">&quot;http://&quot;</span> <span class="o">+</span> <span class="n">url</span>
  87. <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>
  88. <span class="k">if</span> <span class="ow">not</span> <span class="n">response</span><span class="p">:</span>
  89. <span class="k">return</span>
  90. <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>
  91. <span class="k">if</span> <span class="n">soup</span><span class="o">.</span><span class="n">head</span><span class="p">:</span>
  92. <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>
  93. <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>
  94. <span class="k">else</span><span class="p">:</span>
  95. <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">&quot;No title found for </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">url</span><span class="p">)</span></div>
  96. </pre></div>
  97. </div>
  98. </div>
  99. </div>
  100. <div class="sphinxsidebar">
  101. <div class="sphinxsidebarwrapper">
  102. <div id="searchbox" style="display: none">
  103. <h3>Quick search</h3>
  104. <form class="search" action="../../../search.html" method="get">
  105. <input type="text" name="q" size="18" />
  106. <input type="submit" value="Go" />
  107. <input type="hidden" name="check_keywords" value="yes" />
  108. <input type="hidden" name="area" value="default" />
  109. </form>
  110. <p class="searchtip" style="font-size: 90%">
  111. Enter search terms or a module, class or function name.
  112. </p>
  113. </div>
  114. <script type="text/javascript">$('#searchbox').show(0);</script>
  115. </div>
  116. </div>
  117. <div class="clearer"></div>
  118. </div>
  119. <div class="related">
  120. <h3>Navigation</h3>
  121. <ul>
  122. <li class="right" style="margin-right: 10px">
  123. <a href="../../../genindex.html" title="General Index"
  124. >index</a></li>
  125. <li class="right" >
  126. <a href="../../../py-modindex.html" title="Python Module Index"
  127. >modules</a> |</li>
  128. <li><a href="../../../index.html">pyhole v0.5.5-7a22d documentation</a> &raquo;</li>
  129. <li><a href="../../index.html" >Module code</a> &raquo;</li>
  130. </ul>
  131. </div>
  132. <div class="footer">
  133. &copy; Copyright 2011, Josh Kearney.
  134. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
  135. </div>
  136. </body>
  137. </html>