|
@@ -30,17 +30,17 @@ from email.mime.multipart import MIMEMultipart
|
|
|
from lib_stov import subscription
|
|
|
from lib_stov import stov_exceptions
|
|
|
from lib_stov.database import Db
|
|
|
+from lib_stov.configuration import Conf
|
|
|
|
|
|
LOGGER = logging.getLogger("stov")
|
|
|
DATABASE = Db.get_instance()
|
|
|
+CONFIGURATION = Conf.get_instance()
|
|
|
|
|
|
|
|
|
-def add_subscription(conf, channel="", search="", playlist="", site="youtube"):
|
|
|
+def add_subscription(channel="", search="", playlist="", site="youtube"):
|
|
|
"""
|
|
|
Takes care of adding a new subscription to the database.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param site: site the subscription is about to be created for
|
|
|
:type site: str
|
|
|
:param channel: optional channel name
|
|
@@ -56,25 +56,21 @@ def add_subscription(conf, channel="", search="", playlist="", site="youtube"):
|
|
|
try:
|
|
|
if channel and not search:
|
|
|
new_subscription = subscription.Sub(subscription_type="user",
|
|
|
- name=channel, conf=conf,
|
|
|
- site=site)
|
|
|
+ name=channel, site=site)
|
|
|
elif channel and search:
|
|
|
new_subscription = subscription.Sub(subscription_type="user",
|
|
|
- name=channel,
|
|
|
- search=search,
|
|
|
- conf=conf, site=site)
|
|
|
+ name=channel, site=site,
|
|
|
+ search=search)
|
|
|
elif not channel and search:
|
|
|
new_subscription = subscription.Sub(subscription_type="search",
|
|
|
- name=_("Search_"),
|
|
|
- search=search,
|
|
|
- conf=conf, site=site)
|
|
|
+ name=_("Search_"), site=site,
|
|
|
+ search=search)
|
|
|
elif playlist:
|
|
|
if search:
|
|
|
LOGGER.error(_("Playlists do not support searching, the "
|
|
|
"search option will be ignored!"))
|
|
|
new_subscription = subscription.Sub(subscription_type="playlist",
|
|
|
- name=playlist,
|
|
|
- conf=conf, site=site)
|
|
|
+ name=playlist, site=site)
|
|
|
else:
|
|
|
LOGGER.error(_("None or invalid subscription type given, please "
|
|
|
"check the type option and try again."))
|
|
@@ -120,14 +116,12 @@ def add_subscription(conf, channel="", search="", playlist="", site="youtube"):
|
|
|
_(" successfully added"))
|
|
|
|
|
|
|
|
|
-def list_subscriptions(conf):
|
|
|
+def list_subscriptions():
|
|
|
"""
|
|
|
Prints a list of subscriptions from the database.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
"""
|
|
|
- subscriptions_list = DATABASE.get_subscriptions(conf)
|
|
|
+ subscriptions_list = DATABASE.get_subscriptions(CONFIGURATION)
|
|
|
sub_state = None
|
|
|
if subscriptions_list:
|
|
|
LOGGER.info(_("ID Title Site"))
|
|
@@ -161,19 +155,17 @@ def delete_subscription(sub_id):
|
|
|
LOGGER.info(_("Subscription deleted successfully!"))
|
|
|
|
|
|
|
|
|
-def update_subscriptions(conf, subscriptions=None):
|
|
|
+def update_subscriptions(subscriptions=None):
|
|
|
"""
|
|
|
Updates data about videos in a subscription.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param subscriptions: list of subscriptions to update
|
|
|
:type subscriptions: list
|
|
|
"""
|
|
|
- subscriptions_list = get_subscriptions(conf, subscriptions)
|
|
|
+ subscriptions_list = get_subscriptions(subscriptions)
|
|
|
for element in subscriptions_list:
|
|
|
LOGGER.debug(_("Updating subscription %s"), element.get_title())
|
|
|
- videos = DATABASE.get_videos(element.get_id(), conf)
|
|
|
+ videos = DATABASE.get_videos(element.get_id(), CONFIGURATION)
|
|
|
element.gather_videos(videos)
|
|
|
try:
|
|
|
element.update_data()
|
|
@@ -195,12 +187,10 @@ def update_subscriptions(conf, subscriptions=None):
|
|
|
"database."), video.title)
|
|
|
|
|
|
|
|
|
-def download_videos(conf, subscriptions=None):
|
|
|
+def download_videos(subscriptions=None):
|
|
|
"""
|
|
|
Downloads videos that haven't been previously downloaded.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param subscriptions: list of subscriptions to consider for downloading
|
|
|
:type subscriptions: list
|
|
|
:return: tuple containing (in that order) downloaded videos, failed \
|
|
@@ -208,11 +198,11 @@ def download_videos(conf, subscriptions=None):
|
|
|
:rtype: tuple
|
|
|
"""
|
|
|
video_titles = []
|
|
|
- subscriptions_list = get_subscriptions(conf, subscriptions)
|
|
|
+ subscriptions_list = get_subscriptions(subscriptions)
|
|
|
videos_downloaded = 0
|
|
|
videos_failed = 0
|
|
|
for sub in subscriptions_list:
|
|
|
- videos = DATABASE.get_videos(sub.get_id(), conf)
|
|
|
+ videos = DATABASE.get_videos(sub.get_id(), CONFIGURATION)
|
|
|
sub.gather_videos(videos)
|
|
|
try:
|
|
|
sub.download_videos()
|
|
@@ -226,7 +216,7 @@ def download_videos(conf, subscriptions=None):
|
|
|
for video in sub.failed_videos:
|
|
|
try:
|
|
|
DATABASE.update_video_fail_count(video.failcnt, video.get_id())
|
|
|
- if video.failcnt >= int(conf.values["maxfails"]):
|
|
|
+ if video.failcnt >= int(CONFIGURATION.values["maxfails"]):
|
|
|
DATABASE.disable_failed_video(video.get_id())
|
|
|
except stov_exceptions.DBWriteAccessFailedException as error:
|
|
|
LOGGER.error(error)
|
|
@@ -234,12 +224,10 @@ def download_videos(conf, subscriptions=None):
|
|
|
return (videos_downloaded, videos_failed, video_titles)
|
|
|
|
|
|
|
|
|
-def compose_email(conf, downloaded_videos, video_titles):
|
|
|
+def compose_email(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
|
|
@@ -257,8 +245,8 @@ def compose_email(conf, downloaded_videos, video_titles):
|
|
|
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"]
|
|
|
+ msg["From"] = "stov <%s>" % CONFIGURATION.values["mailfrom"]
|
|
|
+ msg["To"] = "<%s>" % CONFIGURATION.values["mailto"]
|
|
|
for line in video_titles:
|
|
|
mail_text += line + "\n"
|
|
|
msg_text = MIMEText(mail_text.encode("utf8"), _charset="utf8")
|
|
@@ -266,17 +254,15 @@ def compose_email(conf, downloaded_videos, video_titles):
|
|
|
return msg
|
|
|
|
|
|
|
|
|
-def send_email(conf, msg):
|
|
|
+def send_email(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"])
|
|
|
+ server_connection = smtplib.SMTP(CONFIGURATION.values["mailhost"],
|
|
|
+ CONFIGURATION.values["smtpport"])
|
|
|
try:
|
|
|
server_connection.connect()
|
|
|
except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
|
|
@@ -288,10 +274,10 @@ def send_email(conf, msg):
|
|
|
server_connection.starttls()
|
|
|
except smtplib.SMTPException:
|
|
|
LOGGER.debug(_("TLS not available, proceeding unencrypted."))
|
|
|
- if conf.values["auth_needed"] == "yes":
|
|
|
+ if CONFIGURATION.values["auth_needed"] == "yes":
|
|
|
try:
|
|
|
- server_connection.login(conf.values["user_name"],
|
|
|
- conf.values["password"])
|
|
|
+ server_connection.login(CONFIGURATION.values["user_name"],
|
|
|
+ CONFIGURATION.values["password"])
|
|
|
except smtplib.SMTPAuthenticationError:
|
|
|
LOGGER.error(_("Authentication failed, please check user "
|
|
|
"name and password!"))
|
|
@@ -299,8 +285,8 @@ def send_email(conf, msg):
|
|
|
LOGGER.error(_("Could not authenticate, server probably "
|
|
|
"does not support authentication!"))
|
|
|
try:
|
|
|
- server_connection.sendmail(conf.values["mailfrom"],
|
|
|
- conf.values["mailto"],
|
|
|
+ server_connection.sendmail(CONFIGURATION.values["mailfrom"],
|
|
|
+ CONFIGURATION.values["mailto"],
|
|
|
msg.as_string())
|
|
|
except smtplib.SMTPRecipientsRefused:
|
|
|
LOGGER.error(_("The server refused the recipient address, "
|
|
@@ -311,12 +297,10 @@ def send_email(conf, msg):
|
|
|
server_connection.quit()
|
|
|
|
|
|
|
|
|
-def list_videos(conf, sub_id):
|
|
|
+def list_videos(sub_id):
|
|
|
"""
|
|
|
Lists all videos in a specified subscription
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param sub_id: ID of the subscription
|
|
|
:type sub_id: int
|
|
|
"""
|
|
@@ -327,15 +311,13 @@ def list_videos(conf, sub_id):
|
|
|
sys.exit(1)
|
|
|
else:
|
|
|
if data:
|
|
|
- sub = subscription.Sub(subscription_id=data[0][0],
|
|
|
- title=data[0][1],
|
|
|
- subscription_type=data[0][2],
|
|
|
- name=data[0][3],
|
|
|
+ sub = subscription.Sub(subscription_type=data[0][2],
|
|
|
+ name=data[0][3], site=data[0][7],
|
|
|
search=data[0][4],
|
|
|
- directory=data[0][5],
|
|
|
- disabled=data[0][6],
|
|
|
- site=data[0][7], conf=conf)
|
|
|
- videos = DATABASE.get_videos(sub.get_id(), conf)
|
|
|
+ subscription_id=data[0][0],
|
|
|
+ title=data[0][1], directory=data[0][5],
|
|
|
+ disabled=data[0][6])
|
|
|
+ videos = DATABASE.get_videos(sub.get_id(), CONFIGURATION)
|
|
|
sub.gather_videos(videos)
|
|
|
videos_list = sub.print_videos()
|
|
|
for video in videos_list:
|
|
@@ -368,17 +350,15 @@ def catchup(sub_id):
|
|
|
"please check if the ID given is correct."))
|
|
|
|
|
|
|
|
|
-def clean_database(conf):
|
|
|
+def clean_database():
|
|
|
"""
|
|
|
Initiates a database cleanup, deleting all videos that are no longer
|
|
|
in the scope of the query and vacuuming the database to free up space.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
"""
|
|
|
- subscription_list = DATABASE.get_subscriptions(conf)
|
|
|
+ subscription_list = DATABASE.get_subscriptions(CONFIGURATION)
|
|
|
for element in subscription_list:
|
|
|
- videos = DATABASE.get_videos(element.get_id(), conf)
|
|
|
+ videos = DATABASE.get_videos(element.get_id(), CONFIGURATION)
|
|
|
element.check_and_delete(videos)
|
|
|
for delete_video in element.to_delete:
|
|
|
LOGGER.debug(_("Deleting video %s from "
|
|
@@ -454,30 +434,28 @@ def print_license():
|
|
|
along with stov. If not, see <http://www.gnu.org/licenses/>.""")
|
|
|
|
|
|
|
|
|
-def download_notify(conf, subscriptions=None):
|
|
|
+def download_notify(subscriptions=None):
|
|
|
"""
|
|
|
starts an update of not yet downloaded videos and notifies the user
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param subscriptions: list of subscriptions to consider for downloading
|
|
|
:type subscriptions: list
|
|
|
"""
|
|
|
videos_downloaded, videos_failed, video_titles = \
|
|
|
- download_videos(conf, subscriptions)
|
|
|
- if videos_downloaded > 0 and conf.values["notify"] == "yes":
|
|
|
- msg = compose_email(conf, videos_downloaded, video_titles)
|
|
|
- send_email(conf, msg)
|
|
|
+ download_videos(subscriptions)
|
|
|
+ if videos_downloaded > 0 and CONFIGURATION.values["notify"] == "yes":
|
|
|
+ msg = compose_email(videos_downloaded, video_titles)
|
|
|
+ send_email(msg)
|
|
|
elif videos_downloaded == 0 and videos_failed == 0:
|
|
|
- if conf.values["notify"] == "no":
|
|
|
+ if CONFIGURATION.values["notify"] == "no":
|
|
|
LOGGER.info(_("There are no videos to be downloaded."))
|
|
|
- elif conf.values["notify"] == "no":
|
|
|
+ elif CONFIGURATION.values["notify"] == "no":
|
|
|
if videos_failed == 0:
|
|
|
LOGGER.info(_("The following videos have been downloaded:\n"))
|
|
|
for i in video_titles:
|
|
|
LOGGER.info(i)
|
|
|
else:
|
|
|
- if conf.values["notify"] != "yes":
|
|
|
+ if CONFIGURATION.values["notify"] != "yes":
|
|
|
LOGGER.error(_("Could not determine how you want to be informed "
|
|
|
"about new videos, please check the notify "
|
|
|
"parameter in your configuration."))
|
|
@@ -513,13 +491,11 @@ def list_sites():
|
|
|
LOGGER.info(entry[1])
|
|
|
|
|
|
|
|
|
-def get_subscriptions(conf, subscriptions=None):
|
|
|
+def get_subscriptions(subscriptions=None):
|
|
|
"""
|
|
|
Retrieves all or only specific subscriptions from the database and
|
|
|
returns them as a list of subscription objects.
|
|
|
|
|
|
- :param conf: configuration object
|
|
|
- :type conf: lib_stov.configuration.Conf
|
|
|
:param subscriptions: list of subscriptions to retrieve
|
|
|
:type subscriptions: list
|
|
|
:return: list of subscription objects
|
|
@@ -530,19 +506,17 @@ def get_subscriptions(conf, subscriptions=None):
|
|
|
for element in subscriptions:
|
|
|
data = DATABASE.get_subscription(element)
|
|
|
if data:
|
|
|
- sub = subscription.Sub(subscription_id=data[0][0],
|
|
|
- title=data[0][1],
|
|
|
- subscription_type=data[0][2],
|
|
|
- name=data[0][3],
|
|
|
+ sub = subscription.Sub(subscription_type=data[0][2],
|
|
|
+ name=data[0][3], site=data[0][7],
|
|
|
search=data[0][4],
|
|
|
- directory=data[0][5],
|
|
|
- disabled=data[0][6],
|
|
|
- site=data[0][7], conf=conf)
|
|
|
+ subscription_id=data[0][0],
|
|
|
+ title=data[0][1], directory=data[0][5],
|
|
|
+ disabled=data[0][6])
|
|
|
subscriptions_list.append(sub)
|
|
|
else:
|
|
|
LOGGER.error(
|
|
|
_("Invalid subscription, please check the list and "
|
|
|
"try again."))
|
|
|
else:
|
|
|
- subscriptions_list = DATABASE.get_subscriptions(conf)
|
|
|
+ subscriptions_list = DATABASE.get_subscriptions(CONFIGURATION)
|
|
|
return subscriptions_list
|