|
@@ -21,6 +21,11 @@ application.
|
|
|
|
|
|
import logging
|
|
|
import sys
|
|
|
+import smtplib
|
|
|
+import socket
|
|
|
+
|
|
|
+from email.mime.text import MIMEText
|
|
|
+from email.mime.multipart import MIMEMultipart
|
|
|
|
|
|
from lib_stov import subscription
|
|
|
from lib_stov import stov_exceptions
|
|
@@ -176,3 +181,127 @@ def update_subscriptions(database, conf):
|
|
|
else:
|
|
|
LOGGER.debug(_("Video %s successfully inserted into "
|
|
|
"database."), video.title)
|
|
|
+
|
|
|
+
|
|
|
+def download_videos(database, conf):
|
|
|
+ """
|
|
|
+ Downloads videos that haven't been previously downloaded.
|
|
|
+
|
|
|
+ :param conf: configuration object
|
|
|
+ :type conf: lib_stov.configuration.Conf
|
|
|
+ :param database: database object
|
|
|
+ :type database: lib_stov.database.Db
|
|
|
+ :return: tuple containing (in that order) downloaded videos, failed \
|
|
|
+ videos and a list of the videos downloaded
|
|
|
+ :rtype: tuple
|
|
|
+ """
|
|
|
+ video_titles = []
|
|
|
+ subscriptions_list = database.get_subscriptions(conf)
|
|
|
+ LOGGER.debug(_("Trying to determine the itag value for youtube-dl from"
|
|
|
+ " your quality and codec settings."))
|
|
|
+ itag = conf.get_youtube_parameter()
|
|
|
+ LOGGER.debug(_("Found value: %s."), itag)
|
|
|
+ if itag == 0:
|
|
|
+ LOGGER.debug(_("Codec and resolution could not be determined, using "
|
|
|
+ "maximum possible value."))
|
|
|
+ itag = 38
|
|
|
+ videos_downloaded = 0
|
|
|
+ videos_failed = 0
|
|
|
+ for sub in subscriptions_list:
|
|
|
+ videos = database.get_videos(sub.get_id(), conf)
|
|
|
+ sub.gather_videos(videos)
|
|
|
+ try:
|
|
|
+ sub.download_videos(itag)
|
|
|
+ except stov_exceptions.SubscriptionDisabledException as error:
|
|
|
+ LOGGER.debug(error)
|
|
|
+ for entry in sub.downloaded_videos:
|
|
|
+ database.update_video_download_status(entry.get_id(), 1)
|
|
|
+ video_titles.append(entry.title)
|
|
|
+ videos_downloaded = len(video_titles)
|
|
|
+ videos_failed = videos_failed + sub.failed_videos_count
|
|
|
+ for video in sub.failed_videos:
|
|
|
+ try:
|
|
|
+ database.update_video_fail_count(video.failcnt, video.get_id())
|
|
|
+ if video.failcnt >= int(conf.values["maxfails"]):
|
|
|
+ database.disable_failed_video(video.get_id())
|
|
|
+ except stov_exceptions.DBWriteAccessFailedException as error:
|
|
|
+ LOGGER.error(error)
|
|
|
+ sys.exit(1)
|
|
|
+ return (videos_downloaded, videos_failed, video_titles)
|
|
|
+
|
|
|
+
|
|
|
+def compose_email(conf, downloaded_videos, video_titles):
|
|
|
+ """
|
|
|
+ Composes an e-mail that can be send out to the user.
|
|
|
+
|
|
|
+ :param conf: configuration object
|
|
|
+ :type conf: lib_stov.configuration.Conf
|
|
|
+ :param downloaded_videos: number of downloaded videos
|
|
|
+ :type downloaded_videos: int
|
|
|
+ :param video_titles: titles of the downloaded videos
|
|
|
+ :type video_titles: list
|
|
|
+ :return: e-mail contents
|
|
|
+ :rtype: MIMEMultipart
|
|
|
+ """
|
|
|
+ mail_text = ""
|
|
|
+ msg = MIMEMultipart()
|
|
|
+ if downloaded_videos == 1:
|
|
|
+ msg["Subject"] = _("Downloaded %i new video") % downloaded_videos
|
|
|
+ mail_text = _("The following episode has been downloaded by stov: "
|
|
|
+ "\n\n")
|
|
|
+ else:
|
|
|
+ msg["Subject"] = _("Downloaded %i new videos") % downloaded_videos
|
|
|
+ mail_text = _("The following episodes have been downloaded by "
|
|
|
+ "stov: \n\n")
|
|
|
+ msg["From"] = "stov <%s>" % conf.values["mailfrom"]
|
|
|
+ msg["To"] = "<%s>" % conf.values["mailto"]
|
|
|
+ for line in video_titles:
|
|
|
+ mail_text += line + "\n"
|
|
|
+ msg_text = MIMEText(mail_text.encode("utf8"), _charset="utf8")
|
|
|
+ msg.attach(msg_text)
|
|
|
+ return msg
|
|
|
+
|
|
|
+
|
|
|
+def send_email(conf, msg):
|
|
|
+ """
|
|
|
+ Sends an e-mail to the user.
|
|
|
+
|
|
|
+ :param conf: configuration object
|
|
|
+ :type conf: lib_stov.configuration.Conf
|
|
|
+ :param msg: message to be sent
|
|
|
+ :type msg: MIMEMultipart
|
|
|
+ """
|
|
|
+ server_connection = smtplib.SMTP(conf.values["mailhost"],
|
|
|
+ conf.values["smtpport"])
|
|
|
+ try:
|
|
|
+ server_connection.connect()
|
|
|
+ except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
|
|
|
+ socket.error):
|
|
|
+ LOGGER.error(_("Could not connect to the smtp server, please check"
|
|
|
+ " your settings!"))
|
|
|
+ else:
|
|
|
+ try:
|
|
|
+ server_connection.starttls()
|
|
|
+ except smtplib.SMTPException:
|
|
|
+ LOGGER.debug(_("TLS not available, proceeding unencrypted."))
|
|
|
+ if conf.values["auth_needed"] == "yes":
|
|
|
+ try:
|
|
|
+ server_connection.login(conf.values["user_name"],
|
|
|
+ conf.values["password"])
|
|
|
+ except smtplib.SMTPAuthenticationError:
|
|
|
+ LOGGER.error(_("Authentication failed, please check user "
|
|
|
+ "name and password!"))
|
|
|
+ except smtplib.SMTPException:
|
|
|
+ LOGGER.error(_("Could not authenticate, server probably "
|
|
|
+ "does not support authentication!"))
|
|
|
+ try:
|
|
|
+ server_connection.sendmail(conf.values["mailfrom"],
|
|
|
+ conf.values["mailto"],
|
|
|
+ msg.as_string())
|
|
|
+ except smtplib.SMTPRecipientsRefused:
|
|
|
+ LOGGER.error(_("The server refused the recipient address, "
|
|
|
+ "please check your settings."))
|
|
|
+ except smtplib.SMTPSenderRefused:
|
|
|
+ LOGGER.error(_("The server refused the sender address, "
|
|
|
+ "please check your settings."))
|
|
|
+ server_connection.quit()
|