mps-youtube / yewtube

yewtube, forked from mps-youtube , is a Terminal based YouTube player and downloader. No Youtube API key required.
GNU General Public License v3.0
8k stars 643 forks source link

How to Ignore non-youtube link when Youtube video data is fetched through a loop? #1171

Closed alive45 closed 1 year ago

alive45 commented 3 years ago

I'm fetching Youtube link (single video) data (such as Duration, Title) with pafy from a column (example: B4:B100). There are total 12 Youtube links and 1 non-youtube link. in the middle. pafy stops if it finds a non-youtube link. Is there any way of ignoring non-youtube links?

My Python Code -

It works perfectly if there are only Youtube links

from gspread.models import Worksheet
from oauth2client.service_account import ServiceAccountCredentials
import gspread
import pafy
import pprint
import youtube_dl

scope = ['https://www.googleapis.com/auth/spreadsheets.readonly','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name("C:\Python39\MyScripts\ptkou\ptkouapi.json",scope)
client = gspread.authorize(creds)

#defining sheet
sheet = client.open("Productivity Tracker").worksheet("Template")

#getting youtube links
SheetLinks = sheet.get('Links')
flat_SheetLinks =[]
for i in SheetLinks:
    for j in i:
        flat_SheetLinks.append(j)

#pafy implementation
flat_duration_out = []
for x in flat_SheetLinks:
    url =x
    video = pafy.new(url)
    title_out = video.title
    duration_out = video.duration
    author_out = video.author
    flat_duration_out.append(duration_out)

    #Print Values (Loop)
    print(url)
    print("Title : " + title_out)
    print("Duration : " + duration_out)
    print("Channel : " + author_out)

#updating cells with fetched data
cell_range = sheet.range('F4:F102')
cell_values = flat_duration_out

for y, val in enumerate(cell_values):
    cell_range[y].value = val
sheet.update_cells(cell_range)

In the Terminal of VS Code-

pafy stops if it sees a non-youtube link and the sheet does no get updated with the fetched data

PS C:\Python39\MyScripts> & C:/Python39/python.exe c:/Python39/MyScripts/ptkou/ptkou.py https://www.youtube.com/watch?v=ddf5Z0aQPzY Title : Python Gspread Tutorial + How To Use the Google Sheets API with Python Duration : 00:13:18 Channel : Frank Mularcik https://www.youtube.com/watch?v=JXlr21jv32Q Title : Most Frequently Asked Questions on Covid19 Vaccine | Dr. Vinoth Kumar | CARE Hospitals Duration : 00:02:59 Channel : CARE Hospitals https://www.youtube.com/watch?v=HFqf6aKdOC0 Title : Why doesn’t the Leaning Tower of Pisa fall over? - Alex Gendler Duration : 00:05:06 Channel : TED-Ed https://www.youtube.com/watch?v=kd2KEHvK-q8 Title : The Vanishing of Flight 370 Duration : 00:24:54 Channel : LEMMiNO https://www.youtube.com/watch?v=mLW6ZOKbq3Y Title : The Hijacking Of Flight 8969 | Mayday S2 EP3 | Wonder Duration : 00:51:41 Channel : Wonder https://www.youtube.com/watch?v=c16hr60PLT4 Title : Cutting Water in Half With a Superhydrophobic knife Duration : 00:00:59 Channel : Action Lab Shorts https://www.youtube.com/watch?v=x2w-tbUBgN4 Title : Darts World Championship 1990 Paul Lim Perfect Leg 9 Darter Duration : 00:02:04 Channel : BDODarts https://www.youtube.com/watch?v=tXh6Ev3oTqU Title : Now You See Me 2 "Hidden Card" Scene [HD] Jesse Eisenberg, Dave Franco, Woody Harrelson Duration : 00:04:44 Channel : We Got This Covered https://www.youtube.com/watch?v=xaiJPBfSjdw Title : Dog reacts to her Cancer test results. Duration : 00:00:44 Channel : Dani Girl https://www.youtube.com/watch?v=lSnXQL94R60 Title : तेरी प्यारी प्यारी दो अखिया dj mix song 2019 mix by parshant fandan Duration : 00:03:14 Channel : Prashant Fandan https://www.youtube.com/watch?v=Wrv_XQ92YQM Title : Should Pedophilia Be Accepted? Duration : 00:07:02 Channel : Answers in Genesis Traceback (most recent call last): File "c:\Python39\MyScripts\ptkou\ptkou.py", line 27, in <module> video = pafy.new(url) File "C:\Python39\lib\site-packages\pafy\pafy.py", line 124, in new return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts) File "C:\Python39\lib\site-packages\pafy\backend_youtube_dl.py", line 31, in __init__ super(YtdlPafy, self).__init__(*args, **kwargs) File "C:\Python39\lib\site-packages\pafy\backend_shared.py", line 62, in __init__ self.videoid = extract_video_id(video_url) File "C:\Python39\lib\site-packages\pafy\backend_shared.py", line 51, in extract_video_id raise ValueError(err % url) ValueError: Need 11 character video id or the URL of the video. Got https://spreadsheetpoint.com/regexmatch-function-google-sheets/

Your Environment

pafy (version 0.5.5) Windows 10

iamtalhaasghar commented 1 year ago

Before passing the link to pafy manually check if the link is of a youtube video or not by doing something like this

if 'youtube' in video_link:
       # pass the link to pafy