Personal website https://benkurtovic.com/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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

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