Alternatively and in order not to slow down too much the code I have implemented the following code:
I do believe that this might be the best version (assuming that I haven't done any mistakes
). I'm running this code next to the previous one.
Code:
di "https://www.youtube.com/watch?v=`video'" cap copy "https://www.youtube.com/watch?v=`video'" "`video'.txt", replace di _rc gen rc = _rc * If the copy fail, try again adding a sleep of 2sec. if _rc != 0 { sleep 2000 cap copy "https://www.youtube.com/watch?v=`video'" "`video'.txt", replace * Apply remaining commands only if it succeed at the second trial, if not, go to the next video. if _rc == 0 { import delimited using "`video'.txt", stringcols(_all) delimiter("þ") varn(noname) clear keep if strpos(v1, "watch-view-count") gen vues = substr(v1, strpos(v1,"watch-view-count"),.) drop v1 keep vues gen video = "`video'" gen time = c(current_time) gen date = c(current_date) append using "VA_`c(current_date)'" order time video vues save "VA_`c(current_date)'", replace } } * If the copy succeed, apply remaining commands. if rc ==0 { import delimited using "`video'.txt", stringcols(_all) delimiter("þ") varn(noname) clear keep if strpos(v1, "watch-view-count") gen vues = substr(v1, strpos(v1,"watch-view-count"),.) drop v1 keep vues gen video = "`video'" gen time = c(current_time) gen date = c(current_date) append using "VA_`c(current_date)'" order time video vues save "VA_`c(current_date)'", replace }

Comment