diff --git a/api.py b/api.py deleted file mode 100644 index b5fe234..0000000 --- a/api.py +++ /dev/null @@ -1,30 +0,0 @@ -import flask -from flask import Response, request -from src.main import main -from werkzeug.exceptions import BadRequest -from youtube_transcript_api import YouTubeTranscriptApi as ytcc -import random - -app = flask.Flask(__name__) - -@app.errorhandler(BadRequest) -def handle_invalid_ytid(e): - return Response("{'status': 400, 'reason': A valid YouTube video ID has not been provided}", status = 400, mimetype = 'application/json') - -@app.errorhandler(ytcc.CouldNotRetrieveTranscript) -def handle_missing_caption(e): - return Response("{'status': 400, 'reason': The video either does not exist or does not have captions enabled}", status = 400, mimetype = 'application/json') - -@app.route('/', defaults={'path': ''}) -@app.route('/') -def home(path): - try: - vid = request.args["video_id"] - out = f"out/{vid[-11:]}.png" - wordcloud = main(vid, out) - filepath = request.base_url + wordcloud - return Response(f"{{'status': 201, 'video': {vid}, 'wordcloud': {filepath}}}", status = 201, mimetype = 'application/json') - except Exception as e: - return Response(f"Error 502: {e}", status = 502, mimetype = 'application/json') - -app.run() diff --git a/now.json b/now.json deleted file mode 100644 index d940f9e..0000000 --- a/now.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": 2, - "name": "VideoCloud", - "builds": [ - { - "src": "**/*.py", - "use": "@now/python", - "config": { "maxLambdaSize": "50mb" } - } - ], - "alias": ["videocloud"] -} diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 571c6fa..0000000 --- a/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -pillow==6.0.0 -wordcloud==1.5.0 -youtube-transcript-api==0.1.4 -flask==1.0.3 diff --git a/scripts/videocloud b/scripts/videocloud new file mode 100644 index 0000000..634180d --- /dev/null +++ b/scripts/videocloud @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +if [[ ! $@ ]]; then + python -m videocloud -h +else + python -m videocloud $@ +fi diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d55a5d9 --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +import setuptools + +with open("README.md") as f: + long_description = f.read() + +setuptools.setup( + name="VideoCloud", + version="1.1", + url="https://github.com/paramt/VideoCloud", + license="MIT", + author="Param Thakkar", + author_email="contact@param.me", + description="A command line tool that generates word clouds from YouTube video captions", + long_description=long_description, + long_description_content_type="text/markdown", + scripts=["./scripts/videocloud"], + packages=["videocloud"], + install_requires=[ + "setuptools", + "pillow==6.0.0", + "wordcloud==1.5.0", + "youtube-transcript-api==0.1.4" + ], + python_requires=">= 3.6" +) diff --git a/src/main.py b/src/main.py deleted file mode 100644 index 0d6e228..0000000 --- a/src/main.py +++ /dev/null @@ -1,12 +0,0 @@ -from src.utilities import get_video_id, generate_word_cloud as word_cloud, get_captions as get_cc - -def main(url: str, filepath="out/test.png"): - video_id = get_video_id(url) - captions = get_cc(video_id) - image = word_cloud(captions) - image.save(filepath) - return filepath - -if __name__ == "__main__": - print("Please enter the link to a YouTube video") - main(input()) diff --git a/src/__init__.py b/videocloud/__init__.py similarity index 100% rename from src/__init__.py rename to videocloud/__init__.py diff --git a/videocloud/__main__.py b/videocloud/__main__.py new file mode 100644 index 0000000..9430c1c --- /dev/null +++ b/videocloud/__main__.py @@ -0,0 +1,35 @@ +import os +import sys +from videocloud.utilities import get_video_id, generate_word_cloud as word_cloud, get_captions as get_cc +from youtube_transcript_api import YouTubeTranscriptApi as ytcc + + +def main(url: str, filepath): + try: + video_id = get_video_id(url) + captions = get_cc(video_id) + image = word_cloud(captions) + image.save(filepath) + except ytcc.CouldNotRetrieveTranscript: + print("The specified video either doesn't exist or doesn't have captions enabled. Please try again") + exit() + except IOError: + print("There was an error saving the wordcloud file") + exit() + + return os.path.abspath(filepath) + +if __name__ == "__main__": + try: + video_id = sys.argv[1] + except: + print("Please specify a YouTube video link or video ID") + exit() + + try: + filepath = sys.argv[2] + except IndexError: + filepath = "wordcloud.png" + + wordcloud = main(video_id, filepath) + print(f"Wordcloud created in {wordcloud}") diff --git a/src/constants.py b/videocloud/constants.py similarity index 100% rename from src/constants.py rename to videocloud/constants.py diff --git a/src/utilities.py b/videocloud/utilities.py similarity index 95% rename from src/utilities.py rename to videocloud/utilities.py index f799bd5..9e75d83 100644 --- a/src/utilities.py +++ b/videocloud/utilities.py @@ -1,7 +1,7 @@ from PIL.Image import Image from wordcloud import WordCloud from youtube_transcript_api import YouTubeTranscriptApi as ytcc -from src import constants +from videocloud import constants def get_video_id(url: str) -> str: return(url[-11:])