Skip to content

Commit

Permalink
fix tests & create superuser & gen ua
Browse files Browse the repository at this point in the history
  • Loading branch information
staugur committed May 19, 2020
1 parent d8ea9c1 commit a2de8cb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_api(self):
self.assertEqual(404, rv.status_code)

def test_comment_login_logout(self):
user = "test_" + generate_random()
user = ("test_" + generate_random()).lower()
pwd = "pwd123"
rv = self.login(user, pwd)
self.assertEqual(200, rv.status_code)
Expand Down
1 change: 1 addition & 0 deletions src/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def echo(msg, color=None):
def exec_createuser(username, password, **kwargs):
"""创建账号"""
ak = rsp("accounts")
username = username.lower()
if check_username(username):
if not password or len(password) < 6:
echo("密码最少6位", "yellow")
Expand Down
28 changes: 27 additions & 1 deletion src/utils/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from uuid import uuid4
from time import time
from datetime import datetime
from random import randrange, sample
from random import randrange, sample, randint, choice
from redis import from_url
from .log import Logger
from ._compat import string_types, text_type, PY2, urlparse
Expand Down Expand Up @@ -280,3 +280,29 @@ def parse_data_uri(datauri):
is_base64=is_base64,
data=data,
))


def gen_ua():
first_num = randint(55, 62)
third_num = randint(0, 3200)
fourth_num = randint(0, 140)
os_type = [
'(Windows NT 6.1; WOW64)',
'(Windows NT 10.0; WOW64)',
'(X11; Linux x86_64)',
'(Macintosh; Intel Mac OS X 10_12_6)'
]
chrome_version = 'Chrome/{}.0.{}.{}'.format(
first_num, third_num, fourth_num
)
ua = ' '.join(
[
'Mozilla/5.0',
choice(os_type),
'AppleWebKit/537.36',
'(KHTML, like Gecko)',
chrome_version,
'Safari/537.36'
]
)
return ua
31 changes: 6 additions & 25 deletions src/utils/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"""

import imghdr
import requests
from posixpath import basename, splitext
from random import choice
from io import BytesIO
from functools import wraps
from base64 import urlsafe_b64decode as b64decode, b64decode as pic64decode
Expand All @@ -23,7 +21,7 @@
from libs.storage import get_storage
from .tool import logger, get_current_timestamp, rsp, sha256, username_pat, \
parse_valid_comma, parse_data_uri, format_apires, url_pat, ALLOWED_EXTS, \
parse_valid_verticaline, parse_valid_colon, is_true
parse_valid_verticaline, parse_valid_colon, is_true, gen_ua
from ._compat import PY2, text_type, urlsplit


Expand Down Expand Up @@ -165,7 +163,7 @@ def dfr(res, default='en-US'):
)
if language == "zh-Hans-CN":
language = "zh-CN"
except:
except (ValueError, TypeError, KeyError, Exception):
language = default
# 翻译转换字典库
trans = {
Expand Down Expand Up @@ -353,34 +351,17 @@ def __init__(self, imgurl, filename=None, allowed_exts=[]):

@property
def Headers(self):
UserAgents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 *OPR*/44.0.2510.1218",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 *2345Explorer*/8.4.1.14855",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *JuziBrowser*",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 *LBBROWSER*",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2441.400 *QQBrowser*/9.5.10632.400",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X *MetaSr* 1.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) *Maxthon*/5.0.2.2000 Chrome/47.0.2526.73 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 *UBrowser*/6.1.2107.204 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 *BIDUBrowser*/8.4 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *TheWorld* 7",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 *YaBrowser*/17.3.1.840 *Yowser*/2.5 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36 *Qiyu*/2.1.0.0",
"Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5",
]
return {"User-Agent": choice(UserAgents)}
return {"User-Agent": gen_ua()}

def __download(self, imgurl):
if imgurl and url_pat.match(imgurl) and \
(splitext(imgurl)[-1] in self._allowed_exts or self._filename):
try:
import requests
resp = requests.get(imgurl, headers=self.Headers, timeout=5)
resp.raise_for_status()
except ImportError:
logger.error("Please install the requests module")
except requests.exceptions.RequestException as e:
logger.debug(e, exc_info=True)
else:
Expand Down
2 changes: 2 additions & 0 deletions src/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def register():
res.update(
msg="The username is invalid or registration is not allowed"
)
else:
res.update(msg="Parameter error")
return res


Expand Down

0 comments on commit a2de8cb

Please sign in to comment.