├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── examples ├── upload_multiple.py └── upload_one.py ├── opplast ├── __init__.py ├── constants.py ├── exceptions.py ├── logging.py └── upload.py └── setup.py /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=crlf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .venv 2 | test.py 3 | *.log 4 | opplast/__pycache__ 5 | dist/ 6 | *.egg-info -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 offish 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # opplast 2 | [![Version](https://img.shields.io/pypi/v/opplast.svg)](https://pypi.org/project/opplast) 3 | [![License](https://img.shields.io/github/license/offish/opplast.svg)](https://github.com/offish/opplast/blob/master/LICENSE) 4 | [![Stars](https://img.shields.io/github/stars/offish/opplast.svg)](https://github.com/offish/opplast/stargazers) 5 | [![Issues](https://img.shields.io/github/issues/offish/opplast.svg)](https://github.com/offish/opplast/issues) 6 | [![Size](https://img.shields.io/github/repo-size/offish/opplast.svg)](https://github.com/offish/opplast) 7 | [![Discord](https://img.shields.io/discord/467040686982692865?color=7289da&label=Discord&logo=discord)](https://discord.gg/t8nHSvA) 8 | [![Code style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) 9 | 10 | [![Donate Steam](https://img.shields.io/badge/donate-steam-green.svg)](https://steamcommunity.com/tradeoffer/new/?partner=293059984&token=0-l_idZR) 11 | [![Donate PayPal](https://img.shields.io/badge/donate-paypal-blue.svg)](https://www.paypal.me/0ffish) 12 | 13 | Upload videos to YouTube using geckodriver, Firefox profiles and Selenium. Easy to setup and use. Inspired by [youtube_uploader_selenium](https://github.com/linouk23/youtube_uploader_selenium). 14 | 15 | "Opplast" is norwegian for "upload". 16 | 17 | ## Installing 18 | Install and update using pip: 19 | 20 | ``` 21 | pip install --upgrade opplast 22 | ``` 23 | 24 | ## geckodriver 25 | Download [geckodriver](https://github.com/mozilla/geckodriver/releases) and place it inside `C:\Users\USERNAME\AppData\Local\Programs\Python\Python37`. If you are using another version of Python, you place it inside there. This only works if Python is added to PATH and it is the correct version. 26 | 27 | You can check if geckodriver has been added to PATH by running `geckodriver --version` in the terminal. If this gives you an error or you are running into: 28 | ### selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 29 | Specify `executable_path` in the [Configuration](https://github.com/offish/opplast#configuration) part. 30 | 31 | ## Configuration 32 | Open Firefox, and go to `about:profiles`. Click "Create a New profile" and name it "Selenium" or whatever. Copy the "Root Directory" path of the new profile. This is your `root_profile_directory`. Now you can "Launch profile in new browser", go to [YouTube](https://youtube.com), and login to the account you want to upload with. 33 | 34 | It's highly recommended that you clear your standard upload settings on YouTube. 35 | 36 | ```python 37 | Upload(root_profile_directory: str, executable_path: str = "geckodriver", timeout: int = 3, headless: bool = True, debug: bool = True, options: FirefoxOptions = webdriver.FirefoxOptions()) -> None 38 | ``` 39 | `root_profile_directory: str` - path to Firefox profile where you're logged into YouTube. 40 | 41 | `executable_path: str` - full path to override which geckodriver binary to use for Firefox 47.0.1 and greater, which defaults to picking up the binary from the system path. Example: `r"C:/Users/USERNAME/Desktop/geckodriver"` (if geckodriver.exe is located in Desktop folder) Default: `geckodriver`. 42 | 43 | `timeout: int` - seconds Selenium should wait, when getting pages and inserting data. Default: `3`. 44 | 45 | `headless: bool` - whether or not you want to see the browser GUI. **Will override headless option if specified in `options`.** Default: `True` (hidden). 46 | 47 | `debug: bool` - whether or not you want to see the debug info. Default: `True` (shown). 48 | 49 | `options: FirefoxOptions` - optional options for webdriver. Use `headless` if you want to hide browser. 50 | 51 | 52 | ## Usage 53 | ```python 54 | from opplast import Upload 55 | 56 | 57 | upload = Upload( 58 | # use r"" for paths, this will not give formatting errors e.g. "\n" 59 | r"C:/Users/USERNAME/AppData/Roaming/Mozilla/Firefox/Profiles/r4Nd0m.selenium", 60 | ) 61 | 62 | was_uploaded, video_id = upload.upload( 63 | r"C:/path/to/video.mp4", 64 | title="My YouTube Title", 65 | description="My YouTube Description", 66 | thumbnail=r"C:/path/to/thumbnail.jpg", 67 | tags=["these", "are", "my", "tags"], 68 | only_upload=False # If True will not set title, description or anything else. 69 | # Might be useful if you want to do it manually or by using the YouTube API. 70 | ) 71 | 72 | if was_uploaded: 73 | print(f"{video_id} has been uploaded to YouTube") 74 | 75 | upload.close() 76 | ``` 77 | 78 | ## License 79 | MIT License 80 | 81 | Copyright (c) 2021 [offish](https://offi.sh) 82 | 83 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 84 | 85 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 86 | 87 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 88 | -------------------------------------------------------------------------------- /examples/upload_multiple.py: -------------------------------------------------------------------------------- 1 | from opplast import Upload 2 | 3 | 4 | if __name__ == "__main__": 5 | videos = [ 6 | { 7 | "file": r"C:/path/to/video.mp4", 8 | "title": "My First YouTube Title", 9 | "description": "My First YouTube Description", 10 | "thumbnail": r"C:/path/to/thumbnail.jpg", 11 | "tags": ["these", "are", "my", "tags"], 12 | }, 13 | { 14 | "file": r"C:/path/to/video2.mp4", 15 | "title": "My Second YouTube Title", 16 | "description": "My Second YouTube Description", 17 | "thumbnail": r"C:/path/to/thumbnail2.jpg", 18 | "tags": ["these", "are", "my", "tags"], 19 | }, 20 | ] 21 | 22 | upload = Upload( 23 | # use r"" for paths, this will not give formatting errors e.g. "\n" 24 | r"C:/Users/USERNAME/AppData/Roaming/Mozilla/Firefox/Profiles/r4Nd0m.selenium", 25 | ) 26 | 27 | for video in videos: 28 | was_uploaded, video_id = upload.upload(**video) 29 | 30 | if was_uploaded: 31 | print(f"{video_id} has been uploaded to YouTube") 32 | 33 | upload.close() 34 | -------------------------------------------------------------------------------- /examples/upload_one.py: -------------------------------------------------------------------------------- 1 | from opplast import Upload 2 | 3 | 4 | if __name__ == "__main__": 5 | upload = Upload( 6 | # use r"" for paths, this will not give formatting errors e.g. "\n" 7 | r"C:/Users/USERNAME/AppData/Roaming/Mozilla/Firefox/Profiles/r4Nd0m.selenium", 8 | ) 9 | 10 | was_uploaded, video_id = upload.upload( 11 | r"C:/path/to/video.mp4", 12 | title="My YouTube Title", 13 | description="My YouTube Description", 14 | thumbnail=r"C:/path/to/thumbnail.jpg", 15 | tags=["these", "are", "my", "tags"], 16 | ) 17 | 18 | if was_uploaded: 19 | print(f"{video_id} has been uploaded to YouTube") 20 | 21 | upload.close() 22 | -------------------------------------------------------------------------------- /opplast/__init__.py: -------------------------------------------------------------------------------- 1 | __name__ = "opplast" 2 | __version__ = "1.0.14" 3 | 4 | from .exceptions import * 5 | from .constants import * 6 | from .logging import Log 7 | from .upload import Upload 8 | -------------------------------------------------------------------------------- /opplast/constants.py: -------------------------------------------------------------------------------- 1 | # URLS 2 | YOUTUBE_URL = "https://www.youtube.com" 3 | YOUTUBE_STUDIO_URL = "https://studio.youtube.com" 4 | YOUTUBE_UPLOAD_URL = "https://www.youtube.com/upload" 5 | 6 | # CONTAINERS 7 | TAGS_CONTAINER = '//*[@id="tags-container"]' 8 | ERROR_CONTAINER = '//*[@id="error-message"]' 9 | STATUS_CONTAINER = "/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[2]/div/div[1]/ytcp-video-upload-progress/span" 10 | VIDEO_URL_CONTAINER = "//span[@class='video-url-fadeable style-scope ytcp-video-info']" 11 | DESCRIPTION_CONTAINER = "/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[1]/ytcp-ve/ytcp-video-metadata-editor/div/ytcp-video-metadata-editor-basics/div[2]/ytcp-social-suggestions-textbox/ytcp-form-input-container/div[1]/div[2]/div/ytcp-social-suggestion-input" 12 | MORE_OPTIONS_CONTAINER = "/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[1]/ytcp-ve/ytcp-video-metadata-editor/div/div/ytcp-button/div" 13 | 14 | # COUNTERS 15 | TAGS_COUNTER = 500 16 | TITLE_COUNTER = 100 17 | DESCRIPTION_COUNTER = 5000 18 | 19 | # OTHER 20 | HREF = "href" 21 | TEXTBOX = "textbox" 22 | UPLOADED = "Uploading" 23 | TEXT_INPUT = "text-input" 24 | RADIO_LABEL = "radioLabel" 25 | DONE_BUTTON = "done-button" 26 | NEXT_BUTTON = "next-button" 27 | PUBLIC_BUTTON = "PUBLIC" 28 | INPUT_FILE_VIDEO = "//input[@type='file']" 29 | VIDEO_URL_ELEMENT = "//a[@class='style-scope ytcp-video-info']" 30 | UPLOAD_DIALOG_MODAL = "#dialog.ytcp-uploads-dialog" 31 | INPUT_FILE_THUMBNAIL = "//input[@accept='image/jpeg,image/png']" 32 | VIDEO_NOT_FOUND_ERROR = "Could not find video_id" 33 | NOT_MADE_FOR_KIDS_LABEL = "VIDEO_MADE_FOR_KIDS_NOT_MFK" 34 | -------------------------------------------------------------------------------- /opplast/exceptions.py: -------------------------------------------------------------------------------- 1 | class OpplastException(Exception): 2 | """General exception class for opplast""" 3 | 4 | 5 | class VideoIDError(OpplastException): 6 | """Error for when Video ID is not found""" 7 | 8 | 9 | class ExceedsCharactersAllowed(OpplastException): 10 | """Exception for when given string is too long""" 11 | -------------------------------------------------------------------------------- /opplast/logging.py: -------------------------------------------------------------------------------- 1 | class Log: 2 | def __init__(self, debug: bool = False): 3 | self.enable_debug = debug 4 | 5 | def debug(self, text: str) -> None: 6 | if self.enable_debug: 7 | text.encode(encoding="UTF-8", errors="ignore") 8 | print(f"DEBUG: {text}") 9 | -------------------------------------------------------------------------------- /opplast/upload.py: -------------------------------------------------------------------------------- 1 | from .constants import * 2 | from .logging import Log 3 | from .exceptions import * 4 | 5 | from pathlib import Path 6 | from typing import Tuple, Optional, Union 7 | from time import sleep 8 | 9 | from selenium.webdriver.common.keys import Keys 10 | from selenium import webdriver 11 | from selenium.webdriver import FirefoxOptions, FirefoxProfile 12 | 13 | 14 | def get_path(file_path: str) -> str: 15 | # no clue why, but this character gets added for me when running 16 | return str(Path(file_path)).replace("\u202a", "") 17 | 18 | 19 | class Upload: 20 | def __init__( 21 | self, 22 | profile: Union[str, FirefoxProfile], 23 | executable_path: str = "geckodriver", 24 | timeout: int = 3, 25 | headless: bool = True, 26 | debug: bool = True, 27 | options: FirefoxOptions = webdriver.FirefoxOptions(), 28 | ) -> None: 29 | if isinstance(profile, str): 30 | profile = webdriver.FirefoxProfile(profile) 31 | 32 | options.headless = headless 33 | 34 | self.driver = webdriver.Firefox( 35 | firefox_profile=profile, options=options, executable_path=executable_path 36 | ) 37 | self.timeout = timeout 38 | self.log = Log(debug) 39 | 40 | self.log.debug("Firefox is now running") 41 | 42 | def click(self, element): 43 | element.click() 44 | sleep(self.timeout) 45 | return element 46 | 47 | def send(self, element, text: str) -> None: 48 | element.clear() 49 | sleep(self.timeout) 50 | element.send_keys(text) 51 | sleep(self.timeout) 52 | 53 | def click_next(self, modal) -> None: 54 | modal.find_element_by_id(NEXT_BUTTON).click() 55 | sleep(self.timeout) 56 | 57 | def not_uploaded(self, modal) -> bool: 58 | return modal.find_element_by_xpath(STATUS_CONTAINER).text.find(UPLOADED) != -1 59 | 60 | def upload( 61 | self, 62 | file: str, 63 | title: str = "", 64 | description: str = "", 65 | thumbnail: str = "", 66 | tags: list = [], 67 | only_upload: bool = False, 68 | ) -> Tuple[bool, Optional[str]]: 69 | """Uploads a video to YouTube. 70 | Returns if the video was uploaded and the video id. 71 | """ 72 | if not file: 73 | raise FileNotFoundError(f'Could not find file with path: "{file}"') 74 | 75 | self.driver.get(YOUTUBE_UPLOAD_URL) 76 | sleep(self.timeout) 77 | 78 | self.log.debug(f'Trying to upload "{file}" to YouTube...') 79 | 80 | self.driver.find_element_by_xpath(INPUT_FILE_VIDEO).send_keys(get_path(file)) 81 | sleep(self.timeout) 82 | 83 | modal = self.driver.find_element_by_css_selector(UPLOAD_DIALOG_MODAL) 84 | self.log.debug("Found YouTube upload Dialog Modal") 85 | 86 | if only_upload: 87 | video_id = self.get_video_id(modal) 88 | 89 | while self.not_uploaded(modal): 90 | self.log.debug("Still uploading...") 91 | sleep(self.timeout) 92 | 93 | return True, video_id 94 | 95 | self.log.debug(f'Trying to set "{title}" as title...') 96 | 97 | # TITLE 98 | title_field = self.click(modal.find_element_by_id(TEXTBOX)) 99 | 100 | # get file name (default) title 101 | title = title if title else title_field.text 102 | 103 | if len(title) > TITLE_COUNTER: 104 | raise ExceedsCharactersAllowed( 105 | f"Title was not set due to exceeding the maximum allowed characters ({len(title)}/{TITLE_COUNTER})" 106 | ) 107 | 108 | # clearing out title which defaults to filename 109 | for _ in range(len(title_field.text) + 10): 110 | # more backspaces than needed just to be sure 111 | title_field.send_keys(Keys.BACKSPACE) 112 | sleep(0.1) 113 | 114 | self.send(title_field, title) 115 | 116 | if description: 117 | if len(description) > DESCRIPTION_COUNTER: 118 | raise ExceedsCharactersAllowed( 119 | f"Description was not set due to exceeding the maximum allowed characters ({len(description)}/{DESCRIPTION_COUNTER})" 120 | ) 121 | 122 | self.log.debug(f'Trying to set "{description}" as description...') 123 | container = modal.find_element_by_xpath(DESCRIPTION_CONTAINER) 124 | description_field = self.click(container.find_element_by_id(TEXTBOX)) 125 | 126 | self.send(description_field, description) 127 | 128 | if thumbnail: 129 | self.log.debug(f'Trying to set "{thumbnail}" as thumbnail...') 130 | modal.find_element_by_xpath(INPUT_FILE_THUMBNAIL).send_keys( 131 | get_path(thumbnail) 132 | ) 133 | sleep(self.timeout) 134 | 135 | self.log.debug('Trying to set video to "Not made for kids"...') 136 | kids_section = modal.find_element_by_name(NOT_MADE_FOR_KIDS_LABEL) 137 | kids_section.find_element_by_id(RADIO_LABEL).click() 138 | sleep(self.timeout) 139 | 140 | if tags: 141 | self.click(modal.find_element_by_xpath(MORE_OPTIONS_CONTAINER)) 142 | 143 | tags = ",".join(str(tag) for tag in tags) 144 | 145 | if len(tags) > TAGS_COUNTER: 146 | raise ExceedsCharactersAllowed( 147 | f"Tags were not set due to exceeding the maximum allowed characters ({len(tags)}/{TAGS_COUNTER})" 148 | ) 149 | 150 | self.log.debug(f'Trying to set "{tags}" as tags...') 151 | container = modal.find_element_by_xpath(TAGS_CONTAINER) 152 | tags_field = self.click(container.find_element_by_id(TEXT_INPUT)) 153 | self.send(tags_field, tags) 154 | 155 | # sometimes you have 4 tabs instead of 3 156 | # this handles both cases 157 | for _ in range(3): 158 | try: 159 | self.click_next(modal) 160 | except: 161 | pass 162 | 163 | self.log.debug("Trying to set video visibility to public...") 164 | public_main_button = modal.find_element_by_name(PUBLIC_BUTTON) 165 | public_main_button.find_element_by_id(RADIO_LABEL).click() 166 | video_id = self.get_video_id(modal) 167 | 168 | while self.not_uploaded(modal): 169 | self.log.debug("Still uploading...") 170 | sleep(1) 171 | 172 | done_button = modal.find_element_by_id(DONE_BUTTON) 173 | 174 | if done_button.get_attribute("aria-disabled") == "true": 175 | self.log.debug(self.driver.find_element_by_xpath(ERROR_CONTAINER).text) 176 | return False, None 177 | 178 | self.click(done_button) 179 | 180 | return True, video_id 181 | 182 | def get_video_id(self, modal) -> Optional[str]: 183 | video_id = None 184 | try: 185 | video_url_container = modal.find_element_by_xpath(VIDEO_URL_CONTAINER) 186 | video_url_element = video_url_container.find_element_by_xpath( 187 | VIDEO_URL_ELEMENT 188 | ) 189 | 190 | video_id = video_url_element.get_attribute(HREF).split("/")[-1] 191 | except: 192 | raise VideoIDError("Could not get video ID") 193 | 194 | return video_id 195 | 196 | def close(self): 197 | self.driver.quit() 198 | self.log.debug("Closed Firefox") 199 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | import re 3 | 4 | 5 | with open("README.md", "r") as f: 6 | long_description = f.read() 7 | 8 | 9 | with open("opplast/__init__.py") as f: 10 | version = re.search( 11 | r"""^__version__\s*=\s*['"]([^\'"]*)['"]""", f.read(), re.MULTILINE 12 | ).group(1) 13 | 14 | 15 | setup( 16 | name="opplast", 17 | version=version, 18 | author="offish", 19 | author_email="overutilization@gmail.com", 20 | description="Upload videos to YouTube using geckodriver, Firefox profiles and Selenium.", 21 | long_description=long_description, 22 | long_description_content_type="text/markdown", 23 | license="MIT", 24 | url="https://github.com/offish/opplast", 25 | download_url="https://github.com/offish/opplast/tarball/v" + version, 26 | packages=["opplast"], 27 | classifiers=[ 28 | "Programming Language :: Python :: 3", 29 | "License :: OSI Approved :: MIT License", 30 | "Operating System :: OS Independent", 31 | ], 32 | install_requires=["selenium<4"], 33 | python_requires=">=3.6", 34 | ) 35 | --------------------------------------------------------------------------------