1
- import tornado .httpserver
2
- import tornado .ioloop
1
+ import json
2
+ import time
3
+
3
4
import tornado .web
4
- #import memcache
5
- import chardet
6
- import urllib
7
5
import html2text
8
6
import requests
9
- import json
10
- from django .utils .feedgenerator import Rss201rev2Feed , Atom1Feed
11
- from pymongo import Connection
7
+
8
+ from pymongo import MongoClient
12
9
from readability .readability import Document
10
+
11
+ from settings import MONGO_HOST , MONGO_PORT
13
12
from textmetric .metric import calc_readability_metrics
14
- import time
13
+
15
14
16
15
READ_DB = 'readability'
17
16
LOG_COLL = 'log'
18
17
19
18
ERROR_NONE = 0
20
19
ERROR_INVALID_DATA = 101
21
20
21
+
22
22
class RusMeasureHandler (tornado .web .RequestHandler ):
23
23
def initialize (self ):
24
- self .conn = Connection ( )
24
+ self .conn = MongoClient ( MONGO_HOST , MONGO_PORT )
25
25
self .db = self .conn [READ_DB ]
26
26
self .log = self .db [LOG_COLL ]
27
27
self .log .ensure_index ("reqtime" , 1 )
28
28
29
29
def __log (self , logrec ):
30
- self .conn = Connection ()
31
30
self .db = self .conn [READ_DB ]
32
31
self .log = self .db [LOG_COLL ]
33
32
self .log .save (logrec )
34
33
35
-
36
34
def get (self ):
37
35
rtime = time .time ()
38
36
url = self .get_argument ('url' )
@@ -41,7 +39,7 @@ def get(self):
41
39
debug = int (debug ) if debug .isdigit () else 0
42
40
r = requests .get (url )
43
41
ctype = r .headers ['content-type' ].lower () if 'content-type' in r .headers .keys () else 'text/html'
44
- print ctype
42
+ print ( ctype )
45
43
ctype = ctype .split (';' , 1 )[0 ]
46
44
if ctype == 'text/html' :
47
45
ht = html2text .HTML2Text ()
@@ -51,8 +49,7 @@ def get(self):
51
49
text = ht .handle (Document (r .text ).summary ())
52
50
status = ERROR_NONE
53
51
elif ctype == 'text/plain' :
54
-
55
- print type (r .content )
52
+ print (type (r .content ))
56
53
text = r .content .decode ('utf8' , 'ignore' )
57
54
# text = r.text.decode('utf8', 'ignore')
58
55
status = ERROR_NONE
@@ -95,8 +92,6 @@ def post(self):
95
92
self .__log (logreq )
96
93
97
94
98
-
99
-
100
95
application = tornado .web .Application ([
101
96
(r"/api/1.0/ru/measure/" , RusMeasureHandler ),
102
97
])
0 commit comments