Skip to content

Commit 8376220

Browse files
committed
Refactor for Py3, add dependencies, locate variables in .env
1 parent 1e5c943 commit 8376220

File tree

7 files changed

+370
-122
lines changed

7 files changed

+370
-122
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
.idea
2-
3+
__pycache__/
34
.venv

api/readabilityio.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
import tornado.httpserver
2-
import tornado.ioloop
1+
import json
2+
import time
3+
34
import tornado.web
4-
#import memcache
5-
import chardet
6-
import urllib
75
import html2text
86
import requests
9-
import json
10-
from django.utils.feedgenerator import Rss201rev2Feed, Atom1Feed
11-
from pymongo import Connection
7+
8+
from pymongo import MongoClient
129
from readability.readability import Document
10+
11+
from settings import MONGO_HOST, MONGO_PORT
1312
from textmetric.metric import calc_readability_metrics
14-
import time
13+
1514

1615
READ_DB = 'readability'
1716
LOG_COLL = 'log'
1817

1918
ERROR_NONE = 0
2019
ERROR_INVALID_DATA = 101
2120

21+
2222
class RusMeasureHandler(tornado.web.RequestHandler):
2323
def initialize(self):
24-
self.conn = Connection()
24+
self.conn = MongoClient(MONGO_HOST, MONGO_PORT)
2525
self.db = self.conn[READ_DB]
2626
self.log = self.db[LOG_COLL]
2727
self.log.ensure_index("reqtime", 1)
2828

2929
def __log(self, logrec):
30-
self.conn = Connection()
3130
self.db = self.conn[READ_DB]
3231
self.log = self.db[LOG_COLL]
3332
self.log.save(logrec)
3433

35-
3634
def get(self):
3735
rtime = time.time()
3836
url = self.get_argument('url')
@@ -41,7 +39,7 @@ def get(self):
4139
debug = int(debug) if debug.isdigit() else 0
4240
r = requests.get(url)
4341
ctype = r.headers['content-type'].lower() if 'content-type' in r.headers.keys() else 'text/html'
44-
print ctype
42+
print(ctype)
4543
ctype = ctype.split(';', 1)[0]
4644
if ctype == 'text/html':
4745
ht = html2text.HTML2Text()
@@ -51,8 +49,7 @@ def get(self):
5149
text = ht.handle(Document(r.text).summary())
5250
status = ERROR_NONE
5351
elif ctype == 'text/plain':
54-
55-
print type(r.content)
52+
print(type(r.content))
5653
text = r.content.decode('utf8', 'ignore')
5754
# text = r.text.decode('utf8', 'ignore')
5855
status = ERROR_NONE
@@ -95,8 +92,6 @@ def post(self):
9592
self.__log(logreq)
9693

9794

98-
99-
10095
application = tornado.web.Application([
10196
(r"/api/1.0/ru/measure/", RusMeasureHandler),
10297
])

api/textmetric/metric.py

+111-92
Large diffs are not rendered by default.

api/tornading.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def daemon():
5555
import tornado.wsgi
5656

5757
#~ # настраиваем Джанго
58-
from readabilityio import application
58+
from api.readabilityio import application
5959
#
6060
# container = tornado.wsgi.WSGIContainer(application)
6161
http_server = tornado.httpserver.HTTPServer(application)
@@ -68,18 +68,18 @@ def start():
6868
if not started:
6969
pid = Popen([HOST, os.path.abspath(__file__), 'daemon'],
7070
executable='python').pid
71-
print 'Server started at port %s (pid: %i)...' % (PORT, pid)
71+
print('Server started at port %s (pid: %i)...' % (PORT, pid))
7272
else:
73-
print 'Server alegry started (pid: %i)' % started
73+
print('Server alegry started (pid: %i)' % started)
7474

7575

7676
def stop():
7777
started = alegry_started()
7878
if started:
7979
os.kill(started, signal.SIGKILL)
80-
print 'Server stoped (pid %i)' % started
80+
print('Server stoped (pid %i)' % started)
8181
else:
82-
print 'Server not started'
82+
print('Server not started')
8383

8484

8585
def restart():
@@ -114,5 +114,5 @@ def alegry_started():
114114
cmd = sys.argv[1]
115115
globals()[cmd]()
116116
else:
117-
print 'Error: invalid command'
118-
print 'Usage: python tornader.py {%s}.' % '|'.join(COMMANDS)
117+
print('Error: invalid command')
118+
print('Usage: python tornader.py {%s}.' % '|'.join(COMMANDS))

0 commit comments

Comments
 (0)