-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhatebu_rec.py
executable file
·76 lines (65 loc) · 1.67 KB
/
hatebu_rec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
import feedparser
# 注目エントリーから記事を取得する
def get_hot():
d = feedparser.parse("http://b.hatena.ne.jp/entrylist?mode=rss&sort=hot&threshold=3")
items = []
for e in d.entries[0:29]:
items.append({'url':e.link, 'title':e.title})
return items
import simplejson
import urllib
# 記事からユーザを取得する
def get_urlposts(url):
url = 'http://b.hatena.ne.jp/entry/json/?url=' + url
lines = urllib.urlopen(url)
for line in lines:
line = line.strip(')')
line = line.strip('(')
json = simplejson.loads(line)
try:
users = []
for u in json['bookmarks']:
users.append({'user':u['user']})
except AttributeError:
users = ''
except TypeError:
users = ''
return users
# ユーザから記事を取得する
def get_userposts(user):
url = 'http://b.hatena.ne.jp/' + user + '/atomfeed'
page = 0
limit = 2
items = []
while(page < limit):
d = feedparser.parse(url + "?of=" + str(page*30))
for e in d.entries:
try:
tags = []
for t in e.tags:
tags.append(t['term'])
except AttributeError:
tags = ''
items.append({'url':e.links[0].href, 'title':e.title, 'tag':tags})
page += 1
return items
# 記事からタグを取得する
def get_itemtags(url):
tags = []
url = 'http://b.hatena.ne.jp/entry/json/?url=' + url
lines = urllib.urlopen(url)
for line in lines:
line = line.strip(')')
line = line.strip('(')
try:
json = simplejson.loads(line)
for u in json['bookmarks']:
tags.extend(u['tags'])
except TypeError:
tags = ''
except AttributeError:
tags = ''
except ValueError:
tags = ''
return tags