Closed ghost closed 7 years ago
This worked for me on 3.6.1. I ran 2to3 (Python's 2.7 converter to 3.x) and below is what I needed to change. `xis-MacBook-Air:YT xi$ 2to3 downloader.py RefactoringTool: Skipping optional fixer: buffer RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: set_literal RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: Refactored downloader.py --- downloader.py (original) +++ downloader.py (refactored) @@ -139,19 +139,19 @@ parser.print_usage() raise ValueError('you need to specify a Youtube ID and an output filename')
print 'Downloading Youtube comments for video:', youtube_id
print('Downloading Youtube comments for video:', youtube_id) count = 0 with open(output, 'wb') as fp: for comment in download_comments(youtube_id):
print >> fp, json.dumps(comment)
print(json.dumps(comment), file=fp) count += 1 sys.stdout.write('Downloaded %d comment(s)\r' % count) sys.stdout.flush()
print '\nDone!'
print('\nDone!')
except Exception, e:
print 'Error:', str(e)
except Exception as e:
print('Error:', str(e)) sys.exit(1)`
You will get an error like this "Error: a bytes-like object is required, not 'str'".
This just means you need to remove the 'b' in 'wb'. Line: 144 with open(output, 'w') as fp:
Then I went into terminal (I'm on a OSX) and ran as my example this. python downloader.py -y fOXKGQw6Ing -o phily_output.json
At the time of writing this it grabbed all 7,000 comments.
Thanks! & Happy Coding!
@hairypaulsack You're right only Python 2.7 was supported,
@d0tN3t Using your suggestions, I've just added Python 3 support. Thanks!
I am in no way a python expert, nor have I ever written my own script, but you claim that your script is 2.7+ capable and I don't think that's the case. I'm sure that doesn't only mean 2.x variants, but could be wrong.
Believed to have confirmed my guess as:
EDIT: works great on 2.7! Thanks!
Also I tried updating print to 3.x syntax and it appears there is more than print being the problem. Is it possible to make a script that reads user python version and acts accordingly?