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.8 KiB

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