Skip to content

Commit 2d4f3c4

Browse files
committed
add sort option to get_tags, add tests for pagerized wiki
1 parent d2b5258 commit 2d4f3c4

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

atwiki/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def __init__(self, uri, **kwargs):
2727
self._user_agent = kwargs.get('user_agent', 'Mozilla/5.0 (AtWikiPython)')
2828
self._sleep = kwargs.get('sleep', 10)
2929

30-
def get_list(self, tag=None):
31-
index = 1
30+
def get_list(self, tag=None, _start=1):
31+
index = _start
3232
while True:
3333
count = 0
3434
if tag:
@@ -52,11 +52,11 @@ def get_list(self, tag=None):
5252
index += 1
5353
time.sleep(self._sleep)
5454

55-
def get_tags(self):
56-
index = 1
55+
def get_tags(self, sort='', _start=1):
56+
index = _start
5757
while True:
5858
count = 0
59-
soup = self._request(self._uri.tags('', index))
59+
soup = self._request(self._uri.tags(sort, index))
6060
links = soup.find('div', attrs={'class': 'cmd_tag'}).findAll('a', attrs={'class': 'tag'})
6161
for link in links:
6262
tag_name = link.text

atwiki/test/test_core.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import absolute_import, division, print_function, unicode_literals
44

5+
import math
6+
import re
57
from unittest import TestCase
68

79
from atwiki.core import AtWikiAPI
@@ -59,3 +61,51 @@ def test_search_or(self):
5961
def test_search_none(self):
6062
results = list(self._api.search('no_result_expected_for_this'))
6163
self.assertEqual(len(results), 0)
64+
65+
66+
class PagerizeTest(TestCase):
67+
def setUp(self):
68+
self._uri = AtWikiURI('https://w.atwiki.jp/hmiku')
69+
self._api = AtWikiAPI(self._uri)
70+
71+
def test_get_list(self):
72+
top_page = next(self._api.get_list())
73+
assert top_page == {'id': 1, 'name': 'トップページ'}
74+
75+
soup = self._api._request(self._uri.list(sort='create', index=1))
76+
text = soup.find('div', class_='pagelist').text
77+
m = re.search(r'計 (\d+) ページ / 1 から 100 を表示', text)
78+
assert m is not None
79+
count = int(m.group(1))
80+
assert 45000 < count < 90000
81+
last_index = math.ceil(count / 100)
82+
83+
# Get list from the last page.
84+
# N.B. The page counter is not updated immediately.
85+
pages = list(self._api.get_list(_start=last_index))
86+
expected = (count % 100)
87+
assert (expected - 5) < len(pages) < (expected + 5)
88+
89+
top_page = next(self._api.get_list(_start=last_index + 1))
90+
assert top_page == {'id': 1, 'name': 'トップページ'}
91+
92+
def test_get_list_tag(self):
93+
soup = self._api._request(self._uri.tag('曲', index=1))
94+
last_index = 1
95+
for link in soup.find('div', class_='cmd_tag').find_all('a'):
96+
if not link.attrs['href'].endswith('&p={}'.format(last_index + 1)):
97+
break
98+
last_index += 1
99+
pages = list(self._api.get_list('曲', _start=last_index))
100+
assert 1 <= len(pages) <= 50
101+
102+
pages = list(self._api.get_list('曲', _start=last_index + 1))
103+
assert len(pages) == 0
104+
105+
def test_get_tags(self):
106+
song = next(self._api.get_tags('num'))
107+
assert song['name'] == '曲'
108+
assert 35000 < song['weight'] < 70000
109+
110+
not_song = next(self._api.get_tags('num', _start=2))
111+
assert not_song['name'] != '曲'

0 commit comments

Comments
 (0)