question

MattiaFanti-4051 avatar image
0 Votes"
MattiaFanti-4051 asked JiachenLiMFST-9349 commented

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

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
- 2) 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

dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MattiaFanti-4051 ,
As LesHay-2099 sail, using IO.File.ReadAllLines could extract line by line from a txt.
It returns String[], then using For Each on the String[].
May I know if your problem has been solved or not?

0 Votes 0 ·

1 Answer

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.