-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (131 loc) · 4.52 KB
/
main.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import urllib.parse
try:
from flup.server.fcgi import WSGIServer
except ImportError:
from wsgiref.simple_server import make_server
import requests
import noormags
import googlebooks
import noorlib
import adinebook
import urls
import doi
import isbn
import commons
import config
if config.lang == 'en':
import html_en as html
else:
import html_fa as html
def mylogger():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(
filename='yadkard.log',
mode='a',
maxBytes=20000,
backupCount=0,
encoding='utf-8',
delay=0
)
handler.setLevel(logging.INFO)
fmt = '\n%(asctime)s\n%(levelname)s\n%(message)s\n'
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
def application(environ, start_response):
qdict = urllib.parse.parse_qs(environ['QUERY_STRING'])
action = qdict.get('action', [''])[0] # apiquery
user_input = qdict.get('user_input', [''])[0]
# Warning: input is not escaped!
user_input = user_input.strip()
date_format = qdict.get('dateformat', [''])[0]
date_format = date_format.strip()
if not user_input.startswith('http'):
url = 'http://' + user_input
else:
url = user_input
netloc = urllib.parse.urlparse(url)[1]
try:
response = None
if not user_input:
# on first run user_input is ''
response = html.default_response
elif '.google.com/books' in url:
response = googlebooks.Response(url, date_format)
elif 'noormags.' in netloc:
response = noormags.Response(url, date_format)
elif 'noorlib.ir' in netloc:
response = noorlib.Response(url, date_format)
elif ('adinebook' in netloc) or ('adinehbook' in netloc):
response = adinebook.Response(url, date_format)
if not response:
# DOI and ISBN check
en_url = urllib.parse.unquote(commons.uninum2en(url))
try:
m = doi.re.search(doi.doi_regex, doi.html.unescape(en_url))
if m:
response = doi.Response(
m.group(1),
pure=True,
date_format=date_format
)
elif isbn.re.search(isbn.isbn13_regex, en_url):
response = isbn.Response(
isbn.re.search(isbn.isbn13_regex, en_url).group(0),
pure=True,
date_format=date_format,
)
elif isbn.re.search(isbn.isbn10_regex, en_url):
response = isbn.Response(
isbn.re.search(isbn.isbn10_regex, en_url).group(0),
pure=True,
date_format=date_format,
)
except isbn.IsbnError:
pass
if not response:
response = urls.Response(url, date_format)
if not response:
# All the above cases have been unsuccessful
response = html.undefined_url_response
logger.info('There was an undefined_url_response\n' + url)
if action == 'apiquery':
response_body = response.api_json()
else:
response_body = html.response_to_template(response)
except (requests.ConnectionError):
logger.exception(url)
if action == 'apiquery':
response_body = html.httperror_response.api_json()
else:
response_body = html.response_to_template(html.httperror_response)
except Exception as e:
logger.exception(url)
if action == 'apiquery':
response_body = html.other_exception_response.api_json()
else:
response_body = html.response_to_template(
html.other_exception_response)
status = '200 OK'
response_headers = [
('Content-Type', 'text/html; charset=UTF-8'),
('Content-Length', '')
]
start_response(status, response_headers)
return [response_body.encode()]
logger = mylogger()
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("langid").setLevel(logging.WARNING)
try:
# on remote server
WSGIServer(application).run()
except NameError:
# on local computer:
httpd = make_server('localhost', 8051, application)
httpd.serve_forever()