How do I show a YouTuber's subscriber count in Visual Basic 2012? - vb.net

I want to have a form that shows a youtuber's subscriber count. I already installed the API and I got this code:
Imports Google.GData.YouTube
Imports Google.GData.Client
Imports Google.GData.Extensions
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim subshelper As New Service
Dim feedUrl As String = "http://gdata.youtube.com/feeds/api/users/SkyDoesMinecraft"
Dim profile As ProfileEntry = subshelper.Get(feedUrl)
Dim subscount As Integer = profile.Statistics.SubscriberCount
Label1.Text = subscount
End Sub
End Class
I got this error though:
An unhandled exception of type 'Google.GData.Client.GDataRequestException' occurred in Google.GData.Client.dll
Additional information: Execution of request failed:
https://gdata.youtube.com/feeds/api/users/SkyDoesMinecraft
Can anyone help me here? Thank you!

If the only thing you care about is using the YouTube Data API to get the subscriber count for that one channel, then your best bet is probably just to make a generic HTTP request (without using any of the GData libraries) for
https://gdata.youtube.com/feeds/api/users/SkyDoesMinecraft?v=2&alt=json
and then using a JSON-parsing library to read the value of yt$statistics -> subscriberCount

Related

VB.net Asynchronous call to Url with no response needed

I have a VB.Net page that needs to submit data to a url after a user clicks a button. I don't need any data back from the url, I just need to pass parameters to it and allow the user to continue to the next step without waiting for the url to do it's thing.
I've seen some similar posts for c# using UploadStringTaskAsync, but haven't been able to find a corresponding method for VB.net.
https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstringtaskasync?view=net-6.0
I believe I can call the Async method from my existing nonasync methods as I don't need the response back. However, if there is a more elegant approach to do this please let me know.
Update with current code attempting to use thread:
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim thread As New Thread(AddressOf testSub)
thread.Start()
End If
End Sub
Sub testSub
Using WC As New WebClient WC.UploadString("https://someurl.com?parameter1=testing&parameter2=anothertest", "SOMEDATA")
End Using
End Sub
This runs but unfortunately does not appear to handle any of the parameters. When I post the url directly in the browser it runs as expected. I don't need to send any data other than the querystring as well so I'm not sure if that's breaking the uploadstring. However, when I run it via debugger I don't see any errors so long as I populate that string for the data with a value.
I might be misunderstanding though when the await call is needed. While I don't need any data back, the external url can take up to 5 minutes to process. I'm wondering if it's taking too long and timing out after that thread is started.
You could run it in its own thread.
Imports System.Net
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
' Create a thread to run this in the background
Dim t As New Thread(
Sub()
Using WC As New WebClient
WC.UploadString("https://YOURURL", "YOURDATA")
End Using
End Sub
)
' Start the thread
t.Start()
End Sub
End Class

Reading Applications and services log

I want to read a custom event log which is stored under Applications and services log section in Windows Eventlog.
Unfortunately when calling the Log according to its naming properties I receive an error message that the log cannot be found.
Ulitmately I try read event details from events with a specific ID but first I need to able to access the log.
This is the code that I have so far:
Imports System
Imports System.Diagnostics.Eventing.Reader
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim query As New EventLog("Logname as per Properties", System.Environment.MachineName)
Dim elEventEntry As System.Diagnostics.EventLogEntry
Dim nmbr As Integer = query.Entries.Count
MsgBox(nmbr)
End Sub
End Class
This is the structure in the eventlog (I want to read the blue highlighted part)
Anybody any idea how to determine the correct log name?
Thx & BR
Daniel
For many of the event logs, you need to use an EventLogQuery.
As an example, if you wanted to query the "Setup" event log to count the number of entries with an EventID of 1, you could do this:
Imports System.Diagnostics.Eventing.Reader
Module Module1
Sub Main()
Dim query As New EventLogQuery("Setup", PathType.LogName, "*[System/EventID=1]")
Dim nEvents = 0
Using logReader = New EventLogReader(query)
Dim eventInstance As EventRecord = logReader.ReadEvent()
While Not eventInstance Is Nothing
nEvents += 1
eventInstance = logReader.ReadEvent()
End While
End Using
Console.WriteLine(nEvents)
Console.ReadLine()
End Sub
End Module
You can see the names of the items to query by looking at the XML for an event in Windows Event Viewer.
The Using construct makes sure that the EventLogReader is properly disposed of after it's been used.
Further information: How to: Access and Read Event Information (from Microsoft).

How to find Roku IP + Port on network using SSDP search in VB.NET

I am trying to find my Roku TV on my network and apparently it needs some SSDP discovery based on Roku API help, however, I am unable to search for my device with any of the Nuget libraries.
I came across ssdpradar and was able to install the Nuget package for Visual Studio (VB.NET) through Visual Studio 2017 community release. However, I am not able to find any documentation on how to use it.
Any advice would be helpful.
Solution:
I found a solution but not with ssdpradar and rather RSSDP. After you add the nugget in your project you can use the following line of code to get all devices and then find the Roku location (ip+port) from that list.
Imports Rssdp
For Each founddevice As DiscoveredSsdpDevice In founddevices.Result
If founddevice.Usn.Contains("roku:ecp") Then
Rokulocation = founddevice.DescriptionLocation.ToString()
Exit For
End If
Next
I was able to successfully use a library called RokuDotNet recently. It's written in C# but you could load it as a project in your solution and reference it from VB.NET.
This is roughly the way I used it:
Imports RokuDotNet.Client
Public Class Form1
Private _discoveryClient As RokuDeviceDiscoveryClient
Public Sub New()
_discoveryClient = New RokuDeviceDiscoveryClient
AddHandler _discoveryClient.DeviceDiscovered, AddressOf DiscoveryHandler
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
_discoveryClient.DiscoverDevicesAsync()
End Sub
Private Async Sub DiscoveryHandler(sender As Object, e As DeviceDiscoveredEventArgs)
If InvokeRequired Then
BeginInvoke(New Action(Sub() DiscoveryHandler(sender, e)))
Return
End If
' Get the display name for the device (if the user entered one when setting it up)
Dim deviceInfo = Await e.Device.Query.GetDeviceInfoAsync
Dim name = deviceInfo.UserDeviceName
If String.IsNullOrEmpty(name) Then
name = deviceInfo.ModelName
End If
AddDevice(e.Device, name)
End Sub
Private Sub AddDevice(device As RokuDevice, name As String)
' Your code here
End Sub
End Class
You might want to add a try/catch around the await in that async function so it can show an error if there's a network problem.

Getting an access denied error when trying to pause/resume an outgoing message queue

Imports System.Messaging
Imports System.Collections
Imports MSMQ
Imports System.IO
Imports System
Imports System.Messaging.MessageQueue
Imports System.Runtime.InteropServices
Public Class PauseOutMessages
'Declare everything to be in the scope of all methods.
Dim mgmt As New MSMQManagement
Dim outqmgmt As MSMQOutgoingQueueManagement
Dim q As New MSMQApplication
Dim outgoingQueues As New ArrayList
Dim myQueue As New MessageQueue("FormatName:DIRECT=OS:myMachine\Private$\myQueue", QueueAccessMode.ReceiveAndAdmin)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
Private Sub Pause_Click(sender As Object, e As EventArgs) Handles Pause.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Pause()
Next
End Sub
Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
For Each queuePath In outgoingQueues
mgmt.Init(FormatName:=queuePath)
outqmgmt = mgmt.Resume()
Next
End Sub
Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
myQueue.Send("Test")
For Each queue In q.ActiveQueues
If queue.IndexOf("DIRECT=") >= 0 Then
outgoingQueues.Add(queue)
End If
Next
End Sub
End Class
Here is the code I am using, by sending the test message to a non-existing path it gets stuck in the outgoing queue where I want to be able to call MSMQOutgoingQueueManagement.Pause or .Resume to be able to start and stop all outgoing queues.
However I keep getting an error on either mgmt.Pause() or mgmt.Resume() saying Access is denied. I can't seem to find a way to get on to the properties of outgoing queues to be able to adjust security settings. Any help would be greatly appreciated!
SOLVED!
Turns out I just needed to start up visual studio as an administrator and then it worked.

VB.NET DownloadDataAsync:

I am having the worst trouble getting around a bug, and am hoping that I can get some advice on this site. In short, I am trying to make an asynchronous web service call from my VB.NET application. But my "client_DownloadDataCompleted" callback is NEVER being called when the download is complete.
Here is my complete code:
Public Sub BeginAsyncDownload(ByVal Url As String)
Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
Dim client As WebClient = New WebClient()
'client_DownloadDataCompleted method gets called when the download completes.
AddHandler client.DownloadDataCompleted, AddressOf client_DownloadDataCompleted
Dim uri As Uri = New Uri(Url)
Downloading = True 'Class variable defined elsewhere
client.DownloadDataAsync(uri, waiter)
End Sub
Private Sub client_DownloadDataCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
MessageBox.Show("Download Completed")
Downloading = False
Debug.Print("Downloaded")
End Sub
Again, the client_DownloadDataCompleted method is never being called. I have also tried using the method:
Private Sub client_DownloadDataCompleted(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)
With no luck. What I really need is that "Downloading" variable to be switched off when the download is complete.
Thanks in advance!
Brett
The client (Webclient) should be declared outside the BeginAsyncDownload subroutine, so it has a form/class level visibility. Please refer to the following code:
Public Class Form1
Dim client as New WebClient()
Private Sub BeginAsyncDownload(ByVal Url As String)
AddHandler client.DownloadDataCompleted, AddressOf client_DownloadDataCompleted
Dim uri As Uri = New Uri(Url)
Downloading = True 'Class variable defined elsewhere
client.DownloadDataAsync(uri, waiter)
End Sub
Private Sub client_DownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs)
MessageBox.Show("Download Completed")
Downloading = False
Debug.Print("Downloaded")
End Sub
This is a tough one. I spent a little time on this and wasn't able to figure out why it wasn't getting called, sorry.
If you aren't able to get this to work, I have some code on CodePlex that includes a WebHelper class that might help you. I tried to make it as easy to use as WebClient but with all the power of HttpWebRequest.
The project is called BizArk. I wrote it just as a repository of code for myself. Feel free to just use the bits you want, I don't have any particular interest in how the code is used (as long as it's not used for evil :).
In what context are you invoking the webclient? WebClient will pick up your SynchronizationContext.Current and post its completion callback to it.
If you are using WinForms and your UI thread is blocked it will never be able to process your callback.