__main__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # tubeup.py - Download a video using youtube-dl and upload to the Internet Archive with metadata
  4. # Copyright (C) 2018 Bibliotheca Anonoma
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. """tubeup - Download a video with Youtube-dlc, then upload to Internet Archive, passing all metadata.
  18. Usage:
  19. tubeup <url>... [--username <user>] [--password <pass>]
  20. [--metadata=<key:value>...]
  21. [--cookies=<filename>]
  22. [--proxy <prox>]
  23. [--quiet] [--debug]
  24. [--use-download-archive]
  25. [--output <output>]
  26. [--get-comments]
  27. [--ignore-existing-item]
  28. tubeup -h | --help
  29. tubeup --version
  30. Arguments:
  31. <url> Youtube-dlc compatible URL to download.
  32. Check Youtube-dlc documentation for a list
  33. of compatible websites.
  34. --metadata=<key:value> Custom metadata to add to the archive.org
  35. item.
  36. Options:
  37. -h --help Show this screen.
  38. -p --proxy <prox> Use a proxy while uploading.
  39. -u --username <user> Provide a username, for sites like Nico Nico Douga.
  40. -p --password <pass> Provide a password, for sites like Nico Nico Douga.
  41. -a --use-download-archive Record the video url to the download archive.
  42. This will download only videos not listed in
  43. the archive file. Record the IDs of all
  44. downloaded videos in it.
  45. -q --quiet Just print errors.
  46. -d --debug Print all logs to stdout.
  47. -o --output <output> Youtube-dlc output template.
  48. -c --get-comments Scrape video comments.
  49. -i --ignore-existing-item Don't check if an item already exists on archive.org
  50. """
  51. import sys
  52. import docopt
  53. import logging
  54. import traceback
  55. import internetarchive
  56. import internetarchive.cli
  57. from tubeup.TubeUp import TubeUp
  58. from tubeup import __version__
  59. def main():
  60. # Parse arguments from file docstring
  61. args = docopt.docopt(__doc__, version=__version__)
  62. URLs = args['<url>']
  63. cookie_file = args['--cookies']
  64. proxy_url = args['--proxy']
  65. username = args['--username']
  66. password = args['--password']
  67. quiet_mode = args['--quiet']
  68. debug_mode = args['--debug']
  69. use_download_archive = args['--use-download-archive']
  70. get_comments = args['--get-comments']
  71. ignore_existing_item = args['--ignore-existing-item']
  72. if debug_mode:
  73. # Display log messages.
  74. root = logging.getLogger()
  75. root.setLevel(logging.DEBUG)
  76. ch = logging.StreamHandler(sys.stdout)
  77. ch.setLevel(logging.DEBUG)
  78. formatter = logging.Formatter(
  79. '\033[92m[DEBUG]\033[0m %(asctime)s - %(name)s - %(levelname)s - '
  80. '%(message)s')
  81. ch.setFormatter(formatter)
  82. root.addHandler(ch)
  83. metadata = internetarchive.cli.argparser.get_args_dict(args['--metadata'])
  84. tu = TubeUp(verbose=not quiet_mode,
  85. output_template=args['--output'],
  86. get_comments=get_comments)
  87. try:
  88. for identifier, meta in tu.archive_urls(URLs, metadata,
  89. cookie_file, proxy_url,
  90. username, password,
  91. use_download_archive,
  92. ignore_existing_item):
  93. print('\n:: Upload Finished. Item information:')
  94. print('Title: %s' % meta['title'])
  95. print('Upload URL: https://archive.org/details/%s\n' % identifier)
  96. except Exception:
  97. print('\n\033[91m' # Start red color text
  98. 'An exception just occured, if you found this '
  99. "exception isn't related with any of your connection problem, "
  100. 'please report this issue to '
  101. 'https://github.com/bibanon/tubeup/issues')
  102. traceback.print_exc()
  103. print('\033[0m') # End the red color text
  104. sys.exit(1)
  105. if __name__ == '__main__':
  106. main()