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:
-
- using each line which correspond to a song title and search for it on youtube
-
- 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
CodePudding user response:
note: I'm not familiar with VB's syntax, just with how .NET works.
Looks like you just need file IO here. This tutorial seems to go over it well enough. Basically, you'll want to create an IO.StreamReader
and open your file. Then loop over the contents of the StreamReader with ReadLine
until you can't anymore, and pass the values to your async functions.
Here's a console app that'll demonstrate the file IO:
Module Module1
Sub Main()
Dim srdFile As IO.StreamReader
Dim strLine As String
srdFile = New IO.StreamReader("my_file.txt")
Do Until srdFile.Peek = -1
strLine = srdFile.ReadLine()
Console.WriteLine(strLine)
Loop
Dim input = Console.ReadLine()
End Sub
End Module
Edit: Here's how I'd integrate it with the rest of your class:
Note how I'm not await
ing on each of the async functions. Since your code doesn't rely on their output values, you can just proceed onwards and let the async do its thing. This is especially important with Button1_Click
, as it's a UI event that needs to end as soon as possible.
Public Class Form1
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RunExtract()
End Sub
Private Async Sub RunExtract()
Dim srdFile As IO.StreamReader
Dim strLine As String
srdFile = New IO.StreamReader(Textbox1.Text)
Do Until srdFile.Peek = -1
strLine = srdFile.ReadLine()
RetrieveID(strLine)
PlaylistUpdates(strLine)
End Sub
Private Async Function RetrieveID(id as String) As Task
'Do something with id
End Function
Private Async Function PlaylistUpdates(id as String) As Task
' Do something with id
End Function
End Class