My favorites
|
Sign in
silassewell
Random code.
Project Home
Downloads
Wiki
Source
Checkout
|
Browse
|
Changes
|
r285
Source path:
svn
/
trunk
/
2008
/
05
/
05
/
feed_parser
/
feed_parser_to_json.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
import feedparser
def json_make_normal(obj):
if type(obj) in [str, unicode, int, float, bool, dict, set, list, tuple]:
return obj
try: return dict(obj)
except: pass
try: return list(obj)
except: pass
return None
def json_handle(obj):
obj = json_make_normal(obj)
if type(obj) in [str, unicode]:
obj = obj.replace('\\', '\\\\')
obj = obj.replace('"', '\\"')
obj = obj.replace('\b', '\\\b')
obj = obj.replace('\f', '\\\f')
obj = obj.replace('\n', '\\\n')
obj = obj.replace('\r', '\\\r')
obj = obj.replace('\t', '\\\t')
return '"%s"' % obj
elif type(obj) in [int, float]:
return obj
elif type(obj) is bool:
if obj: return 'true'
else: return 'false'
elif type(obj) is type(None):
return 'null'
elif type(obj) is dict:
temp = ''
for key in obj.keys():
temp += '%s:%s, ' % (json_handle(key), json_handle(obj[key]))
return '{%s}' % temp[:-2]
elif type(obj) in [set, list, tuple]:
temp = ''
for value in obj:
temp += '%s, ' % json_handle(value)
return '[%s]' % temp[:-2]
return 'null'
data = feedparser.parse('http://digg.com/rss/index.xml')
print json_handle(data)
Show details
Hide details
Change log
r130
by silassewell on Oct 18, 2008
Diff
Added feed parser to JSON code
Go to:
/trunk/2008/05
/trunk/2008/05/05
/trunk/2008/05/05/feed_parser
...ed_parser/feed_parser_to_json.py
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 1294 bytes, 44 lines
View raw file
Hosted by