How to extract line by line from a txt and make each work with 2 async functions?

Mattia Fanti 356 Reputation points
2022-09-01T19:56:15.347+00:00

Hello,

I have a txt with titles of songs one title below to the other and I'm looking to extract every single line at the time and make it work with 2 async functions.
These functions are 2:

    1. using each line which correspond to a song title and search for it on youtube
    1. adding that id to a playlist

Both of the functions are working, I used the Youtube Data API v3 and this is the code

Imports System  
Imports System.IO  
Imports System.Reflection  
Imports System.Threading  
Imports System.Threading.Tasks  
Imports Google.Apis.Auth.OAuth2  
Imports Google.Apis.Services  
Imports Google.Apis.Upload  
Imports Google.Apis.Util.Store  
Imports Google.Apis.YouTube.v3  
Imports Google.Apis.YouTube.v3.Data  
Public Class Form1  
    Public id As String  
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  

        Try  
            Await RetrieveID()  
            Await PlaylistUpdates()  
        Catch ex As AggregateException  

            For Each inner In ex.InnerExceptions  
                MsgBox("Error: " & inner.Message)  
            Next  
        End Try  
    End Sub  
    Private Async Function RetrieveID() As Task  
        Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With {  
            .ApiKey = "my api key ",  
            .ApplicationName = Me.[GetType]().ToString()  
        })  

        Dim searchListRequest = youtubeService.Search.List("snippet")  
        searchListRequest.Q = TextBox1.Text  
        searchListRequest.MaxResults = 1  
        Dim searchListResponse = Await searchListRequest.ExecuteAsync()  

        For Each searchResult In searchListResponse.Items  

            id = searchResult.Id.VideoId  

        Next  

        MsgBox(id)  

    End Function  
    Private Async Function PlaylistUpdates() As Task  
        Dim credential As UserCredential  

        Using stream = New FileStream("client_secret_697184292163-n9c9etf5arv7iqkq6f84tlfqmjtfqi9g.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read)  
            credential = Await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, {YouTubeService.Scope.Youtube}, "user", CancellationToken.None, New FileDataStore(Me.[GetType]().ToString()))  
        End Using  

        Dim youtubeServiceo = New YouTubeService(New BaseClientService.Initializer() With {  
        .HttpClientInitializer = credential,  
        .ApplicationName = Me.[GetType]().ToString()  
    })  


        Dim newPlaylistItem = New PlaylistItem()  
        newPlaylistItem.Snippet = New PlaylistItemSnippet()  
        newPlaylistItem.Snippet.PlaylistId = "PL4_Dx88dpu7cEY_cBjTZFFM1tVKF5Plsx"  
        newPlaylistItem.Snippet.ResourceId = New ResourceId()  
        newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video"  
        newPlaylistItem.Snippet.ResourceId.VideoId = id  
        newPlaylistItem = Await youtubeServiceo.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync()  
        MsgBox(id + " è stato inserito nella playlist")  
    End Function  
End Class  

but now how can I make the process automatically extracting line by line from a txt? as you can see at the moment I'm inserting into textbox1 the query, but I would like to make it automatically since I have at least 200 queries ( song titles ).

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-09-01T21:21:19.55+00:00

    Hi
    Maybe something like this:

        Dim path As String = "C:\Users\lesha\Documents\Word Dictionaries\WordList.txt"  
        For Each line As String In IO.File.ReadAllLines(path)  
          ' do your code here using 'line' as the rquired text  
        Next  
      
    
    0 comments No comments