1
0

program.py 20 KB

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