我有一個帶有歌曲標題的 txt,其中一個標題在另一個標題之下,我希望同時提取每一行并使其與 2 個異步函式一起使用。這些功能是2:
-
- 使用與歌曲標題相對應的每一行并在 youtube 上搜索它
-
- 將該 ID 添加到播放串列
這兩個功能都在作業,我使用了 Youtube Data API v3,這是代碼
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
但是現在我怎樣才能讓這個程序自動從txt中逐行提取呢?正如您目前所看到的,我正在將查詢插入到 textbox1 中,但我想自動進行,因為我至少有 200 個查詢(歌曲標題)。
謝謝
uj5u.com熱心網友回復:
注意:我不熟悉 VB 的語法,只了解 .NET 的作業原理。
看起來你只需要這里的檔案 IO。本教程似乎已經足夠好。基本上,您需要創建一個IO.StreamReader
并打開您的檔案。然后回圈遍歷 StreamReader 的內容,ReadLine
直到你不能再使用,并將值傳遞給你的異步函式。
這是一個演示檔案 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
編輯:這是我將它與您的其他班級整合的方式:
請注意我不是await
在每個異步函式上。由于您的代碼不依賴于它們的輸出值,您可以繼續前進并讓異步完成它的作業。這對于 尤其重要Button1_Click
,因為它是一個需要盡快結束的 UI 事件。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503726.html
上一篇:如何在閱讀后從txt中洗掉一行