Share My Creation Youtube Video Lister

Discussion in 'Share Your Creations' started by Syd Wright, Apr 12, 2018.

  1. Syd Wright

    Syd Wright Active Member Licensed User

    This B4A code lists (nearly) all Youtube videos on a Youtube channel!
    The regular Youtube API only provides for the listing of 50 videos at a time with a maximum of 500 videos by using the "NextPageToken".

    The code below has a different approach because it lists all the videos that were uploaded in a particular period. A period of 30 days has been chosen, but this can be altered in the code.
    By concatenating periods, (nearly) all the videos can be listed that the user added over the past [X] years.
    In this example 36 periods of 30 days are the default, i.e. 3 years.

    If the search phrase is too generic then the Youtube ChannelID can be included in the search.

    The code sorts the derived videos list alphabetically and saves the list (that holds the Youtube video titles and Youtube ID URL's) in the default external directory with a filename that is based on the Youtube channel name.

    Please note that deriving a list will take between 1 to 10 minutes. Progress can be observed in the Log's.
    Kindly use this code with care, without over-demanding Youtube...
    You need to add the OKhttpUtils2 library and your own Google Youtube API key.

    Here is the sourcecode: Enjoy!
    Code:
    #Region Module Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
        
    #ApplicationLabel: Youtube Videos Lister
        
    #VersionCode: 1
        
    #VersionName:
        
    #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    'Activity module
    Sub Process_Globals
        
    Dim Job1 As HttpJob              'Use OKHttpUtils2 !
    End Sub

    Sub Globals
        
    Dim Button1 As Button
        
    Dim DateBeg1 As String
        
    Dim DateEnd1 As String
        
    Dim List1 As List
        
    Dim MyYouKey1 As String
        
    Dim PBar1 As ProgressBar
        
    Dim SourceText1 As String
        
    Dim SourceWeb1 As String
        
    Dim Tel1 As Long
        
    Dim Tel2 As Long
        
    Dim Tel3 As Long
        
    Dim Ticks1 As Long
        
    Dim Text1 As EditText
        
    Dim Text2 As EditText            'Insert Youtube User Name
        Dim Text3 As EditText            'Insert Youtube Channel ID
        Dim Text4 As EditText            'Insert Number of months to search back in time
        Dim YouMap1 As Map
        
    Dim YouChan1 As String
        
    Dim YouUser1 As String
        
    Dim YouPageNext1 As String       'Next page Token
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        PBar1.Initialize(
    "Pbar1")
        Text1.Initialize(
    "Text1")
        Text2.Initialize(
    "Text2")
        Text3.Initialize(
    "Text3")
        Text4.Initialize(
    "Text4")
        Button1.Initialize(
    "Button1")
        
    Activity.AddView(Button1,0,1%y,15.7%x,9%y)
        
    Activity.AddView(Text1,0,10%y,100%x,90%y)
        
    Activity.AddView(Text2,16%x,1%y,30.7%x,9%y)
        
    Activity.AddView(Text3,47%x,1%y,44.7%x,9%y)
        
    Activity.AddView(Text4,92%x,1%y,8%x,9%y)
        
    Activity.AddView(PBar1,2%x,92%y,96%x,3%y)
        Button1.Text = 
    "START"
        List1.Initialize
        YouMap1.Initialize
        MyYouKey1 = 
    "AIza.........."         'Use your own Google Youtube API key!
     
        Text2.text = 
    "TopPop"                'Remove these 3 lines; they are here just as an example
        Text3.text = "UCxjzlr0hdKWfIwK2lY0SvEg"
        Text4.Text = 
    "36"
    End Sub

    Sub Activity_Resume
     
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
     
    End Sub

    Sub Button1_Click
        Tel1 = 
    0
        Tel2 = 
    0
        Tel3 = Text4.text
        YouUser1 = Text2.text
        YouChan1 = Text3.text
     
        PBar1.Visible = 
    True
        PBar1.BringToFront
        Text1.Text = 
    ""
        
    If File.Exists(File.DirDefaultExternal,YouUser1 & "1.txt"Then File.Delete(File.DirDefaultExternal,YouUser1 & "1.dsh")
        Ticks1 = 
    DateTime.Now
        
    DateTime.DateFormat="yyyy-MM-dd"
        DateBeg1 = 
    DateTime.Date(Ticks1)
        DateBeg1 = DateBeg1 & 
    "T00:00:00Z"

        Ticks1 = Ticks1 - (
    DateTime.TicksPerDay * 30)    '30 days
        DateEnd1 = DateTime.Date(Ticks1)
        DateEnd1 = DateEnd1 & 
    "T00:00:00Z"

        
    If YouChan1 ="" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&publishedBefore=" & DateBeg1 & "&publishedAfter=" & DateEnd1 & "&maxResults=50&key=" & MyYouKey1     'Without using the ChannelID
        If YouChan1<>"" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&channelID=" & YouChan1 & "&publishedBefore=" & DateBeg1 & "&publishedAfter=" & DateEnd1 & "&maxResults=50&key=" & MyYouKey1
        Job1.Initialize(
    "Job1", Me)
        Job1.Download(SourceWeb1)
    End Sub

    Sub JobDone (Job As HttpJob)
        
    Dim n As Long
        
    Dim m As Long
        
    Dim VideoID1 As String
        
    Dim Titel1 As String
        
    Dim Dhulp1 As String
     
        
    If Job.Success = True Then
          
    Select Job.JobName
            
    Case "Job1"
              SourceText1 = Job.GetString2(
    "ISO-8859-1")
              n=
    0
              YouPageNext1=
    ""
              n= SourceText1.IndexOf2(
    "nextPageToken",n)+17
              m= SourceText1.IndexOf2(
    Chr(34),n)
              
    If m>n And n>17 Then YouPageNext1=SourceText1.SubString2(n,m)

              n=m+
    1
              Dhulp1  = 
    ""
              
    Do While n < SourceText1.Length - 100
                VideoID1= 
    ""
                Titel1  = 
    ""
                n= SourceText1.IndexOf2(
    "videoId" & Chr(34) & ":",n)
                
    If n=-1 Then Exit
                n=n+
    11
                m= SourceText1.IndexOf2(
    Chr(34),n)
                
    If m>n And n>11 Then VideoID1=SourceText1.SubString2(n,m).trim

                n=m+
    1
                n= SourceText1.IndexOf2(
    "title" & Chr(34) & ":",n)
                
    If n=-1 Then Exit
                n=n+
    9
                m= SourceText1.IndexOf2(
    Chr(34),n)
                
    If m>n And n>9 Then Titel1=SourceText1.SubString2(n,m).trim
                  
    Log(Titel1)
                Titel1 = Titel1.Replace(
    "• TopPop","")        'Do your own tidying-up here, dependent on the Channel's particulars
                Titel1 = Titel1.Replace("TOPPOP: ","")
                Titel1 = Titel1.Replace(
    " (Studio, TOTP)","")
                Titel1 = Titel1.Replace(
    " (Video, TOTP)","")
                Titel1 = CAPITALS1(Titel1)
                Dhulp1 = Dhulp1 & Titel1 & 
    Chr(9) & " = " & VideoID1 & CRLF
                YouMap1.Put(Titel1,VideoID1)
                Tel1 = Tel1 + 
    1
                n=m+
    1
              
    Loop
     
              
    If YouPageNext1="" Or Tel1=0 Then
                
    Log("============")
                
    Log("  ")
                Tel2 = Tel2 + 
    1
                
    If Tel2 <= Tel3 Then                              'Period of [36] months  (3 years)
                  PBar1.Progress = Tel2 * 100 / Tel3
                  
    DateTime.DateFormat="yyyy-MM-dd"
                  DateBeg1 = 
    DateTime.Date(Ticks1)
                  DateBeg1 = DateBeg1 & 
    "T00:00:00Z"

                  Ticks1 = Ticks1 - (
    DateTime.TicksPerDay * 30)   '30 days
                  DateEnd1 = DateTime.Date(Ticks1)
                  DateEnd1 = DateEnd1 & 
    "T00:00:00Z"

                  
    Log( DateBeg1 & " -  " & DateEnd1)
                  
    Log("   ")
                  
    If YouChan1 ="" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&publishedBefore=" & DateBeg1 & "&publishedAfter=" & DateEnd1 & "&maxResults=50&key=" & MyYouKey1            'Without using the ChannelID
                  If YouChan1<>"" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&channelID=" & YouChan1 & "&publishedBefore=" & DateBeg1 & "&publishedAfter=" & DateEnd1 & "&maxResults=50&key=" & MyYouKey1
                  Job1.Initialize(
    "Job1", Me)
                  Job1.Download(SourceWeb1)
                  
    Return                                           'Go back another 30 days
                End If

                
    Log("READY! Now Reading YouMap1...")
                List1.Clear
                
    For i = 0 To YouMap1.Size -1
                  List1.Add(YouMap1.GetKeyAt(i) & 
    Chr(9) & "https://www.youtube.com/watch?v=" & YouMap1.GetValueAt(i))
                
    Next
     
                
    Log("Sorting List1...")
                List1.Sort(
    True)
                PBar1.Visible = 
    False
     
                
    Log("Regenerating Text1")
                Dhulp1 = 
    ""
                
    For i=0 To List1.Size -1
                  Dhulp1 = Dhulp1 & List1.Get(i) & 
    Chr(13) & Chr(10)
                
    Next
     
                
    Log("Saving File")
                
    Dim TW As TextWriter
                TW.Initialize(
    File.OpenOutput(File.DirDefaultExternal, YouUser1 & "1.txt"False))
                TW.Write(Dhulp1)
                TW.Close

                
    Log("Showing Text1")
                Text1.Text = Dhulp1
     
                
    Log("Total number of Youtube videos = " & (List1.Size-1))
                
    Return
              
    End If
     
              
    Log("NextPageToken=" & YouPageNext1)
              Tel1=
    0
              
    If YouChan1 ="" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&maxResults=50&pageToken=" & YouPageNext1 & "&key=" & MyYouKey1
              
    If YouChan1<>"" Then SourceWeb1 = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" & YouUser1 & "&channelID=" & YouChan1 & "&maxResults=50&pageToken=" & YouPageNext1 & "&key=" & MyYouKey1
              Job1.Initialize(
    "Job1", Me)
              Job1.Download(SourceWeb1)
          
    End Select

        
    Else

          
    Select Job.JobName
            
    Case "Job1"
              
    Log("Downloading of: " & YouUser1 & " has failed")
          
    End Select
        
    End If
    End Sub

    'Starts every word with a Capital
    Sub CAPITALS1(inData As StringAs String
        
    Dim Dhulp1 As String
        
    Dim n As Long
     
        inData = inData.ToLowerCase & 
    "     "
        Dhulp1=inData.SubString2(
    0,1).ToUpperCase
        n=
    1
        
    Do While n < inData.Length-2
          
    If inData.SubString2(n,n+1) = " " Then
            Dhulp1 = Dhulp1 & 
    " " & inData.SubString2(n+1,n+2).ToUpperCase
            n=n+
    1
          
    Else
            Dhulp1 = Dhulp1 & inData.SubString2(n,n+
    1)
          
    End If
          n=n+
    1
        
    Loop
        
    Return Dhulp1.Trim
    End Sub
     
    Last edited: Apr 12, 2018
    Almora, inakigarm, scsjc and 2 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice