program.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. # This file is part of stov, written by Helmut Pozimski 2012-2021.
  2. #
  3. # stov is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 2 of the License.
  6. #
  7. # stov is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  14. # -*- coding: utf8 -*-
  15. """ This module contains the functions that make up the core of the
  16. application.
  17. """
  18. import logging
  19. import smtplib
  20. import socket
  21. import sys
  22. from email.mime.multipart import MIMEMultipart
  23. from email.mime.text import MIMEText
  24. from lib_stov import stov_exceptions
  25. from lib_stov import subscription
  26. from lib_stov.configuration import Conf
  27. from lib_stov.database import Db
  28. LOGGER = logging.getLogger("stov")
  29. def add_subscription(channel="", search="", playlist="", site="youtube"):
  30. """
  31. Takes care of adding a new subscription to the database.
  32. :param site: site the subscription is about to be created for
  33. :type site: str
  34. :param channel: optional channel name
  35. :type channel: str
  36. :param search: optional search string
  37. :type search: str
  38. :param playlist: optional playlist ID
  39. :type playlist: str
  40. """
  41. database = Db.get_instance()
  42. LOGGER.debug(_("Creating new subscription with the following "
  43. "parameters:\nChannel: %s\nSearch: %s\nPlaylist: %s"),
  44. channel, search, playlist)
  45. try:
  46. if channel and not search:
  47. new_subscription = subscription.Sub(subscription_type="user",
  48. name=channel, site=site)
  49. elif channel and search:
  50. new_subscription = subscription.Sub(subscription_type="user",
  51. name=channel, site=site,
  52. search=search)
  53. elif not channel and search:
  54. new_subscription = subscription.Sub(subscription_type="search",
  55. name=_("Search_"), site=site,
  56. search=search)
  57. elif playlist:
  58. if search:
  59. LOGGER.error(_("Playlists do not support searching, the "
  60. "search option will be ignored!"))
  61. new_subscription = subscription.Sub(subscription_type="playlist",
  62. name=playlist, site=site)
  63. else:
  64. LOGGER.error(_("None or invalid subscription type given, please "
  65. "check the type option and try again."))
  66. sys.exit(1)
  67. except stov_exceptions.TypeNotSupported as error:
  68. LOGGER.error(error)
  69. sys.exit(1)
  70. try:
  71. subscription_data = new_subscription.add_sub()
  72. site_id = database.get_site_id(subscription_data[6])
  73. new_sub_data = (subscription_data[0], subscription_data[1],
  74. subscription_data[2], subscription_data[3],
  75. subscription_data[4], subscription_data[5],
  76. site_id)
  77. subscription_id = database.insert_subscription(new_sub_data)
  78. new_subscription.set_id(subscription_id)
  79. except stov_exceptions.DBWriteAccessFailedException as error:
  80. LOGGER.error(error)
  81. sys.exit(1)
  82. except stov_exceptions.ChannelNotFound as error:
  83. LOGGER.error(error)
  84. sys.exit(1)
  85. else:
  86. LOGGER.debug(_("Subscription sucessfully inserted into database."))
  87. try:
  88. new_subscription.update_data()
  89. except stov_exceptions.YoutubeAPITimeoutException as error:
  90. LOGGER.error(error)
  91. except stov_exceptions.NoDataFromYoutubeAPIException as error:
  92. LOGGER.error(error)
  93. for video in new_subscription.parsed_response.videos:
  94. if not database.video_in_database(video.video_id):
  95. if new_subscription.check_string_match(video):
  96. try:
  97. database.insert_video(video, new_subscription.get_id())
  98. except stov_exceptions.DBWriteAccessFailedException as error:
  99. LOGGER.error(error)
  100. sys.exit(1)
  101. else:
  102. LOGGER.debug(_("Video %s successfully inserted into "
  103. "database."), video.title)
  104. LOGGER.info(_("New subscription ") + new_subscription.get_title() +
  105. _(" successfully added"))
  106. def list_subscriptions():
  107. """
  108. Prints a list of subscriptions from the database.
  109. """
  110. database = Db.get_instance()
  111. conf = Conf.get_instance()
  112. subscriptions_list = database.get_subscriptions(conf)
  113. sub_state = None
  114. if subscriptions_list:
  115. LOGGER.info(_("ID Title Site"))
  116. for sub in subscriptions_list:
  117. if not sub.disabled:
  118. sub_state = _("enabled")
  119. elif sub.disabled:
  120. sub_state = _("disabled")
  121. LOGGER.info(str(sub.get_id()) + " " + sub.get_title() +
  122. " " + sub.site + " " + "(%s)" % sub_state)
  123. else:
  124. LOGGER.info(_("No subscriptions added yet, add one!"))
  125. def delete_subscription(sub_id):
  126. """
  127. Deletes a specified subscription from the database
  128. :param sub_id: ID of the subscription to be deleted
  129. :type sub_id: int
  130. """
  131. database = Db.get_instance()
  132. try:
  133. database.delete_subscription(sub_id)
  134. except stov_exceptions.SubscriptionNotFoundException as error:
  135. LOGGER.error(error)
  136. sys.exit(1)
  137. except stov_exceptions.DBWriteAccessFailedException as error:
  138. LOGGER.error(error)
  139. sys.exit(1)
  140. else:
  141. LOGGER.info(_("Subscription deleted successfully!"))
  142. def update_subscriptions(subscriptions=None):
  143. """
  144. Updates data about videos in a subscription.
  145. :param subscriptions: list of subscriptions to update
  146. :type subscriptions: list
  147. """
  148. database = Db.get_instance()
  149. subscriptions_list = get_subscriptions(subscriptions)
  150. for element in subscriptions_list:
  151. LOGGER.debug(_("Updating subscription %s"), element.get_title())
  152. videos = database.get_videos(element.get_id())
  153. element.gather_videos(videos)
  154. try:
  155. element.update_data()
  156. except stov_exceptions.YoutubeAPITimeoutException as error:
  157. LOGGER.error(error)
  158. except stov_exceptions.NoDataFromYoutubeAPIException as error:
  159. LOGGER.error(error)
  160. for video in element.parsed_response.videos:
  161. if not database.video_in_database(video.video_id):
  162. if element.check_string_match(video):
  163. try:
  164. database.insert_video(video, element.get_id())
  165. except stov_exceptions.DBWriteAccessFailedException as \
  166. error:
  167. LOGGER.error(error)
  168. sys.exit(1)
  169. else:
  170. LOGGER.debug(_("Video %s successfully inserted into "
  171. "database."), video.title)
  172. def download_videos(subscriptions=None):
  173. """
  174. Downloads videos that haven't been previously downloaded.
  175. :param subscriptions: list of subscriptions to consider for downloading
  176. :type subscriptions: list
  177. :return: tuple containing (in that order) downloaded videos, failed \
  178. videos and a list of the videos downloaded
  179. :rtype: tuple
  180. """
  181. database = Db.get_instance()
  182. conf = Conf.get_instance()
  183. video_titles = []
  184. subscriptions_list = get_subscriptions(subscriptions)
  185. videos_downloaded = 0
  186. videos_failed = 0
  187. for sub in subscriptions_list:
  188. videos = database.get_videos(sub.get_id())
  189. sub.gather_videos(videos)
  190. try:
  191. sub.download_videos()
  192. except stov_exceptions.SubscriptionDisabledException as error:
  193. LOGGER.debug(error)
  194. for entry in sub.downloaded_videos:
  195. database.update_video_download_status(entry.get_id(), 1)
  196. video_titles.append(entry.title)
  197. videos_downloaded = len(video_titles)
  198. videos_failed = videos_failed + sub.failed_videos_count
  199. for video in sub.failed_videos:
  200. try:
  201. database.update_video_fail_count(video.failcnt, video.get_id())
  202. if video.failcnt >= int(conf.values["maxfails"]):
  203. database.disable_failed_video(video.get_id())
  204. except stov_exceptions.DBWriteAccessFailedException as error:
  205. LOGGER.error(error)
  206. sys.exit(1)
  207. return videos_downloaded, videos_failed, video_titles
  208. def compose_email(downloaded_videos, video_titles):
  209. """
  210. Composes an e-mail that can be send out to the user.
  211. :param downloaded_videos: number of downloaded videos
  212. :type downloaded_videos: int
  213. :param video_titles: titles of the downloaded videos
  214. :type video_titles: list
  215. :return: e-mail contents
  216. :rtype: MIMEMultipart
  217. """
  218. conf = Conf.get_instance()
  219. mail_text = ""
  220. msg = MIMEMultipart()
  221. if downloaded_videos == 1:
  222. msg["Subject"] = _("Downloaded %i new video") % downloaded_videos
  223. mail_text = _("The following episode has been downloaded by stov: "
  224. "\n\n")
  225. else:
  226. msg["Subject"] = _("Downloaded %i new videos") % downloaded_videos
  227. mail_text = _("The following episodes have been downloaded by "
  228. "stov: \n\n")
  229. msg["From"] = "stov <%s>" % conf.values["mailfrom"]
  230. msg["To"] = "<%s>" % conf.values["mailto"]
  231. for line in video_titles:
  232. mail_text += line + "\n"
  233. msg_text = MIMEText(mail_text.encode("utf8"), _charset="utf8")
  234. msg.attach(msg_text)
  235. return msg
  236. def send_email(msg):
  237. """
  238. Sends an e-mail to the user.
  239. :param msg: message to be sent
  240. :type msg: MIMEMultipart
  241. """
  242. conf = Conf.get_instance()
  243. server_connection = smtplib.SMTP(conf.values["mailhost"],
  244. conf.values["smtpport"])
  245. try:
  246. server_connection.connect()
  247. except (smtplib.SMTPConnectError, smtplib.SMTPServerDisconnected,
  248. socket.error):
  249. LOGGER.error(_("Could not connect to the smtp server, please check"
  250. " your settings!"))
  251. else:
  252. try:
  253. server_connection.starttls()
  254. except smtplib.SMTPException:
  255. LOGGER.debug(_("TLS not available, proceeding unencrypted."))
  256. if conf.values["auth_needed"] == "yes":
  257. try:
  258. server_connection.login(conf.values["user_name"],
  259. conf.values["password"])
  260. except smtplib.SMTPAuthenticationError:
  261. LOGGER.error(_("Authentication failed, please check user "
  262. "name and password!"))
  263. except smtplib.SMTPException:
  264. LOGGER.error(_("Could not authenticate, server probably "
  265. "does not support authentication!"))
  266. try:
  267. server_connection.sendmail(conf.values["mailfrom"],
  268. conf.values["mailto"],
  269. msg.as_string())
  270. except smtplib.SMTPRecipientsRefused:
  271. LOGGER.error(_("The server refused the recipient address, "
  272. "please check your settings."))
  273. except smtplib.SMTPSenderRefused:
  274. LOGGER.error(_("The server refused the sender address, "
  275. "please check your settings."))
  276. server_connection.quit()
  277. def list_videos(sub_id):
  278. """
  279. Lists all videos in a specified subscription
  280. :param sub_id: ID of the subscription
  281. :type sub_id: int
  282. """
  283. database = Db.get_instance()
  284. try:
  285. data = database.get_subscription(sub_id)
  286. except stov_exceptions.DBWriteAccessFailedException as error:
  287. LOGGER.error(error)
  288. sys.exit(1)
  289. else:
  290. if data:
  291. sub = subscription.Sub(subscription_type=data[0][2],
  292. name=data[0][3], site=data[0][7],
  293. search=data[0][4],
  294. subscription_id=data[0][0],
  295. title=data[0][1], directory=data[0][5],
  296. disabled=data[0][6])
  297. videos = database.get_videos(sub.get_id())
  298. sub.gather_videos(videos)
  299. videos_list = sub.print_videos()
  300. for video in videos_list:
  301. LOGGER.info(video)
  302. else:
  303. LOGGER.error(_("Invalid subscription, please check the list and "
  304. "try again."))
  305. def catchup(sub_id):
  306. """
  307. Marks all videos in a subscription as downloaded
  308. :param sub_id: ID of the subscription
  309. :type sub_id: int
  310. """
  311. database = Db.get_instance()
  312. try:
  313. sub_data = database.get_subscription_title(sub_id)
  314. except stov_exceptions.DBWriteAccessFailedException as error:
  315. LOGGER.error(error)
  316. sys.exit(1)
  317. else:
  318. if sub_data:
  319. try:
  320. database.mark_video_downloaded(sub_id)
  321. except stov_exceptions.DBWriteAccessFailedException as error:
  322. LOGGER.error(error)
  323. else:
  324. LOGGER.error(_("The subscription could not be updated, "
  325. "please check if the ID given is correct."))
  326. def clean_database():
  327. """
  328. Initiates a database cleanup, deleting all videos that are no longer
  329. in the scope of the query and vacuuming the database to free up space.
  330. """
  331. database = Db.get_instance()
  332. conf = Conf.get_instance()
  333. subscription_list = database.get_subscriptions()
  334. for element in subscription_list:
  335. videos = database.get_videos(element.get_id(), conf)
  336. element.check_and_delete(videos)
  337. for delete_video in element.to_delete:
  338. LOGGER.debug(_("Deleting video %s from "
  339. "database"), delete_video.title)
  340. try:
  341. database.delete_video(delete_video.get_id())
  342. except stov_exceptions.DBWriteAccessFailedException as error:
  343. LOGGER.error(error)
  344. sys.exit(1)
  345. try:
  346. database.vacuum()
  347. except stov_exceptions.DBWriteAccessFailedException as error:
  348. LOGGER.error(error)
  349. sys.exit(1)
  350. def change_subscription_state(sub_id, enable=False):
  351. """
  352. Enables or disables a subscription.
  353. :param sub_id: ID of the subscription
  354. :type sub_id: int
  355. :param enable: whether to enable or disable the subscription
  356. :type enable: bool
  357. """
  358. database = Db.get_instance()
  359. subscription_state = database.get_subscription(sub_id)
  360. try:
  361. if enable:
  362. if int(subscription_state[0][6]) == 0:
  363. LOGGER.error(_("The subscription ID %s is already enabled."),
  364. sub_id)
  365. elif int(subscription_state[0][6]) == 1:
  366. try:
  367. database.change_subscription_state(sub_id, 0)
  368. except stov_exceptions.DBWriteAccessFailedException as error:
  369. LOGGER.error(error)
  370. sys.exit(1)
  371. else:
  372. LOGGER.info(_("Enabled subscription ID %s."), sub_id)
  373. else:
  374. if int(subscription_state[0][6]) == 1:
  375. LOGGER.error(_("Subscription ID %s is already disabled."),
  376. sub_id)
  377. elif int(subscription_state[0][6]) == 0:
  378. try:
  379. database.change_subscription_state(sub_id, 1)
  380. except stov_exceptions.DBWriteAccessFailedException as error:
  381. LOGGER.error(error)
  382. sys.exit(1)
  383. else:
  384. LOGGER.info(_("Disabled subscription ID %s."),
  385. sub_id)
  386. except IndexError:
  387. LOGGER.error(_("Could not find the subscription with ID %s, "
  388. "please check and try again."), sub_id)
  389. def print_license():
  390. """
  391. Prints the license of the application.
  392. """
  393. LOGGER.info("""
  394. stov is free software: you can redistribute it and/or modify
  395. it under the terms of the GNU General Public License as published by
  396. the Free Software Foundation, version 2 of the License.
  397. stov is distributed in the hope that it will be useful,
  398. but WITHOUT ANY WARRANTY; without even the implied warranty of
  399. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  400. GNU General Public License for more details.
  401. You should have received a copy of the GNU General Public License
  402. along with stov. If not, see <http://www.gnu.org/licenses/>.""")
  403. def download_notify(subscriptions=None):
  404. """
  405. starts an update of not yet downloaded videos and notifies the user
  406. :param subscriptions: list of subscriptions to consider for downloading
  407. :type subscriptions: list
  408. """
  409. conf = Conf.get_instance()
  410. videos_downloaded, videos_failed, video_titles = \
  411. download_videos(subscriptions)
  412. if videos_downloaded > 0 and conf.values["notify"] == "yes":
  413. msg = compose_email(videos_downloaded, video_titles)
  414. send_email(msg)
  415. elif videos_downloaded == 0 and videos_failed == 0:
  416. if conf.values["notify"] == "no":
  417. LOGGER.info(_("There are no videos to be downloaded."))
  418. elif conf.values["notify"] == "no":
  419. if videos_failed == 0:
  420. LOGGER.info(_("The following videos have been downloaded:\n"))
  421. for i in video_titles:
  422. LOGGER.info(i)
  423. else:
  424. if conf.values["notify"] != "yes":
  425. LOGGER.error(_("Could not determine how you want to be informed "
  426. "about new videos, please check the notify "
  427. "parameter in your configuration."))
  428. def initialize_sites():
  429. """
  430. Adds sites to the database if they are not in there yet.
  431. """
  432. database = Db.get_instance()
  433. supported_sites = ["youtube", "zdf_mediathek", "twitch"]
  434. sites = database.get_sites()
  435. for site in supported_sites:
  436. site_found = False
  437. for result in sites:
  438. if site in result:
  439. site_found = True
  440. if not site_found:
  441. database.add_site(site)
  442. for site in sites:
  443. if site[1] not in supported_sites:
  444. database.remove_site(site[1])
  445. def list_sites():
  446. """
  447. Lists the currently supported sites.
  448. """
  449. database = Db.get_instance()
  450. sites = database.get_sites()
  451. LOGGER.info(_("Sites currently supported by stov:"))
  452. for entry in sites:
  453. LOGGER.info(entry[1])
  454. def get_subscriptions(subscriptions=None):
  455. """
  456. Retrieves all or only specific subscriptions from the database and
  457. returns them as a list of subscription objects.
  458. :param subscriptions: list of subscriptions to retrieve
  459. :type subscriptions: list
  460. :return: list of subscription objects
  461. :rtype: list
  462. """
  463. database = Db.get_instance()
  464. if subscriptions:
  465. subscriptions_list = []
  466. for element in subscriptions:
  467. data = database.get_subscription(element)
  468. if data:
  469. sub = subscription.Sub(subscription_type=data[0][2],
  470. name=data[0][3], site=data[0][7],
  471. search=data[0][4],
  472. subscription_id=data[0][0],
  473. title=data[0][1], directory=data[0][5],
  474. disabled=data[0][6])
  475. subscriptions_list.append(sub)
  476. else:
  477. LOGGER.error(
  478. _("Invalid subscription, please check the list and "
  479. "try again."))
  480. else:
  481. subscriptions_list = database.get_subscriptions()
  482. return subscriptions_list