0

I have combined this example in stem with pytube to measure the time it takes for me to download a youtube video via Tor.

Here's the code:

import io
import pycurl
import stem.process
from stem.util import term
import pickle
import socks  # SocksiPy module
import socket
import urllib
from pytube import YouTube
import pdb
import time
import string
import random

SOCKS_PORT = 9050
# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def query(url):

    try:
        yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')
        yt.set_filename(id_generator(6))
        video = yt.get('3gp', '144p')
        start = time.time()
        video.download('/tmp/') 
        end = time.time()
        time_ = end - start
        return time_

    except:
        return "Unable to reach %s" % url

def print_bootstrap_lines(line):
    if "Bootstrapped " in line:
        print(term.format(line, term.Color.BLUE))

def main():

    print(term.format("Starting Tor:\n", term.Attr.BOLD))

    tor_process = stem.process.launch_tor_with_config(
        config = {
            'SocksPort': str(SOCKS_PORT),

        },
        init_msg_handler = print_bootstrap_lines,
    )

    url = 'https://www.youtube.com/watch?v=5mkm22yO-bs'
    times = [0]*10

    for i in xrange(10):

        times[i] = query(url)

    tor_process.kill()  # stops tor

    with open('times_yout_jung.pickle', 'wb') as f:
        pickle.dump(times, f)

if __name__ == "__main__":
    main()

However, it gives the "Unable to reach url" error. And when I try normally route traffic via this code (to see whether pytube is working) then it works:

from pytube import YouTube
import pdb
import time     
import string
import random

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def main():


    pdb.set_trace()
    yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')
    yt.set_filename(id_generator(6))
    video = yt.get('3gp', '144p')
    start = time.time()
    video.download('/tmp/') 
    end = time.time()

    print("Time ", end - start)

if __name__ == "__main__":
    main()

I've tried to debug the code with pdb but I can't seem to figure out what could be going wrong. Could anyone please help?

klutt
  • 30,332
  • 17
  • 55
  • 95
QPTR
  • 1,620
  • 7
  • 26
  • 47
  • Is Tor starting up properly? The code works fine for me if I remove the part about launching the Tor process because I already have it running as a daemon. So there seems to be nothing wrong with it, unless Tor socks isn't running. – drew010 Apr 29 '16 at 04:33
  • @drew010 Yes it is. Printing all the bootstrap lines. However when I read back the pickle file where I've stored all the times, all of them have 'Unable to reach url' error. And the videos aren't there in the tmp folder as well. Do videos download on your end ? – QPTR Apr 29 '16 at 04:53
  • Additionally, when I use pdb, it seems to get stuck on `yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')` . The line after `yt.set_filename(id_generator(6))` doesn't seem to execute. And the loop returns back to `yt = YouTube('https://www.youtube.com/watch?v=5mkm22yO-bs')`. Even though the `set_filename` line does execute in normal un-Tor'red code. – QPTR Apr 29 '16 at 05:00
  • The videos downloaded, and I just changed the SOCKS_PORT to something else and it was able to start the Tor process and then download no problem. Unfortunately looking at the YouTube code, I don't see how to get at the exceptions possibly being thrown, my Python is not that great. There appears to be some logging going on behind the scenes that may help but I don't know where it's going. Can you run a regular Tor daemon and see if you can use it successfully rather than launch one from Stem? – drew010 Apr 29 '16 at 05:13
  • I just ran [this](https://stem.torproject.org/tutorials/to_russia_with_love.html#custom-path-selection) example. Its not working as well. Gives the error : `7, 'Failed to receive SOCKS5 connect request ack` Tor is opening fine though, on the other terminal. PS: Ignore the previous comment. – QPTR Apr 29 '16 at 05:28
  • Okay, its working fine now, with a different exit node. – QPTR Apr 29 '16 at 05:30
  • I tried to debug it a bit, and somewhere in between it gives the GeneralProxyError (while being in the Youtube module). From this it appears there may be something happening with socks.py module, since earlier it was giving me problems. A problem similar to [this](http://stackoverflow.com/questions/28790000/tor-stem-to-russia-with-love-connection-issues?rq=1) (the solution given in that link works). I downloaded socks.py from sourceforge then copy/pasted it inside the dist-packages in python's folder and replaced the older version. I wonder if this may be the reason. – QPTR Apr 29 '16 at 05:54

0 Answers0