Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. Check_Schduler:
  2. import time
  3. from threading import Thread
  4. from main.Downloader import Downloader
  5.  
  6.  
  7. class Check_scheduler(object):
  8.  
  9.     def check(self):
  10.         while True:
  11.             connection = self.mysql.connect()
  12.             data = self.mysql.get_mysql_data(connection)
  13.             for vidarray in data:
  14.                 if vidarray[1] == 'video':
  15.                     self.downloader.downloadVideo(vidarray[0])
  16.                     self.mysql.changetoDownloaded(vidarray[0], connection)
  17.                 elif vidarray[1] == 'playlist':
  18.                     self.downloader.downloadPlaylist(vidarray[0])
  19.                     self.mysql.changetoDownloaded(vidarray[0], connection)
  20.                 elif vidarray[1] == 'channel':
  21.                     self.downloader.downloadChannel(vidarray[0])
  22.                     self.mysql.changetoDownloaded(vidarray[0], connection)
  23.                 else:
  24.                     print 'Something wrent wrong in Check_scheduler by checking type! type:' + vidarray[1]
  25.             time.sleep(10)
  26.  
  27.     def __init__(self, mysql):
  28.         self.mysql = mysql
  29.         self.downloader = Downloader()
  30.         t = Thread(target=self.check(), args=())
  31.         t.start()
  32. Downloader:
  33. from __future__ import unicode_literals
  34.  
  35. from youtube_dl import YoutubeDL
  36.  
  37. class MyLogger(object):
  38.     def debug(self, msg):
  39.         print msg
  40.  
  41.     def warning(self, msg):
  42.         pass
  43.  
  44.     def error(self, msg):
  45.         print(msg)
  46.  
  47.  
  48. class Downloader(object):
  49.     def __init__(self):
  50.         print('')
  51.  
  52.     def downloadVideo(self, link):
  53.         ydl_opts = {
  54.             'format': 'bestaudio/best',
  55.             'postprocessors': [{
  56.                 'key': 'FFmpegExtractAudio',
  57.                 'preferredcodec': 'mp3',
  58.                 'preferredquality': '192',
  59.             }],
  60.             'logger': MyLogger()
  61.         }
  62.         with YoutubeDL(ydl_opts) as ydl:
  63.             list = []
  64.             list.append(link)
  65.  
  66.             ydl.download(list)
  67.  
  68.  
  69.  
  70.  
  71.     def downloadChannel(self, link):
  72.         print ''
  73.  
  74.     def downloadPlaylist(self, link):
  75.         print ''
  76. Mysql:
  77. import mysql.connector
  78. from mysql.connector import errorcode
  79.  
  80. class Mysql(object):
  81.     def __init__(self):
  82.         print ''
  83.  
  84.     def connect(self):
  85.         try:
  86.             connection = mysql.connector.connect(user='user', password='pass',
  87.                                                  host='ip',
  88.                                                  database='database', port=3306)
  89.             return connection
  90.         except mysql.connector.Error as err:
  91.             if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  92.                 print("Something is wrong with your user name or password")
  93.             elif err.errno == errorcode.ER_BAD_DB_ERROR:
  94.                 print("Database does not exist")
  95.             else:
  96.                 print(err)
  97.         return 0
  98.  
  99.     def get_mysql_data(self, connection):
  100.         cursor = connection.cursor()
  101.         query = ("SELECT * FROM `vid_links` WHERE downloaded = 0")
  102.         cursor.execute(query)
  103.         video_array = []
  104.         for (link, type, downloaded) in cursor:
  105.  
  106.             video_array.extend([[link, type]])
  107.             #print("{}, {} ".format(
  108. #                link, type))
  109.         cursor.close()
  110.  
  111.         return video_array
  112.  
  113.     def changetoDownloaded(self, link, connection):
  114.         cursor = connection.cursor()
  115.         query = ("SELECT * FROM `vid_links` WHERE link = '%s'")
  116.         query = ("UPDATE `vid_links` SET downloaded=1 WHERE link = '%s'")
  117.         #query = "SELECT * FROM `vid_links` WHERE link = 'https://www.youtube.com/watch?v=IS6Hn4x7S6k'"
  118.        # print query
  119.         query = query % link
  120.         print query
  121.         #cursor.execute(query)
  122.         #connection.commit()
  123.  
  124.  
  125.  
  126.         for (link, type, downloaded) in cursor:
  127.             print type
  128.             #print("{}, {} type".format(
  129.                 #link, type))
  130.  
  131.         cursor.close()
  132.  
  133. Main:
  134. from mysql_con import Mysql
  135. from main import Check_scheduler
  136. RUNNING = 1
  137.  
  138. mysqlc = Mysql.Mysql()
  139. scheduler = Check_scheduler.Check_scheduler(mysqlc)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement