-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure.py
125 lines (110 loc) · 4.23 KB
/
configure.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
#!/usr/bin/env python
#-*-coding:utf-8-*-
# author : "qiulimao"
# email : "[email protected]"
"""
news module configure
"""
#---------- code begins below -------
from django.core.cache import cache
from news.models import Settings
def getDBConfigure(key,default="0",type_=str):
"""
get configured value from db.
"""
try:
setting = Settings.objects.get(key=key)
except Settings.DoesNotExist:
setting = Settings(key=key,option=str(default))
setting.save()
return type_(setting.option)
def setDBConfigure(key,option):
"""
set configure value in db
"""
try:
setting = Settings.objects.get(key=key)
setting.option = str(option)
setting.save()
except Settings.DoesNotExist:
setting = Settings(key=key,option=str(option))
setting.save()
def durationOfSettings():
"""
配置有效时间,过期之后才会读取新设置的值,最小的粒度为 60s
"""
if not cache.get("DurationOfSettings"):
DurationOfSettings = getDBConfigure("DURATION_OF_SETTINGS",60,type_=int)
cache.set("DurationOfSettings",DurationOfSettings,60)
return cache.get("DurationOfSettings")
def getDaysRangeForSearchResult():
"""
默认搜索时间范围(最近30天)
"""
if not cache.get("DaysRangeForSearchResult"):
DaysRangeForSearchResult = getDBConfigure("DAYS_RANGE_FOR_SEARCH_RESULT",30,type_=int)
DaysRangeCacheLife = durationOfSettings()
cache.set("DaysRangeForSearchResult",DaysRangeForSearchResult,DaysRangeCacheLife)
return cache.get("DaysRangeForSearchResult")
def getMaxCountForSearchResult():
"""
默认搜索结果最大条目数
"""
if not cache.get("MaxCountForSearchResult"):
MaxCountForSearchResult = getDBConfigure("SEARCH_RESULT_MAX_COUNT",200,type_=int)
MaxResultCountCacheLife = durationOfSettings()
cache.set("MaxCountForSearchResult",MaxCountForSearchResult,MaxResultCountCacheLife)
return cache.get("MaxCountForSearchResult")
def getSearchTrace():
"""
是否开启搜索跟踪
"""
if not cache.get("SearchTrace"):
SearchTrace = getDBConfigure("SEARCH_TRACE",default="1",type_=lambda x:bool(int(x)))
MaxSearchTraceCacheLife = durationOfSettings()
cache.set("SearchTrace",SearchTrace,MaxSearchTraceCacheLife)
return cache.get("SearchTrace")
def banSpider():
"""
是否反爬虫
"""
if not cache.get("BanSpider"):
BanSpider = getDBConfigure("BAN_SPIDER",default="0",type_=lambda x:bool(int(x)))
MaxBanSpiderCacheLife = durationOfSettings()
cache.set("BanSpider",BanSpider,MaxBanSpiderCacheLife)
return cache.get("BanSpider")
def getMaxSearchPerDay():
"""
设置每天最多搜索次数
"""
if not cache.get("MaxSearchPerDay"):
MaxSearchPerDay = getDBConfigure("MAX_SEARCH_PER_DAY",default=399,type_=int)
MaxMaxSearchPerDayCacheLife = durationOfSettings()
cache.set("MaxSearchPerDay",MaxSearchPerDay,MaxMaxSearchPerDayCacheLife)
return cache.get("MaxSearchPerDay")
def getMaxItemOnHeadline():
"""
"""
if not cache.get("MaxItemOnHeadline"):
MaxItemOnHeadline = getDBConfigure("MAX_ITEM_ON_HEADLINE",default=19,type_=int)
MaxMaxItemOnHeadlineCacheLife = durationOfSettings()
cache.set("MaxItemOnHeadline",MaxItemOnHeadline,MaxMaxItemOnHeadlineCacheLife)
return cache.get("MaxItemOnHeadline")
def getSearchStrategy():
"""
获取搜索策略
"""
if not cache.get("SearchStrategy"):
SearchStrategy = getDBConfigure("SEARCH_STRATEGY",default="NormalOrder",type_=str)
MaxSearchStrategyCacheLife = durationOfSettings()
cache.set("SearchStrategy",SearchStrategy,MaxSearchStrategyCacheLife)
return cache.get("SearchStrategy")
def getHalfRankForTrend():
"""
获取 加权 新闻活跃值 排行 半衰期
"""
if not cache.get("HalfRankForTrend"):
HalfRankForTrend = getDBConfigure("HALF_RANK_FOR_TREND",default=30,type_=int)
MaxHalfRankForTrendCacheLife = durationOfSettings()
cache.set("HalfRankForTrend",HalfRankForTrend,MaxHalfRankForTrendCacheLife)
return cache.get("HalfRankForTrend")