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.

Revisions

  1. maxpoletaev revised this gist Nov 13, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion youtube_migrate.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    Tested with:
    - selenium 3.0
    - firefox 45.0
    - firefox 49.0
    - python 3.5
    1. Install selenium from pypi:
  2. maxpoletaev revised this gist Nov 13, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions youtube_migrate.py
    Original file line number Diff line number Diff line change
    @@ -24,11 +24,8 @@
    from collections import namedtuple
    from selenium import webdriver
    from xml.dom import minidom
    import urllib
    import pickle
    import time
    import re
    import os


    def main():
  3. maxpoletaev renamed this gist Nov 13, 2016. 1 changed file with 21 additions and 31 deletions.
    52 changes: 21 additions & 31 deletions transfer_subscriptions.py → youtube_migrate.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    YouTube account with Python and Selenium.
    Tested with:
    - selenium 2.52
    - selenium 3.0
    - firefox 45.0
    - python 3.5
    @@ -14,10 +14,7 @@
    and download your current subscriptions feed.
    Save file as subscription_manager.xml.
    3. Enter your credentials into constants
    GOOGLE_EMAIL and GOOGLE_PASSWORD
    4. Run script and go to drink coffee.
    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.
    @@ -27,34 +24,35 @@
    from collections import namedtuple
    from selenium import webdriver
    from xml.dom import minidom
    import urllib
    import pickle
    import time
    import re
    import os

    GOOGLE_EMAIL = '[email protected]'
    GOOGLE_PASSWORD = 'securepassword'

    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: ')

    def sign_in(driver, credentials):
    cookies_file = 'cookies.bin'
    if os.path.isfile(cookies_file):
    driver.get('http://www.youtube.com')
    cookies = pickle.load(open(cookies_file, 'rb'))
    for cookie in cookies:
    driver.add_cookie(cookie)
    driver.refresh()
    return
    driver.get('https://www.youtube.com')
    driver.find_element_by_css_selector('.signin-container button').click()
    time.sleep(1)

    email, password = credentials
    driver.get('https://www.google.com/accounts/Login?continue=http://www.youtube.com')
    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()

    pickle.dump(driver.get_cookies(), open(cookies_file, 'wb'))
    time.sleep(1)


    def load_subcribtions():
    @@ -82,22 +80,14 @@ def subscribe(driver, channel):
    time.sleep(1)

    button = driver.find_element_by_css_selector('.channel-header-subscription-button-container > button')
    skip = button.get_attribute('data-is-subscribed')
    is_subscribed = button.get_attribute('data-is-subscribed')

    if not skip:
    if not is_subscribed:
    button.click()

    print('{:.<50}{}'.format(channel.title, 'skip' if skip else ''))
    print('{:.<50}{}'.format(channel.title, 'skip' if is_subscribed else 'done'))
    time.sleep(1)


    def main():
    driver = webdriver.Firefox()
    sign_in(driver, (GOOGLE_EMAIL, GOOGLE_PASSWORD))
    for channel in load_subcribtions():
    subscribe(driver, channel)
    driver.close()


    if __name__ == '__main__':
    main()
  4. maxpoletaev renamed this gist Mar 13, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. maxpoletaev revised this gist Mar 13, 2016. No changes.
  6. maxpoletaev revised this gist Mar 13, 2016. 1 changed file with 11 additions and 13 deletions.
    24 changes: 11 additions & 13 deletions move_subscriptions.py
    Original file line number Diff line number Diff line change
    @@ -4,14 +4,14 @@
    Tested with:
    - selenium 2.52
    - firefox 42.0
    - firefox 45.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 subscribtions feed.
    and download your current subscriptions feed.
    Save file as subscription_manager.xml.
    3. Enter your credentials into constants
    @@ -20,7 +20,7 @@
    4. Run script and go to drink coffee.
    It will take some time.
    Note YouTube will temporary block you if you have more that 80 subscribtions.
    Note YouTube will temporary block you if you have more that 80 subscriptions.
    Just restart the script in a few hours.
    """

    @@ -36,7 +36,7 @@
    GOOGLE_PASSWORD = 'securepassword'


    def sign_in(driver):
    def sign_in(driver, credentials):
    cookies_file = 'cookies.bin'
    if os.path.isfile(cookies_file):
    driver.get('http://www.youtube.com')
    @@ -46,11 +46,12 @@ def sign_in(driver):
    driver.refresh()
    return

    email, password = credentials
    driver.get('https://www.google.com/accounts/Login?continue=http://www.youtube.com')
    driver.find_element_by_id('Email').send_keys(GOOGLE_EMAIL)
    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(GOOGLE_PASSWORD)
    driver.find_element_by_id('Passwd').send_keys(password)
    driver.find_element_by_id('signIn').click()

    pickle.dump(driver.get_cookies(), open(cookies_file, 'wb'))
    @@ -92,13 +93,10 @@ def subscribe(driver, channel):

    def main():
    driver = webdriver.Firefox()

    try:
    sign_in(driver)
    for channel in load_subcribtions():
    subscribe(driver, channel)
    except KeyboardInterrupt:
    driver.close()
    sign_in(driver, (GOOGLE_EMAIL, GOOGLE_PASSWORD))
    for channel in load_subcribtions():
    subscribe(driver, channel)
    driver.close()


    if __name__ == '__main__':
  7. maxpoletaev created this gist Mar 13, 2016.
    105 changes: 105 additions & 0 deletions move_subscriptions.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    """
    Automatic migration of subscriptions to another
    YouTube account with Python and Selenium.
    Tested with:
    - selenium 2.52
    - firefox 42.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 subscribtions feed.
    Save file as subscription_manager.xml.
    3. Enter your credentials into constants
    GOOGLE_EMAIL and GOOGLE_PASSWORD
    4. Run script and go to drink coffee.
    It will take some time.
    Note YouTube will temporary block you if you have more that 80 subscribtions.
    Just restart the script in a few hours.
    """

    from collections import namedtuple
    from selenium import webdriver
    from xml.dom import minidom
    import pickle
    import time
    import re
    import os

    GOOGLE_EMAIL = '[email protected]'
    GOOGLE_PASSWORD = 'securepassword'


    def sign_in(driver):
    cookies_file = 'cookies.bin'
    if os.path.isfile(cookies_file):
    driver.get('http://www.youtube.com')
    cookies = pickle.load(open(cookies_file, 'rb'))
    for cookie in cookies:
    driver.add_cookie(cookie)
    driver.refresh()
    return

    driver.get('https://www.google.com/accounts/Login?continue=http://www.youtube.com')
    driver.find_element_by_id('Email').send_keys(GOOGLE_EMAIL)
    driver.find_element_by_id('next').click()
    time.sleep(1)
    driver.find_element_by_id('Passwd').send_keys(GOOGLE_PASSWORD)
    driver.find_element_by_id('signIn').click()

    pickle.dump(driver.get_cookies(), open(cookies_file, 'wb'))


    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')
    skip = button.get_attribute('data-is-subscribed')

    if not skip:
    button.click()

    print('{:.<50}{}'.format(channel.title, 'skip' if skip else '✔'))
    time.sleep(1)


    def main():
    driver = webdriver.Firefox()

    try:
    sign_in(driver)
    for channel in load_subcribtions():
    subscribe(driver, channel)
    except KeyboardInterrupt:
    driver.close()


    if __name__ == '__main__':
    main()