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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 26 17:57:38 2018
@author: fangyucheng
"""
class Std_fields_video:
def __init__(self, data_provider=None):
if data_provider==None:
data_provider='gengmei'
self.video_data={
'platform': None,
'channel': None,
# 'channel_url': None,
# 'channel_subdomain': None,
'describe': None,
'title': None,
'url': None,
'duration': 0,
'releaser': None,
'play_count': None,
'favorite_count': 0,
'comment_count': 0,
# 'dislike_count': None,
'repost_count': None,
'isOriginal': None,
'data_provider': data_provider,
'video_id': None,
'releaserUrl': None,
'release_time': 0,
'fetch_time': 0,
}
def field_type_correct(self):
def none_is_allowed(field, field_type):
if field not in self.video_data:
return 'field "%s" is absent' % field
is_correct=(isinstance(self.video_data[field], field_type)
or self.video_data[field]==None)
if is_correct:
return True
else:
return 'field "%s" should be of [%s] type or None' % (field, field_type.__name__)
def none_is_not_allowed(field, field_type):
if field not in self.video_data:
return 'FATAL: field "%s" is NOT FOUND!' % field
else:
is_correct=isinstance(self.video_data[field], field_type)
if is_correct:
return True
else:
return 'field "%s" should be of [%s] type' % (field, field_type.__name__)
platform_chk=none_is_not_allowed('platform', str)
duration_chk=none_is_not_allowed('duration', int)
play_count_chk=none_is_not_allowed('play_count', int)
favorite_count_chk=none_is_allowed('favorite_count', int)
comment_count_chk=none_is_allowed('comment_count', int)
# dislike_count_chk=none_is_allowed('dislike_count', int)
repost_count_chk=none_is_allowed('repost_count', int)
isOriginal_chk=none_is_allowed('isOriginal', bool)
release_time_chk=none_is_allowed('release_time', int)
fetch_time_chk=none_is_not_allowed('fetch_time', int)
type_chk={
'platform': platform_chk,
'duration': duration_chk,
'play_count': play_count_chk,
'favorite_count': favorite_count_chk,
'comment_count': comment_count_chk,
# 'dislike_count': dislike_count_chk,
'repost_count': repost_count_chk,
'isOriginal': isOriginal_chk,
'release_time': release_time_chk,
'fetch_time': fetch_time_chk,
}
false_time_warning=[]
for field in type_chk:
if type_chk[field]!=True:
false_time_warning.append(type_chk[field])
if false_time_warning==[]:
return True
else:
warn_msg='WARNING: ' + '\n'.join(false_time_warning)
return warn_msg
if __name__=='__main__':
a=Std_fields_video()
a.video_data['platform']='腾讯视频'
a.video_data['duration']=62
a.video_data['title']='This is test video'
# a.video_data={'duration': 62}
print(a.field_type_correct())