def download(): db = DBImpl({'url': os.path.join(playlists_dir, 'videos.db')}) sql = 'select id, title from playlists where used = 1' sql2 = 'select * from videos where playlist = ?' res = db.querymany(sql) video_folder = "/Volumes/Seagate/VideoAnalytics/Videos" for list_id, title in res: res = db.querymany(sql2, list_id) if len(res) > 0: print 'list has been downloaded', list_id continue print list_id, title playlist_url = "https://www.youtube.com/playlist?list=%s" % list_id output_folder = os.path.join(video_folder, list_id) if not os.path.exists(output_folder): os.mkdir(output_folder) videos = download_youtube_list(playlist_url, output_folder) for idx, (video_hash, title) in enumerate(videos): insert_video(db, video_hash, title, list_id, idx + 1) db.close()
class APIDBImpl: def __init__(self): self.dbimpl = DBImpl({ "type": "mysql", "url": "127.0.0.1", "username": "******", "password": "******", "database": "link_api" }) def query_records(self, entity): idx = entity.find('(') if idx > 0: entity = entity[0:idx].strip() sql = 'select * from link_api_record where name = %s' return self.dbimpl.querymany(sql, entity) def query_web_cache(self, link): sql = 'select * from web_cache where url = %s' return self.dbimpl.queryone(sql, link) def insert_or_update_cache(self, result): try: if not result[3]: sql = 'update web_cache set content=%s, access_time=%s where url=%s' self.dbimpl.updateone(sql, result[1], datetime.now(), result[2]) else: sql = 'insert web_cache(url, content) values(%s, %s)' self.dbimpl.updateone(sql, result[2], result[1]) except Exception as e: print e def close(self): self.dbimpl.close()