Skip to content

Instantly share code, notes, and snippets.

@yusanshi
Forked from maxpoletaev/youtube_migrate.py
Last active November 16, 2020 15:55
Show Gist options
  • Save yusanshi/1b23fae534f82bf482ecfb68b5aa9677 to your computer and use it in GitHub Desktop.
Save yusanshi/1b23fae534f82bf482ecfb68b5aa9677 to your computer and use it in GitHub Desktop.
Transfer YouTube subscriptions to another account
"""
Automatic migration of subscriptions to another
YouTube account with Python and Selenium.
1. Install selenium from pypi:
$ pip install selenium
2. Go to the down of page https://www.youtube.com/subscription_manager
and download your current subscriptions feed.
Save file as subscription_manager.xml.
4. Run script, enter your credentials and go to drink coffee.
It will take some time.
Note YouTube will temporary block you if you have more that 80 subscriptions.
Just restart the script in a few hours.
"""
from collections import namedtuple
from selenium import webdriver
from xml.dom import minidom
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException
def main():
driver = webdriver.Chrome(ChromeDriverManager().install())
sign_in(driver)
for channel in load_subcribtions():
subscribe(driver, channel)
driver.close()
def sign_in(driver):
driver.get('https://www.youtube.com')
input('Sign in manually. After you signed, press enter.')
time.sleep(1)
def load_subcribtions():
xmldoc = minidom.parse('subscription_manager.xml')
itemlist = xmldoc.getElementsByTagName('outline')
channel_id_regexp = re.compile('channel_id=(.*)$')
Channel = namedtuple('Channel', ['id', 'title'])
subscriptions = []
for item in itemlist:
try:
feed_url = item.attributes['xmlUrl'].value
channel = Channel(id=channel_id_regexp.findall(feed_url)[0],
title=item.attributes['title'].value)
subscriptions.append(channel)
except KeyError:
pass
return subscriptions
def subscribe(driver, channel):
channel_url = 'https://www.youtube.com/channel/' + channel.id
driver.get(channel_url)
time.sleep(1)
try:
button = driver.find_element_by_css_selector(
'ytd-subscribe-button-renderer > paper-button')
is_subscribed = button.get_attribute('subscribed') is not None
if not is_subscribed:
button.click()
print('{:.<50}{}'.format(channel.title,
'skip' if is_subscribed else 'done'))
except NoSuchElementException:
print('{:.<50}{}, check {} manually.'.format(channel.title, 'error',
channel.id))
time.sleep(1)
if __name__ == '__main__':
main()
@cloverpi
Copy link

Works very well but you can't manually login to google with chrome drivers like selenium, puppeteer, etc any longer. To make this work, you need to import your youtube cookie.

Goto:
https://chrome.google.com/webstore/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm?hl=en

install in both your regular browser, and this webdriver. Export from your browser, import into webdriver, press Ctrl-F5 from the webdriver and you'll be signed in and this script can continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment