-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
154 lines (115 loc) · 3.81 KB
/
core.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!uesr/bin/env python
# encoding: utf-8
"""
Created by Naotaka Jay HOTTA on 2010-12-10
Copyright (c) 2010 CMScom. All rights reserved.
"""
# import StringIO
import logging
import codecs
import os
import re
import urllib2
# from xml.sax.saxutils import unescape
from xml.etree import ElementTree
from html2rst import html2text
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
filename=os.path.join(os.getcwd(), '/tmp/web2sphinx.log'),
filemode='w')
class Web2SphinxBase(object):
"""
"""
URL = ""
PAGE_LIST_FILE = 'pages_list.txt'
HTML_SAVE_DIR = 'html'
REST_SAVE_DIR = 'rest/root'
def __init__(self):
"""
"""
base_os_path = os.getcwd()
self.page_list_File = os.path.join(base_os_path, self.PAGE_LIST_FILE)
self.html_save_dir = os.path.join(base_os_path, self.HTML_SAVE_DIR)
self.rest_save_dir = os.path.join(base_os_path, self.REST_SAVE_DIR)
def _html_validate(self, html):
return html
def _path_localize(self, dom):
return dom
def _change_charactor(self, text, org, new):
text = re.sub(org, new, text)
# print text
return text
def _clean_charactor(self, text, org):
text = self._change_charactor(text, org, "")
#print text
return text
class mindtouch2Sphinx(Web2SphinxBase):
"""
"""
def __init__(self, url):
"""
"""
self.url = url
self.url_list_api = self.url + "/@api/deki/pages"
def _get_page_content(self, elem):
id = elem.get('id')
title = elem.findtext('.//title')
apiurl = elem.get('href').split("?")[0] + '/contents'
url = elem.findtext('.//uri.ui')
path = elem.findtext('.//path')
path_list = path.rsplit('/', 1)
if len(path_list) > 1:
os_path, name = path_list
else:
os_path, name = ('', path_list[0] or 'root')
return (name, id, title, apiurl, os_path, url)
def save_data_file(self, root_path, path, file_name, body):
dir_path = os.path.join(root_path, path)
file_path = os.path.join(dir_path, file_name)
try:
os.makedirs(dir_path)
except OSError:
pass
with open(file_path, 'w') as f:
f = codecs.lookup('utf-8')[-1](f)
f.write(body)
logging.info(file_path)
print file_path
def get_page_content(self, path, name, title, apiurl):
try:
s = urllib2.urlopen(apiurl)
xml = s.read()
# print xml
except Exception, e:
print "Error: root api ", e
else:
s.close()
dom = ElementTree.fromstring(xml)
import pdb; pdb.set_trace()
# dom = _link_path_localize(dom)
body = dom.findtext('.//body')
html = "<html><head><title>%s</title></head><body>%s</body></html>" % (title, body,)
rst = html2text(html)
self.save_data_file(self.HTML_SAVE_DIR, path, name + '.html', html)
self.save_data_file(self.REST_SAVE_DIR, path, name + '.rst', rst)
def get_pages(self, root='/'):
try:
s = urllib2.urlopen(self.url_list_api)
try:
xml = s.read()
except Exception, e:
print "Error: root api ", e
finally:
s.close()
except Exception, e:
print "Error: root api", e
dom = ElementTree.fromstring(xml)
pages = dom.findall('.//page')
for page in pages:
data = self._get_page_content(page)
self. get_page_content(data[4], data[0], data[2], data[3])
if __name__ == '__main__':
"""
"""
pass
# get_pages()