VB.NET Get the V variable from a youtube url - vb.net

I am writing a program to play videos from the internet in it.
for the user to get the video they have to paste a url in E.G. https://www.youtube.com/watch?v=5xniR1GN69U that url works but this one https://www.youtube.com/watch?feature=c4-overview&list=UUOYWgypDktXdb-HfZnSMK6A&v=5xniR1GN69U dosnt
this is my currunt code
how do i get it to just the v varibale from the url

Your querystring looks like "?v=5xniR1GN69U" the first time and "?feature=c4-overview&list=UUOYWgypDktXdb-HfZnSMK6A&v=5xniR1GN69U" the second time. You could have figured that out by yourself by simply debugging and checking your variables, since it doesn't even reach the code in the if block... Making a screenshot doesn't help people to figure out what's wrong with your code.
You might want to use Regex to parse the id Regex.Match(str, "v=(\w+)").Groups(1).Value

Just request the parameter v?
Request("v")
or
Request.Querystring("v")
EDIT
If in winforms. You can apply as suggested by #Markus
Try it like this
Dim querystring As String = url.Query
Dim myMatches As MatchCollection
Dim MyRegEx As New Regex("v=+(\w+)")
myMatches = MyRegEx.Matches(querystring)
For Each Row In myMatches
AxShockFlashMovie1.Movie = "https://www.youtube.com/v/" & Row.ToString() & ""
Next
You will also need to include
Imports System.Text.RegularExpressions

I've used HttpUtility.ParseQueryString in the past for something similar. Don't forget that youtube also offer tiny version of the urls with http://youtu.be/???

Related

Remove parts of a string in vb.net

Im using the FolderBrowserDialog to pick a path.
It will return for ex. this= C:\Mypath1\Mypath2\DOCS
I would like to remove everything but DOCS.
If i use VBA i would use a InStrRev combined with a left. But now in in VB.net and im not sure how to achieve this, im pretty sure there is something better than my old VBA way?
Anyone there that could help, google failed me.
Try this:
IO.Path.GetFileName("C:\Mypath1\Mypath2\DOCS")
Which returns DOCS.
I am not sure what you want to do. But maybe this can help you get the last part of a string
Dim fullPath As String = " C:\Mypath1\Mypath2\DOCS"
Dim Parts As String() = fullPath.Split("\")
Dim lastPart As String = Parts(Parts.Length - 1)

How to extract URLs from HTML source in vb.net

My question is: I have a program that fetches the whole source code of a specified URL. The source code will be saved in a variable.
Part of the source code looks like this:
"thumbnail_src":"https:\/\/scontent-fra3-1.blablabla.com\/t51.2885-15\/s640x640\/sh0.08\/e35\/1234567_984778981596410_1107218704_n.jpg","is_video":false,
The code is has quite a bunch of those URLs. I want my code to look for the part "thumbnail_src":" as a marker for beginning the extraction process and stop the extraction at ","is_video":
This should be obviously done in a loop until all URLs are being extracted and saved into a listing variable.
How can I achieve that?
I am trying to get that Regexp into my sourcecode. The one that codexer wrote, which is correct but I am getting eerrors in visual basic net.
Dim regex As Regex = New Regex("thumbnail_src""": """(.*)""","""is_video")
Dim match As Match = regex.Match(sourceString)
If match.Success Then
Console.WriteLine(match.Value)
End If
I tried it this way..and also that way:
Dim regex As Regex = New Regex("thumbnail_src":"(.*)","is_video")
Something is wrong the way I am entering the regex code.
Here is the correct one I need to implement:
https://regex101.com/r/hK0xH8/4
thumbnail_src":"(.*)","is_video
In light of your recent edit I am going to redo this answer.
Since it looks like everything is coming in on one line of text here is how I would handle it.
Dim LargetxtLine as String = TheVeryLargylineofText
Dim CommaSplit as String() = LargetxtLine.split(","c)
Dim URLList as New List(of String)
Dim RG as New Regex("\"":\""(.*)\""")
For Each str as String in CommaSplit
If str.contains("thumbnail_src") Then
URLList.Add(RG.Match(str).value)
End If
Next
This will break up the long line of text into managable chunks and then it uses regex to add it to a list of URL's (URLList)
From there you can do just about anything with a list(of String).
There is another way of doing it without splitting on the ,'s
if you use this Regex
"thumbnail_src\"":\""(.*?)\"",\""is_video"
Adding the "?" in there turns it into a greedy statement meaning that it will stop at the first occurance.
After that you could create a URLList like this
DIM RG as New Regex("thumbnail_src\"":\""(.*?)\"",\""is_video")
Dim URLList as MatchCollection = RG.Matches(reallybigString)
It's really personal preference

Check if cookie exists #2

Trying to start with VB.Net and having problem
We have such a Class in C#.
How to check if cookie exist? Exactly, how to set input object "Cookie" for a function CookieExist? How to create it using GeckoFx?
For example, how to find cookie with the name "Test2"?
Please, make an example.
I'm going crazy with VB.Net
P.S. I rarely write to forums if can find info on my own but VB.Net have too few examples in the net. ;/
I think the code below will help you:
Dim cookieValue As String = "Value of cookie"
Dim cookieName As String = "CookieName"
Dim realCookie As HttpCookie = HttpContext.Current.Request.Cookies.Get(cookieName)
If IsNothing(realCookie) Then
HttpContext.Current.Request.Cookies.Add(New HttpCookie(cookieName, cookieValue))
End If

New WebProxy to read from list box VB.net

I am using http web requests and want it to use different proxies.
This is my starting point:
Dim myproxy As New WebProxy("http://1.1.1.1:80")
I would like to populate the address section using an item from ListBox.
VB does not let me do it, because I am trying to convert a string to an address data type.
Is there a way?
If you managed to solve your problem with the C# code you posted, VB could look like this:
Dim prx As String = "http://" & lstProxy.Items(x)
Dim myProxy As New WebProxy(prx)
And if you write same in C#, there is hardly any difference.
Ok - could not figure it out! But have since switched to C#.net and it's all good.
I've handled it like this:
string front = "http://";
string prx = front + lstProxy.Items[x].ToString();
WebProxy myProxy = new WebProxy(prx);
So, top advice from me is to get C#.Net. Feel free to comment on this if any help is needed.

How to set user "logon to" AD attribute in VB.NET

I'm working on upgrading a solution in VB.NET that is heavily based on Active Directory. As of now, I'm trying to add a PC restriction to a new AD User upon user creation. Essentially, I need to update the Logon To attribute to include 1 or more PCs, how do I go about doing this?
I learned that I am interested in the IADsUser property "LoginWorkstations" (thanks to http://msdn.microsoft.com/en-us/library/Aa746340). As of now, I have code that can fetch this attribute from any AD user, but I cannot set it.
Here is the code I have to fetch the attribute:
Dim userADObject As new DirectoryEntry(ADPath)
Dim logonToPC as String = userADObject.InvokeGet("LoginWorkstations")(0).ToString
That will fetch the first restricted PC (if there is one) and save it in logonToPC and will look something like "PC10000"
That works great, so intuitively I would assume something like this would work:
Dim userADObject As new DirectoryEntry(ADPath)
Dim args() As Object = {"PC100001"}
userADObject.InvokeSet("LoginWorkstations", args)
But it doesn't work... It just throws a rather unhelpful exception.
I've tried testing this approach with a different attribute and it works just fine. Not much out there on Google either unfortunately...
Any help would be greatly appreciated.
You should be able to do this fairly easily - also: note that you should use the userWorkstations LDAP attribute (see note here) - this is multi-valued, e.g. it allows multiple entries.
Dim userADObject As new DirectoryEntry(ADPath)
userADObject.Properties("userWorkstations").Add("PC001")
userADObject.Properties("userWorkstations").Add("PC002")
userADObject.Properties("userWorkstations").Add("PC003")
userADObject.CommitChanges()
If you have the necessary permissions to update Active Directory, that should basically do it, I think.
Found the solution that works. I took marc_s's code and modified a bit to work properly. here's what I have:
Dim userADObject As New DirectoryEntry(Me.ADPath)
'Grab the previous restriction, because we may have to clear it first in the future
Dim priorRestriction As String = userADObject.Properties("userWorkstations").Value
If priorRestriction = "" Then
'Simply add
userADObject.Properties("userWorkstations").Add("PC001,PC002")
Else
'Important - We have to clear the old restriction before adding the new
userADObject.Properties("userWorkstations").Remove(priorRestriction)
'Now add the new restriction
userADObject.Properties("userWorkstations").Add(priorRestriction & ",PC003")
End If
'Commit!
userADObject.CommitChanges()
Something that gave me some pretty good grief what that I you can't have a space in the string being added. Example: .Add("PC001, PC002") has to be .Add("PC001,PC002")