Personal website https://benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2011-08-05-earwigbot-progress-wiki-toolset.md 3.7 KiB

13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ---
  2. layout: post
  3. title: EarwigBot Progress: Wiki Toolset
  4. description: YAWTF (Yet Another Wiki Tools Framework, or Yet Another... WTF?)
  5. ---
  6. So I've been spending the past week and a half working on EarwigBot's new
  7. wikitools framework thing (to avoid confusion with Mr.Z-man's
  8. `python-wikitools` package, I'm referring to it as "EarwigBot's Wiki Toolset"
  9. in the docs, even though it's `wiki.tools` internally). Basically, it's the
  10. interface between EarwigBot and the MediaWiki API.
  11. As Josh put it, this is "the thing that actually makes it work".
  12. So, now you can do this (from within Python's interpreter, a wiki bot task, or
  13. an IRC command):
  14. {% highlight pycon %}
  15. >>> from wiki import tools
  16. >>> site = tools.get_site()
  17. >>> print site.name()
  18. enwiki
  19. >>> print site.project()
  20. wikipedia
  21. >>> print site.lang()
  22. en
  23. >>> print site.domain()
  24. en.wikipedia.org
  25. {% endhighlight %}
  26. Our `config.json` file stores site information, along with our chosen "default
  27. site". Pretty neat, huh? "But what can it actually do?" I hear you ask? Well,
  28. for example, we can get information about users:
  29. {% highlight pycon %}
  30. >>> user = site.get_user("The Earwig")
  31. >>> print user.editcount()
  32. 11079
  33. >>> print user.groups()
  34. [u'*', u'user', u'autoconfirmed', u'abusefilter', u'sysop']
  35. >>> reg = user.registration()
  36. >>> import time
  37. >>> print time.strftime("%a, %d %b %Y %H:%M:%S", reg)
  38. Thu, 03 Jul 2008 21:51:34
  39. {% endhighlight %}
  40. and pages as well, with intelligent namespace logic:
  41. {% highlight pycon %}
  42. >>> page = site.get_page("Wikipedia:Articles for creation")
  43. >>> print page.url()
  44. http://en.wikipedia.org/wiki/Wikipedia:Articles_for_creation
  45. >>> print page.namespace()
  46. 4
  47. >>> print site.namespace_id_to_name(4)
  48. Wikipedia
  49. >>> print site.namespace_id_to_name(4, all=True)
  50. [u'Wikipedia', u'Project', u'WP']
  51. >>> print page.is_talkpage()
  52. False
  53. >>> talkpage = page.toggle_talk()
  54. >>> print talkpage.title()
  55. Wikipedia talk:Articles for creation
  56. >>> print talkpage.is_talkpage()
  57. True
  58. {% endhighlight %}
  59. and with support for redirect following:
  60. {% highlight pycon %}
  61. >>> page = site.get_page("Main page")
  62. >>> print page.is_redirect()
  63. True
  64. >>> print page.get()
  65. #REDIRECT [[Main Page]]
  66. [[Category:Protected redirects]]
  67. [[Category:Main Page| ]]
  68. >>> print page.get_redirect_target()
  69. Main Page
  70. >>> page = site.get_page("Main page", follow_redirects=True)
  71. >>> print page.is_redirect()
  72. False # would only be True if "Main page" is a double redirect
  73. >>> print page.get()
  74. <!-- BANNER ACROSS TOP OF PAGE -->
  75. {| id="mp-topbanner" style="width:100%; background:#f9f9f9; margin:1.2em 0 6px 0; border:1px solid #ddd;"
  76. | style="width:61%; color:#000;" |
  77. ...
  78. {% endhighlight %}
  79. Of course, a Wiki Toolset would be nothing without login! Our username and
  80. password are stored (encrypted with Blowfish) in the bot's `config.json` file,
  81. and we login automatically whenever we create a new Site object &ndash; unless
  82. we're already logged in, of course, and we know that based on whether we have
  83. valid login cookies.
  84. {% highlight pycon %}
  85. >>> user = site.get_user() # gets the logged-in user
  86. >>> print user.name()
  87. EarwigBot
  88. {% endhighlight %}
  89. Cookies are stored in a special `.cookies` file in the project root (with no
  90. access given to other users, of course). We support both per-project login and
  91. CentralAuth, meaning I can do...
  92. {% highlight pycon %}
  93. >>> es = tools.get_site("eswiki")
  94. >>> print es.get_user().name()
  95. EarwigBot
  96. {% endhighlight %}
  97. without making additional logins. One thing I strove for when designing the
  98. toolset was as minimal API usage as possible &ndash; we accept gzipped data, we
  99. don't make API queries unless they're actually requested, and we combine
  100. queries whenever possible. Of course, I'm probably doing it all wrong, but
  101. it seems to be working so far.
  102. So... yeah. Carry on then!
  103. :&mdash;earwig