def downloads(): import redis r = redis.Redis(host=ytdl.settings.REDIS_HOST, port=ytdl.settings.REDIS_PORT) ids = r.smembers("dl") or [] all_info = {} for cur in ids: cur = cur.decode("utf-8") key = "dl:{id}:info".format(id=cur) status = (r.hget(key, "status") or b"").decode("utf-8") message = (r.hget(key, "message") or b"").decode("utf-8") progress = float(r.hget(key, "progress") or 0.0) try: v = Video.get(id=cur) except Video.DoesNotExist: title = "Unknown" else: title = v.title all_info[cur] = { 'id': cur, 'title': title, 'status': status, 'message': message, 'progress': progress, } return json.dumps(all_info)
def downloads(): import redis r = redis.Redis() ids = r.smembers("dl") or [] all_info = {} for cur in ids: key = "dl:{id}:info".format(id=cur) status = r.hget(key, "status") or "" message = r.hget(key, "message") or "" progress = float(r.hget(key, "progress") or 0.0) try: v = Video.get(id=cur) except Video.DoesNotExist: title = "Unknown" else: title = v.title all_info[cur] = { 'id': cur, 'title': title, 'status': status, 'message': message, 'progress': progress, } return json.dumps(all_info)
def downloads(): import redis r = redis.Redis(host=ytdl.settings.REDIS_HOST, port=ytdl.settings.REDIS_PORT) ids = r.smembers("dl") or [] all_info = {} for cur in ids: cur = cur.decode("utf-8") key = "dl:{id}:info".format(id=cur) status = (r.hget(key, "status") or "").decode("utf-8") message = (r.hget(key, "message") or "").decode("utf-8") progress = float(r.hget(key, "progress") or 0.0) try: v = Video.get(id=cur) except Video.DoesNotExist: title = "Unknown" else: title = v.title all_info[cur] = { 'id': cur, 'title': title, 'status': status, 'message': message, 'progress': progress, } return json.dumps(all_info)
def _set_status(videoid, status): video = Video.get(id=videoid) if video is None: abort(404) video.status = status video.save() return json.dumps({"status": video.status})
def video_status(): ids = request.args.get("ids") if ids is None or len(ids) == 0: return "{}" ids = ids.split(",") videos = {} for cur in ids: v = Video.get(id=cur) if v is None: abort(404) videos[int(cur)] = v.status return json.dumps(videos)
def grab_video(videoid, force=False): # Get video from DB video = Video.get(id=videoid) # Validation grabbable = video.status in [ Video.STATE_NEW, Video.STATE_GRAB_ERROR, Video.STATE_QUEUED ] if not grabbable and not force: emsg = "%s status not NEW or GRAB_ERROR, and force was %s" % (video, force) raise ValueError(emsg) if force and video.status == Video.STATE_DOWNLOADING: raise ValueError("Already downloading") # Grab video log.info("Starting to grab %s" % video) video.status = Video.STATE_DOWNLOADING video.save() cwd = ytdl.settings.OUTPUT_DIR try: os.makedirs(cwd) except OSError as e: if e.errno == 17: # Ignore errno 17 (File exists) pass else: raise # Grab video outtmpl = os.path.join(ytdl.settings.OUTPUT_DIR, ytdl.settings.OUTPUT_FORMAT) x = ytdl.download_api.YDL(id=videoid, url=video.url, outtmpl=outtmpl) try: x.go() except Exception as e: # ? video.status = Video.STATE_GRAB_ERROR video.save() log.error("Error grabbing %s: %s" % (video, e), exc_info=True) return else: video.status = Video.STATE_GRABBED video.save() log.info("Grab complete %s" % video)
def grab_video(videoid, force=False): # Get video from DB video = Video.get(id=videoid) # Validation grabbable = video.status in [Video.STATE_NEW, Video.STATE_GRAB_ERROR, Video.STATE_QUEUED] if not grabbable and not force: emsg = "%s status not NEW or GRAB_ERROR, and force was %s" % ( video, force) raise ValueError(emsg) if force and video.status == Video.STATE_DOWNLOADING: raise ValueError("Already downloading") # Grab video log.info("Starting to grab %s" % video) video.status = Video.STATE_DOWNLOADING video.save() cwd = ytdl.settings.OUTPUT_DIR try: os.makedirs(cwd) except OSError as e: if e.errno == 17: # Ignore errno 17 (File exists) pass else: raise # Grab video outtmpl = os.path.join(ytdl.settings.OUTPUT_DIR, ytdl.settings.OUTPUT_FORMAT) x = ytdl.download_api.YDL(id=videoid, url=video.url, outtmpl=outtmpl) try: x.go() except Exception as e: # ? video.status = Video.STATE_GRAB_ERROR video.save() log.error("Error grabbing %s: %s" % (video, e), exc_info=True) return else: video.status = Video.STATE_GRABBED video.save() log.info("Grab complete %s" % video)
def grab(videoid): """For a given video ID, enqueue the task to download it """ video = Video.get(id=videoid) if video is None: abort(404) force = request.args.get("force", "false").lower() == "true" grabbable = video.status in [Video.STATE_NEW, Video.STATE_GRAB_ERROR, Video.STATE_IGNORE] if not grabbable and not force: ret = {"error": "Already grabbed (status %s)" % (video.status)} return json.dumps(ret), 500 ytdl.tasks.grab_video.delay(video.id, force=force) video.status = Video.STATE_QUEUED video.save() return json.dumps({"status": video.status})
def channel_details(chanid): if chanid == "_all": query = Video.select() else: chan = Channel.get(id=chanid) query = Video.select().filter(channel=chan) query = query.order_by(Video.publishdate.desc()) search = request.args.get('search', "") if len(search) > 0: query = query.where(Video.title.contains(search)) # Query based on status status = request.args.get('status', "") if len(status) > 0: status = status.strip().split(",") x = Video.status == status[0] for st in status[1:]: x = x | (Video.status == st) query = query.where(x) # 25 videos per page, with no less than 5 per page paginator = Paginator(query, per_page=25, orphans=5) # Get page parameter page_num = request.args.get('page', '1') if int(page_num) < 1: page_num = 1 try: page = paginator.page(page_num) except PageNotAnInteger: page = paginator.page(1) except EmptyPage: page = paginator.page(paginator.num_pages) out_videos = [] for v in page: out_videos.append({ 'id': v.id, 'title': v.title, 'imgs': v.img, 'url': v.url, 'description': v.description, 'publishdate': str(v.publishdate), 'status': v.status, # FIXME: Data duplication, only used for "all" channel view 'channel': _channel_info_dict(v.channel), }) if chanid == '_all': channel = None else: channel = _channel_info_dict(chan) page_info = { 'total': paginator.num_pages, 'current': page.number, 'has_next': page.has_next(), 'has_previous': page.has_previous(), } return json.dumps({ 'channel': channel, 'videos': out_videos, 'pagination': page_info })
def channel_details(chanid): if chanid == "_all": query = Video.select() else: chan = Channel.get(id=chanid) query = Video.select().filter(channel = chan) query = query.order_by(Video.publishdate.desc()) search = request.args.get('search', "") if len(search) > 0: query = query.where(Video.title.contains(search)) # Query based on status status = request.args.get('status', "") if len(status) > 0: status = status.strip().split(",") x = Video.status == status[0] for st in status[1:]: x = x | (Video.status == st) query = query.where(x) # 25 videos per page, with no less than 5 per page paginator = Paginator(query, per_page=25, orphans=5) # Get page parameter page_num = request.args.get('page', '1') if int(page_num) < 1: page_num = 1 try: page = paginator.page(page_num) except PageNotAnInteger: page = paginator.page(1) except EmptyPage: page = paginator.page(paginator.num_pages) out_videos = [] for v in page: out_videos.append({ 'id': v.id, 'title': v.title, 'imgs': v.img, 'url': v.url, 'description': v.description, 'publishdate': str(v.publishdate), 'status': v.status, # FIXME: Data duplication, only used for "all" channel view 'channel': _channel_info_dict(v.channel), }) if chanid == '_all': channel = None else: channel = _channel_info_dict(chan) page_info = { 'total': paginator.num_pages, 'current': page.number, 'has_next': page.has_next(), 'has_previous': page.has_previous(), } return json.dumps( {'channel': channel, 'videos': out_videos, 'pagination': page_info})