Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Check_Schduler:
- import time
- from threading import Thread
- from main.Downloader import Downloader
- class Check_scheduler(object):
- def check(self):
- while True:
- connection = self.mysql.connect()
- data = self.mysql.get_mysql_data(connection)
- for vidarray in data:
- if vidarray[1] == 'video':
- self.downloader.downloadVideo(vidarray[0])
- self.mysql.changetoDownloaded(vidarray[0], connection)
- elif vidarray[1] == 'playlist':
- self.downloader.downloadPlaylist(vidarray[0])
- self.mysql.changetoDownloaded(vidarray[0], connection)
- elif vidarray[1] == 'channel':
- self.downloader.downloadChannel(vidarray[0])
- self.mysql.changetoDownloaded(vidarray[0], connection)
- else:
- print 'Something wrent wrong in Check_scheduler by checking type! type:' + vidarray[1]
- time.sleep(10)
- def __init__(self, mysql):
- self.mysql = mysql
- self.downloader = Downloader()
- t = Thread(target=self.check(), args=())
- t.start()
- Downloader:
- from __future__ import unicode_literals
- from youtube_dl import YoutubeDL
- class MyLogger(object):
- def debug(self, msg):
- print msg
- def warning(self, msg):
- pass
- def error(self, msg):
- print(msg)
- class Downloader(object):
- def __init__(self):
- print('')
- def downloadVideo(self, link):
- ydl_opts = {
- 'format': 'bestaudio/best',
- 'postprocessors': [{
- 'key': 'FFmpegExtractAudio',
- 'preferredcodec': 'mp3',
- 'preferredquality': '192',
- }],
- 'logger': MyLogger()
- }
- with YoutubeDL(ydl_opts) as ydl:
- list = []
- list.append(link)
- ydl.download(list)
- def downloadChannel(self, link):
- print ''
- def downloadPlaylist(self, link):
- print ''
- Mysql:
- import mysql.connector
- from mysql.connector import errorcode
- class Mysql(object):
- def __init__(self):
- print ''
- def connect(self):
- try:
- connection = mysql.connector.connect(user='user', password='pass',
- host='ip',
- database='database', port=3306)
- return connection
- except mysql.connector.Error as err:
- if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
- print("Something is wrong with your user name or password")
- elif err.errno == errorcode.ER_BAD_DB_ERROR:
- print("Database does not exist")
- else:
- print(err)
- return 0
- def get_mysql_data(self, connection):
- cursor = connection.cursor()
- query = ("SELECT * FROM `vid_links` WHERE downloaded = 0")
- cursor.execute(query)
- video_array = []
- for (link, type, downloaded) in cursor:
- video_array.extend([[link, type]])
- #print("{}, {} ".format(
- # link, type))
- cursor.close()
- return video_array
- def changetoDownloaded(self, link, connection):
- cursor = connection.cursor()
- query = ("SELECT * FROM `vid_links` WHERE link = '%s'")
- query = ("UPDATE `vid_links` SET downloaded=1 WHERE link = '%s'")
- #query = "SELECT * FROM `vid_links` WHERE link = 'https://www.youtube.com/watch?v=IS6Hn4x7S6k'"
- # print query
- query = query % link
- print query
- #cursor.execute(query)
- #connection.commit()
- for (link, type, downloaded) in cursor:
- print type
- #print("{}, {} type".format(
- #link, type))
- cursor.close()
- Main:
- from mysql_con import Mysql
- from main import Check_scheduler
- RUNNING = 1
- mysqlc = Mysql.Mysql()
- scheduler = Check_scheduler.Check_scheduler(mysqlc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement