if you got some path/enhancements, you can mail me at my pseudo at gmail.com, i'll update it.
(you should install the marvellous beautifulsoup module, http://www.crummy.com/software/BeautifulSoup/documentation.html)
the snippets.py file :
1
2 from BeautifulSoup import BeautifulSoup
3 import urllib
4
5 class Keyword:
6 def __init__(self,tag,nb):
7 self.tag=tag
8 self.nb=int(nb)
9 def __repr__(self):
10 return "<Keyword '%s' : %d>" % (self.tag,self.nb)
11
12 class Snippet:
13 def __init__(self,title,code,tags):
14 self.title=title
15 self.code=code
16 self.tags = tags
17 def __repr__(self):
18 return "<Snippet '%s' : tags %s>" % (self.title,str(self.tags))
19
20 class Snippets:
21 urlForTags = "http://www.bigbold.com/snippets/tags"
22
23 def __init__(self,l=[]):
24 url = self.__getUrlForTags(l)
25
26
27 fu = urllib.urlopen(url)
28 content = fu.read()
29 fu.close()
30
31 self.tags = l
32 self.keywords,self.snippets = self.__extractContent(content)
33
34 def __repr__(self):
35 return "<Snippets for tags:%s>" % (str(self.tags))
36
37 def __getUrlForTags(self, l ):
38 assert type(l)==list
39 l = [Snippets.urlForTags] + l
40 return "/".join(l)
41
42 def __extractContent(self,content):
43
44 soup = BeautifulSoup( content )
45
46
47 tagTable=soup('div', {'id' : "sidebar"})[0].table
48 keywords=[]
49 for i in tagTable("tr"):
50 td = i("td")
51
52
53 try:
54
55 keywords.append( Keyword(td[1].span.a.string , td[0].string) )
56 except TypeError:
57
58 keywords.append( Keyword(td[2].span.a.string , td[1].string) )
59
60
61 postList=soup('div', {'class' : "post"})
62 snippets=[]
63 for i in postList:
64 divs = i("div")
65
66
67 title = divs[0].h3.a.string
68 tags = [j.string for j in divs[1]("a")][:-1]
69
70
71 list = [j for j in divs[0]][1:]
72 code=""
73 for i in list:
74 try:
75 if i.name == "pre":
76 try:
77 code+=i.string
78 except TypeError:
79 pass
80 except AttributeError:
81
82 out = str(i).strip()
83 if out:
84 code+="#| "+out+"\n"
85
86
87 snippets.append( Snippet(title,code,tags) )
88
89 return keywords,snippets
and an example (all returned "strings" are in utf-8):
1
2 from snippets import Snippets
3
4 s = Snippets(["python","xml"])
5 print s
6 print s.keywords
7 for i in s.snippets:
8 print i
9 print s.snippets[6].title
10 print s.snippets[6].code