philbot9 / youtube-comments-task

Scrape comments, including their replies, from a YouTube video.
ISC License
39 stars 4 forks source link

ERROR Button is missing attribute "data-token" #1

Closed nemanjaq closed 7 years ago

nemanjaq commented 7 years ago

Module doesn't work. Tried with simple example from readme.

philbot9 commented 7 years ago

Hi there, Thanks for opening an issue. The example works fine for me.

Could you check whether the video I used in the example is available in your country?https://www.youtube.com/watch?v=h_tkIpwbsxY

If it's not, that would explain why you're getting an error. You could also try with a different video to see if that works.

nemanjaq commented 7 years ago

Hi, thanks for reply.

Sorry I didn't mention it. Yes, video is working and i tried with another videos.

Code is basic

console.log('Testing....');

const fetchComments = require('youtube-comments-task')

fetchComments('h_tkIpwbsxY')
  .fork(e => console.error('ERROR', e),
        p => {
          console.log('comments', p.comments)
          console.log('nextPageToken', p.nextPageToken)
        })


On running

DEBUG=* node app.js

result is this

request sending request: 'https://www.youtube.com/watch?v=h_tkIpwbsxY' +0ms
  request sending request: { method: 'POST', json: true, url: 'https://www.youtube.com/watch_fragments_ajax?v=h_tkIpwbsxY&ctoken=EhYSC2hfdGtJcHdic3hZwAEAyAEA4AEBGAY%3D&frags=comments&tr=time&distiller=1&spf=load', form: { session_token: 'QUFFLUhqbUxnZmhmVFMxaVAycERBODlGSHFkUDktalhWd3xBQ3Jtc0tuamVGRGMybHQwOFh3Y2IxcjUwWFZwM1VLTlZHdGprTmI4WnJ1RHZUQl9YcVYtRi1HMllqaWtfUW9XSHhZdGtPVXpUUFpYckJCeHVlaUVJb0xPNkE3Tm9IWnM3UTR4c3R2MDdPV0NyZ1FEd0pydGRWNjhmalVCanFnN25CNk16MnE5YkZvNjZKZUNPbjlFaUdfbHJPYjlQM2d5Nnc=', page_token: undefined } } +599ms
ERROR Button is missing attribute "data-token"



PS. I also tried youtube-comment-api, which worked. Is there a reason you used different approach here ( different request urls etc. ) ?

philbot9 commented 7 years ago

Interesting. Thanks for posting the code, always helps.

The youtube-comment-api module is a lot older than this one. When I wrote this new module YouTube had switched to different URLs and slightly different mechanisms for the comments. However, the old mechanisms that the youtube-comment-api uses still work.

I've tried your code, with a fresh npm install youtube-comment-tasks and it is working for me (I know, not much of a consolation). At this point, I am unclear as to why you're getting that error since I cannot reproduce it. Because you mentioned the old system still works, my best guess is that the new YouTube comment system is unavailable in your location.

Am I correct in assuming you're not in North America? Would you mind sharing what country you're trying this from? I'd like to see if my guess is correct using a VPN or something.

I would recommend you use youtube-comment-api for now. If you'd like to use Tasks you could use the Futurize Promise Wrapper (I haven't confirmed whether that's working though).

nemanjaq commented 7 years ago

Well, since it's working for you, problem could be our Southern European Gremlins.

Thanks anyway.

philbot9 commented 7 years ago

Yeah, the good old Localization-Gremlin strikes again. ;)

Turns out I wasn't too far off with my guess, albeit a much simpler problem. When requesting a comment page from any country, the results are translated to the language of that country. The problem was with this CSS selector: button[data-menu_name="newest-first"].

The data-menu_name attribute changes with different languages, which means the selector no longer works. Fortunately, when sending requests with the Accept-Language=en HTTP header YouTube sends English content, no matter where you are.

Commits 640040e049d9a76b590d4f324c025244bd986184 and 053b64e1207397d9378628c160a6b36b3501f5e7 should fix this problem. I also pushed the fix to npm.

Thanks for your help in sorting this out.