def parser_response(): from dbimpl import DBImpl from setting import * db = DBImpl({'url': os.path.join(playlists_dir, 'videos.db')}) sql = "insert into playlists(id, title, channel) values(?, ?, ?)" with open("youtube_search_list.json") as fin: result = json.load(fin) for item in result['items']: playlist_title = item['snippet']['title'] channel = item['snippet']['channelId'] playlist_id = item['id']['playlistId'] print playlist_id, playlist_title, channel db.updateone(sql, playlist_id, playlist_title, channel)
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()