Skip to content

Instantly share code, notes, and snippets.

@maxpoletaev
Last active July 31, 2021 16:52
Show Gist options
  • Save maxpoletaev/0037fff3be1fbdb889bb to your computer and use it in GitHub Desktop.
Save maxpoletaev/0037fff3be1fbdb889bb 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.
Tested with:
- selenium 3.0
- firefox 49.0
- python 3.5
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
def main():
driver = webdriver.Firefox()
sign_in(driver)
for channel in load_subcribtions():
subscribe(driver, channel)
driver.close()
def sign_in(driver):
email, password = input('Email: '), input('Password: ')
driver.get('https://www.youtube.com')
driver.find_element_by_css_selector('.signin-container button').click()
time.sleep(1)
driver.find_element_by_id('Email').send_keys(email)
driver.find_element_by_id('next').click()
time.sleep(1)
driver.find_element_by_id('Passwd').send_keys(password)
driver.find_element_by_id('signIn').click()
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)
button = driver.find_element_by_css_selector('.channel-header-subscription-button-container > button')
is_subscribed = button.get_attribute('data-is-subscribed')
if not is_subscribed:
button.click()
print('{:.<50}{}'.format(channel.title, 'skip' if is_subscribed else 'done'))
time.sleep(1)
if __name__ == '__main__':
main()
@ChrisWithTheHat
Copy link

ChrisWithTheHat commented Aug 30, 2017

Hey. Really like this tool as both a stepping stone to learning code but it's also really useful. Had a couple of issues (I'm assuming just due to YouTube's layout and such changing) with the sign-in, but I got around it just by doing a manual login. Other than that this works great. Thanks a lot!

@thunkWaltz
Copy link

Thanks a lot. This is exactly what I was looking for.
As FaustsDaemon told earlier, you need to do manual login, which you could do by adding sleep before subscribe and login in the same process launched by the selenium.
Replace "driver.find_element_by_css_selector('.channel-header-subscription-button-container > button')" with "driver.find_element_by_id('subscribe-button')".
I ran it for chrome, had to provide path for the chromedriver as it was not in path.
driver = webdriver.Chrome(executable_path=r'/Applications/chromedriver')

@rvp1993
Copy link

rvp1993 commented Jan 28, 2018

Thanks a lot for this code! Works like a charm with the couple of changes mentioned previously.

@skhzhang
Copy link

Thanks for the code!

In addition, I had a lot of trouble with the script trying to subscribe me to channels that I was already subscribed to. So I figured out I could subtract the destination channel list from the source. Of course, that meant I had to also import my subscription list for my destination account.

Terminated accounts and VEVO accounts are something you want to consider. Apparently a lot of VEVO accounts apparently hide the subscribe button completely if you are subscribed(!). I also added some exception handling in case the script could not scroll the subscribe button into view.

Now, I just have to wait a couple hours for the rest of my subscriptions...

@thdung002
Copy link

Hey. Any new update? Still need this :(

@maxpoletaev
Copy link
Author

@thdung002 Sorry but wrote this for one-time use and have no plans for supporting each change youtube is making. Check out the forks—if this one doesn’t, some of the forks still might work.

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