-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_service.py
179 lines (153 loc) · 6.34 KB
/
data_service.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#-*- coding:utf-8 -*-
# coding=utf8
from flask import jsonify
from flask import abort
from flask import make_response
from flask import request
from flask import url_for
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import text
from sqlalchemy import func
import time
import datetime
import sys
import math
import global_config
from Models import Video
from Models import init_database
db_engine = create_engine(global_config.config['db_engine'], isolation_level="READ UNCOMMITTED")
session_factory = sessionmaker(bind=db_engine)
Scope_Session = scoped_session(session_factory)
per_page = 10
class DataService:
def __init__(self, engine):
"""
:param engine: The engine route and login details
:return: a new instance of DAL class
:type engine: string
"""
if not engine:
raise ValueError('The values specified in engine parameter has to be supported by SQLAlchemy')
print 'init DataService'
def init_database(self):
"""
Initializes the database tables and relationships
:return: None
"""
init_database(self.engine)
def videos_at_page(self, page=1, key="", serialize=False):
try:
offset = (page - 1) * per_page
temp_session = Scope_Session()
# count = temp_session.query(func.count(Video.status != -1))
count = temp_session.query(Video).filter(Video.status != -1, Video.name.like('%' + key + '%')).count()
page_count = int(math.ceil(count / float(per_page)))
print page_count
if page_count == 0:
page_count = 1
page_indexs = [(i + 1) for i in range(page_count)]
current_page = page
videos = temp_session.query(Video).filter(Video.status != -1, Video.name.like('%' + key + '%')).order_by(Video.upload_time.desc()).offset(offset).limit(per_page)
clean_videos = [Video.get_new_instance(vi) for vi in videos]
Scope_Session.remove()
if serialize:
return [vi.mini_serialize() for vi in clean_videos], page_indexs, current_page
else:
return clean_videos, page_indexs, current_page
except (Exception) as e:
print "抓到exception"
print "all_videos 操作失败"
print e.message
return [], [], 1
def all_videos(self, serialize=False):
try:
temp_session = Scope_Session()
start = time.time()
videos = temp_session.query(Video).filter(Video.status != -1).order_by(Video.upload_time.desc()).offset(40).limit(10)
print videos
print("查询所有video用时: %.2fs" % (time.time() - start))
new_start = time.time()
count = temp_session.query(Video).filter(Video.status != -1).count()
print count
print("查询总共有多少video用时: %.2fs" % (time.time() - new_start))
new_start = time.time()
count = temp_session.query(func.count(Video.status != -1))
print count
print("新方式查询总共有多少video用时: %.2fs" % (time.time() - new_start))
clean_videos = [Video.get_new_instance(vi) for vi in videos]
Scope_Session.remove()
if serialize:
return [vi.mini_serialize() for vi in clean_videos]
else:
return clean_videos
except (Exception) as e:
print "抓到exception"
print "all_videos 操作失败"
print e.message
return []
def get_video_by_hash_name(self, hash_name, serialize=False):
try:
temp_session = Scope_Session()
videos = temp_session.query(Video).filter(Video.hash_name == hash_name).all()
clean_videos = [Video.get_new_instance(vi) for vi in videos]
Scope_Session.remove()
if serialize:
return [vi.serialize() for vi in clean_videos]
else:
return clean_videos
except (Exception) as e:
print "抓到exception"
print e.message
print "get_video_by_hash_name 操作失败"
def get_video_by_name(self, name, serialize=False):
try:
temp_session = Scope_Session()
videos = temp_session.query(Video).filter(Video.name.like('%' + name + '%')).limit(1)
clean_videos = [Video.get_new_instance(vi) for vi in videos]
Scope_Session.remove()
if serialize:
return [vi.serialize() for vi in clean_videos]
else:
return clean_videos
except (Exception) as e:
print "抓到exception"
print e.message
print "get_video_by_name 操作失败"
def get_all_fetching_caption_videos(self, serialize=False):
try:
temp_session = Scope_Session()
videos = temp_session.query(Video).filter(Video.status == 7).all()
clean_videos = [Video.get_new_instance(vi) for vi in videos]
Scope_Session.remove()
if serialize:
return [vi.serialize() for vi in clean_videos]
else:
return clean_videos
except (Exception) as e:
print "抓到exception"
print "get_all_fetching_caption_videos 操作失败"
print e.message
def update_video(self, video): ## trick: 先查出来,再更新
try:
temp_session = Scope_Session()
videos = temp_session.query(Video).filter(Video.id == video.id).all()
if len(videos) > 0:
vi = videos[0]
vi = Video.sync_old_video(old_vi=vi, vi=video)
temp_session.add(vi)
temp_session.commit()
Scope_Session.remove()
except (Exception) as e:
print "抓到exception"
print "update_video 操作失败"
print e.message
def add_unprocessed_videos(self, videos):
temp_session = Scope_Session()
for vi in videos:
temp_session.add(vi)
temp_session.commit()
Scope_Session.remove()
DATA_PROVIDER = DataService(global_config.config['db_engine'])