Skip to content

Commit 0ef021a

Browse files
committed
fix tag enumeration
1 parent 1959501 commit 0ef021a

2 files changed

Lines changed: 9 additions & 15 deletions

File tree

atwiki/core.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
class AtWikiAPI(object):
2323
_PAGER_PATTERN = re.compile(r'.+?(\d+).+?(\d+).+?(\d+).+?') # "計 110 ページ / 1 から 100 を表示"
24+
_TAG_WEIGHT_PATTERN = re.compile(r'\((\d+)\)$') # "タグ名(1)"
2425

2526
def __init__(self, uri, **kwargs):
2627
self._uri = uri
@@ -57,32 +58,25 @@ def get_list(self, tag=None):
5758
time.sleep(self._sleep)
5859

5960
def get_tags(self):
60-
index = 0
61+
index = 1
6162
while True:
6263
count = 0
6364
soup = self._request(self._uri.tag('', index))
6465
links = soup.find('div', attrs={'class': 'cmd_tag'}).findAll('a', attrs={'class': 'tag'})
6566
for link in links:
6667
tag_name = link.text
6768
tag_weight = 0
68-
for clazz in link.attrs['class']:
69-
if clazz.startswith('weight'):
70-
tag_weight = int(clazz[6:])
71-
break
69+
m = self._TAG_WEIGHT_PATTERN.search(link.attrs['title'])
70+
if m:
71+
tag_weight = int(m.group(1))
7272
count += 1
7373
yield {'name': tag_name, 'weight': tag_weight}
7474
if count == 0: break
7575

76-
pagerArea = soup.find('div', attrs={'class': 'cmd_tag'}).find('div')
77-
if pagerArea is None:
78-
# Pager area will not be shown when tag list fits in one page.
79-
assert index == 0
76+
# Find "次の500件" link.
77+
pager = soup.find('div', attrs={'class': 'cmd_tag'}).select_one('a[href$="/tag/?p={}"]'.format(index + 1))
78+
if not pager:
8079
break
81-
pagers = pagerArea.findAll('a')
82-
if len(pagers) == 1:
83-
if pagers[0].attrs['href'].endswith('/?p={}'.format(index - 1)):
84-
# Valid pager found, and no more tags.
85-
break
8680
index += 1
8781
time.sleep(self._sleep)
8882

atwiki/test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_get_tags(self):
2727
results = list(self._api.get_tags())
2828
self.assertEqual(len(results), 1)
2929
self.assertEqual(results[0]['name'], 'tag01')
30-
self.assertEqual(results[0]['weight'], 3)
30+
self.assertEqual(results[0]['weight'], 1)
3131

3232
def test_get_source(self):
3333
self.assertEqual(self._api.get_source(14, 0),

0 commit comments

Comments
 (0)