util.py
1.7 KB
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
# _*_ coding: utf-8 _*_
# @Time : 2020/9/3 17:03
# @Author vanwhebin
from __future__ import absolute_import, unicode_literals
import os
import hashlib
import time
import pytz
import datetime
from uuid import uuid4
from base64 import urlsafe_b64encode
from rest_framework import status
from django.http import JsonResponse
from wxProject.settings import TIME_ZONE, BASE_LOG_DIR
def uuid():
# 生成uuid
return urlsafe_b64encode(uuid4().bytes).decode("ascii").rstrip("=")
def get_yesterday(day=1):
""" 获取昨天日期 """
return (datetime.datetime.now(pytz.timezone(TIME_ZONE)) - datetime.timedelta(days=day)).strftime("%Y-%m-%d")
def response(data="", status_code=status.HTTP_200_OK, code=0, **kwargs):
""" 格式化返回信息 """
res = {
"code": code,
"msg": "ok",
"data": data,
}
res.update(kwargs)
return JsonResponse(res, status=status_code)
def save_log(directory, content, log_type=None):
""" 将爬取数据写入日志文件 """
log_file = str(log_type) + "_" + time.strftime("%Y%m%d", time.localtime(time.time())) + '.log'
cur_dir = os.path.join(BASE_LOG_DIR, directory)
if not os.path.exists(cur_dir):
os.makedirs(cur_dir)
with open(os.path.join(cur_dir, log_file), 'a+', encoding="utf-8") as f:
f.write(str(content) + os.linesep)
def get_md5_hash(file_path, blocksize=65536):
"""
:param file_path: 文件路径
:param blocksize: 文件块大小
:return:
"""
hash_handler = hashlib.md5()
with open(file_path, "rb") as f:
for block in iter(lambda: f.read(blocksize), b""):
hash_handler.update(block)
return hash_handler.hexdigest()