123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #! /usr/bin/env python
- # -*- coding: utf8 -*-
- """stov - a program to subscribe to channels and users from online video portals and download
- the videos automatically
- written by Helmut Pozimski in 2012
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; version 2
- of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA."""
- import sys
- import gettext
- import os
- import subscription
- import youtube
- import configuration
- import sqlite3
- import email
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from optparse import OptionParser
- """Initialize gettext to support translation of the program"""
- trans = gettext.translation("stov", "locale", ["de"])
- trans.install()
- """Process the given options and parameters,
- add: Add a new subscription (which can be a search, channel or playlist)
- lssubs: List the currently available subscriptions
- remove: remove a subscription
- update: update the information about the available videos
- download: download all available videos which haven't already been downloaded
- search: optionally add a search string to a new subscription"""
-
- parser = OptionParser(usage=_("stov.py option1 parameter1 [option2] [parameter2] ..."))
- parser.add_option("-a", "--add", dest="add", help=_("Add a new channel subscription"))
- parser.add_option("-p", "--playlist", dest="playlist", help=_("Add a new Playlist subscription"))
- parser.add_option("-l", "--lssubs", action="store_true", dest="list", help=_("List the currently available subscriptions"))
- parser.add_option("-r", "--remove", dest="delete", help=_("remove a subscription"))
- parser.add_option("-u", "--update", action="store_true", dest="update", help=_("update the information about the available videos"))
- parser.add_option("-d", "--download", action="store_true", dest="download", help=_("download all available videos which haven't already been downloaded"))
- parser.add_option("-s", "--search", dest="search", help=_("optionally add a search string to a new channel subscription or create a new search subscription"))
- parser.add_option("-x", "--listvids", dest="lsv", help=_("Print all videos from a subscription"))
- (options, arguments) = parser.parse_args()
- """Create the lock file which is used to determine if another instance is already running by chance, the program shouldn't be run in this case
- since we want to prevent concurent access to the database"""
- if os.access("/tmp/stov.lock", os.F_OK):
- print >> sys.stderr, _("The lock file already exists, probably another instance of this program is already running\n \
- if you are sure this is not the case, delete it manually and try again!")
- sys.exit(1)
- else:
- try:
- lockfile = open("/tmp/stov.lock", "w")
- lockfile.write(str(os.getpid()))
- lockfile.close()
- except IOError:
- print _("Lock file could not be created, please check that /tmp is writable and properly configured, ending now")
- sys.exit(1)
- """youtube-dl is really a dependency but the program will run with limited functionality without it so we need to check that here"""
- if os.system("which youtube-dl > /dev/null") != 0:
- print >> sys.stderr, _("Could not find youtube-dl. Please note that youtube-dl is not needed for the program to run but is needed to use the download option which won't work otherwise")
- if os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK) != True:
- print _("This seems to be the first time you run the programm, it will now create the default configuration for you")
- conf = configuration.conf()
- conf.Initialize()
- else:
- conf = configuration.conf()
- conf.ReadConfig()
- MailContent = []
- if options.add != None:
- if options.search != None:
- NewSubscription = subscription.sub(type = "channel", name = options.add, search = options.search, conf = conf)
- else:
- NewSubscription = subscription.sub(type = "channel", name = options.add, conf = conf)
- NewSubscription.AddSub()
- NewSubscription.GetVideos()
- print _("New subscription ") + NewSubscription.GetTitle() + _(" successfully added")
- elif options.playlist != None:
- if options.search != None:
- print _("Playlists do not support searching, search option will be ignored!")
- NewSubscription = subscription.sub(type = "playlist", name = options.playlist, conf = conf)
- NewSubscription.AddSub()
- NewSubscription.GetVideos()
- print _("New playlist subscription successfully added")
- elif options.search != None:
- NewSubscription = subscription.sub(type = "search", name = _("Search_"), search = options.search, conf = conf)
- NewSubscription.AddSub()
- NewSubscription.GetVideos()
- print _("New search subscription successfully added")
- elif options.list == True:
- try:
- database = sqlite3.connect(conf.dbpath)
- cursor = database.cursor()
- cursor.execute("SELECT id, title FROM subscriptions")
- ListofSubscriptions = cursor.fetchall()
- except sqlite3.OperationalError:
- print _("Could not access the database, please check path and permissions and try again!")
- print _("ID Title")
- for subscription in ListofSubscriptions:
- if subscription[0] != None:
- print str(subscription[0]) + " " + subscription[1]
- database.close()
- elif options.delete != None:
- try:
- DeleteId = int(options.delete)
- Subscription = subscription.sub(type = "", name = "delete", id = DeleteId, conf = conf)
- except ValueError:
- print _("Invalid Option, please use the ID of the subscription you want to delete as parameter for the remove option")
- Subscription.Delete()
- elif options.update == True:
- ListofSubscriptions = []
- try:
- database = sqlite3.connect(conf.dbpath)
- cursor = database.cursor()
- cursor.execute("SELECT id,title,type,name,searchstring,directory FROM subscriptions")
- Subscriptions = cursor.fetchall()
- except sqlite3.OperationalError:
- print _("Could not access the database, please check path and permissions and try again!")
- for element in Subscriptions:
- ListofSubscriptions.append(subscription.sub(id = element[0], title = element[1], type = element[2], name = element[3], search = element[4], directory = element[5], conf = conf))
- for element in ListofSubscriptions:
- element.GetVideos()
- elif options.download == True:
- ListofSubscriptions = []
- try:
- database = sqlite3.connect(conf.dbpath)
- cursor = database.cursor()
- cursor.execute("SELECT id,title,type,name,searchstring,directory FROM subscriptions")
- Subscriptions = cursor.fetchall()
- except sqlite3.OperationalError:
- print _("Could not access the database, please check path and permissions and try again!")
- for element in Subscriptions:
- ListofSubscriptions.append(subscription.sub(id = element[0], title = element[1], type = element[2], name = element[3], search = element[4], directory = element[5], conf = conf))
- VideosDownloaded = 0
- for element in ListofSubscriptions:
- element.GetVideos()
- element.DownloadVideos()
- for entry in element.DownloadedVideos:
- MailContent.append(entry)
- VideosDownloaded = len(MailContent)
- if VideosDownloaded > 0:
- MailText = u""
- msg = MIMEMultipart()
- msg["Subject"] = _("Downloaded %i new videos") %VideosDownloaded
- msg["From"] = "stov <%s>" %conf.values["mailfrom"]
- msg["To"] = "<%s>" %conf.values["mailto"]
- MailText = _("The following episodes have been downloaded by stov: \n\n")
- for line in MailContent:
- MailText += line + "\n"
- msgtext = MIMEText(MailText.encode("utf8"), _charset="utf8")
- msg.attach(msgtext)
- Mail = smtplib.SMTP(conf.values["mailhost"], conf.values["smtpport"])
- try:
- Mail = smtplib.SMTP(conf.values["mailhost"], conf.values["smtpport"])
- except SMTPHeloError:
- _("Could not connect to the smtp server, please check your settings!")
- Mail.sendmail(conf.values["mailfrom"],conf.values["mailto"],msg.as_string())
- Mail.quit()
-
- elif options.lsv != None:
- try:
- database = sqlite3.connect(conf.dbpath)
- cursor = database.cursor()
- cursor.execute("SELECT id,title,type,name,searchstring,directory FROM subscriptions where id=%s" %options.lsv)
- Data = cursor.fetchall()
- except sqlite3.OperationalError:
- print _("Could not access the database, please check path and permissions and try again!")
- Subscription = subscription.sub(id = Data[0][0], title = Data[0][1], type = Data[0][2], name = Data[0][3], search = Data[0][4], directory = Data[0][5], conf = conf)
- Subscription.GetVideos()
- Subscription.PrintVideos()
- """Remove the lock file and end the program so it can be run again"""
- try:
- os.remove("/tmp/stov.lock")
- sys.exit(0)
- except os.error:
- print >> sys.stderr, _("Could not delete the lock file. Please check what went wrong and clean up manually!")
|