Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

"""Handle processing and setting web content into Readability/cleaned 

 

""" 

import httplib 

import logging 

import lxml 

import socket 

import urllib2 

 

from BaseHTTPServer import BaseHTTPRequestHandler as HTTPH 

from breadability.readable import Article 

from urlparse import urlparse 

 

LOG = logging.getLogger(__name__) 

 

 

class DictObj(dict): 

    def __getattr__(self, name): 

        try: 

            return self.__getitem__(name) 

        except KeyError: 

            return super(DictObj, self).__getattr__(name) 

 

 

USER_AGENT = 'bookie / ({url})'.format( 

    url="https://github.com/mitechie/bookie", 

) 

 

 

STATUS_CODES = DictObj({ 

    '1': 1,    # used for manual parsed 

    '200': 200, 

    '404': 404, 

    '403': 403, 

    '429': 429,   # wtf, 429 doesn't exist... 

 

    # errors like 9's 

    '900': 900,   # used for unparseable 

    '901': 901,   # url is not parseable/usable 

    '902': 902,   # socket.error during download 

    '903': 903,   # httplib.IncompleteRead error 

    '904': 904,   # lxml error about document is empty 

    '905': 905,   # httplib.BadStatusLine 

}) 

 

IMAGE_TYPES = DictObj({ 

    'png': 'image/png', 

    'jpeg': 'image/jpeg', 

    'jpg': 'image/jpg', 

    'gif': 'image/gif', 

}) 

 

 

class Readable(object): 

    """Understand the base concept of making readable""" 

    is_error = False 

    content = None 

    content_type = None 

    headers = None 

    status_message = None 

    status = None 

    url = None 

 

    def error(self, code, msg): 

        """This readable request was an error, assign it so""" 

        self.status = code 

        self.status_message = str(msg) 

 

    def is_error(self): 

        """Check if this is indeed an error or not""" 

        if self.status not in [STATUS_CODES['200'], ]: 

            return True 

        else: 

            return False 

 

    def is_image(self): 

        """Check if the current object is an image""" 

        # we can only get this if we have headers 

        LOG.debug('content type') 

        LOG.debug(self.content_type) 

        if (self.content_type is not None and 

                self.content_type.lower() in IMAGE_TYPES.values()): 

            return True 

        else: 

            return False 

 

    def set_content(self, content, content_type=None): 

        """assign the content and potentially content type header""" 

        self.content = content 

        if content_type: 

            self.content_type = content_type 

 

 

class ReadContent(object): 

    """Handle some given content and parse the readable out of it""" 

 

    @staticmethod 

    def parse(content, content_type=None, url=None): 

        """Handle the parsing out of the html content given""" 

        read = Readable() 

        document = Article(content.read(), url=url) 

 

        if not document.readable: 

            read.error(STATUS_CODES['900'], "Could not parse content.") 

        else: 

            read.set_content(document.readable, 

                             content_type=content_type) 

            read.status = STATUS_CODES['1'] 

        return read 

 

 

class ReadUrl(object): 

    """Fetch a url and read some content out of it""" 

 

    @staticmethod 

    def parse(url): 

        """Fetch the given url and parse out a Readable Obj for the content""" 

        read = Readable() 

 

        if not isinstance(url, unicode): 

            url = url.decode('utf-8') 

 

        # first check if we have a special url with the #! content in it 

        if u'#!' in url: 

            # rewrite it with _escaped_fragment_=xxx 

            # we should be doing with this some regex, but cheating for now 

            idx = url.index(u'#') 

            fragment = url[idx:] 

            clean_url = u"{0}?_escaped_fragment_={1}".format(url[0:idx], 

                                                             fragment) 

        else: 

            # we need to clean up the url first, we can't have any anchor tag 

            # on the url or urllib2 gets cranky 

            parsed = urlparse(url) 

 

            if parsed.query is not None and parsed.query != '': 

                query = u'?' 

            else: 

                query = u'' 

 

            clean_url = u"{0}://{1}{2}{query}{3}".format( 

                parsed[0], 

                parsed[1], 

                parsed[2], 

                parsed[4], 

                query=query) 

 

        try: 

            LOG.debug('Readable Parsed: ' + clean_url) 

            request = urllib2.Request(clean_url.encode('utf-8')) 

            request.add_header('User-Agent', USER_AGENT) 

            opener = urllib2.build_opener() 

            fh = opener.open(request) 

 

            # if it works, then we default to a 200 request 

            # it's ok, promise :) 

            read.status = 200 

            read.headers = fh.info() 

            read.content_type = read.headers.gettype() 

 

        except urllib2.HTTPError, exc: 

            # for some reason getting a code 429 from a server 

            if exc.code not in [429]: 

                read.error(exc.code, HTTPH.responses[exc.code]) 

            else: 

                read.error(exc.code, unicode(exc.code) + ': ' + clean_url) 

 

        except httplib.InvalidURL, exc: 

            read.error(STATUS_CODES['901'], str(exc)) 

 

        except urllib2.URLError, exc: 

            read.error(STATUS_CODES['901'], str(exc)) 

 

        except httplib.BadStatusLine, exc: 

            read.error(STATUS_CODES['905'], str(exc)) 

 

        except socket.error, exc: 

            read.error(STATUS_CODES['902'], str(exc)) 

 

        LOG.debug('is error?') 

        LOG.debug(read.status) 

 

        # let's check to make sure we should be parsing this 

        # for example: don't parse images 

        if not read.is_error() and not read.is_image(): 

            try: 

                document = Article(fh.read(), url=clean_url) 

                if not document.readable: 

                    read.error(STATUS_CODES['900'], 

                               "Could not parse document.") 

                else: 

                    read.set_content(document.readable) 

 

            except socket.error, exc: 

                read.error(STATUS_CODES['902'], str(exc)) 

            except httplib.IncompleteRead, exc: 

                read.error(STATUS_CODES['903'], str(exc)) 

            except lxml.etree.ParserError, exc: 

                read.error(STATUS_CODES['904'], str(exc)) 

 

        return read