- Joined
- Nov 1, 2007
- Messages
- 42
well, recently I've been programming in C# more than anything lately, which is most likely because of it's amazing socket library, which cuts the time it takes me to complete some of my web-related programming tasks and well recently i was on a web page and i needed to grab something like 50 rapidshare links from the page but i was sick of "Right-Click + C" 'ing every single link so i decided to program this little application which will go to the URL that the user gives as input and then parses all the rapidshare links into a text box for copying (only works on HTML pages at the moment):
This is the layout for the program (don't mind the custom GUI, a normal one will do) i don't plan on continuing it so here is the source code for anyone interested:
I'll try to explain anything that looks complicated:
i created this function so that when a user presses "Ctrl + a" it would simulate the "select all" function so that all links would be copied to clipboard, if you would prefer to set it up so that the links get sent straight to the clipboard without the user having to press "Ctrl + a" it's pretty simple to do with the function "Clipboard.SetText" just modify the filtering function like so:
next:
All this code does is when the button with the value "button1" is clicked, a new thread is created with the name "thread2" which executes the function "GetPage" which gets the contents of the page entered in the first text box then displays them in the second text box. For those of you who don't know what a thread is, i can't really give you a SOLID definition because if you ask 20 different programmers what a thread is, they will all give different explanations, so i will give mine.
In my opinion, a thread is like a duplicate instance of your already existing program, why is this useful you may ask? Think about it this way, if you have your main function (int main(), void main() etc.) and you wish to lets say.....i'll use a game protection example, just say you needed to have an infinite loop running to check if a certain process (lets say "gamehack.exe") is executed but you still wanted your program or users to be able to perform functions, how would we do this? if we run the function to look for process "gamehack.exe" in our main, we will halt ALL other tasks meaning, the program is inaccessible, so what we do is use THREADS, now you probably get what i mean by a duplicate instance, we can create a new thread within our current process of our program and have it perform the infinite loop while the main, can still carry out other procedures.
all we really do here is grab the input from textbox1 and place it in a variable named "link" we read the web page located at the address stored inside link then close all readers and finally end the thread, after a thread has been used and is no longer needed, always remember to end it because the more threads you use means, the other programs have less threads to use, so there's no point in having threads around that aren't doing anything.
Finally:
Ok, so basically what's happening here is we start a while loop which will continue the loop under the condition that the textbox contains the string which is stored in our variable "filterString", which is "http://rapidshare.com/" next all we are doing is removing everything apart from "http://rapidshare.com/" + 50 chars, which is the length of the link of the FULL URL for the page i had chosen, the "+50" will change obviously with length of filename and the chosen extensions (e.g. RAR, WMV, RMVB, MP4, AVI).
-
This is the layout for the program (don't mind the custom GUI, a normal one will do) i don't plan on continuing it so here is the source code for anyone interested:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Net;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace RS_LinkGrabber
{
public partial class Form1 : Form
{
// Seeing as i only recently got serious in C#, i imported a few functions that i prefer using in C/C++ :D
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); // Keys enumeration
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
// Defines the values for possible left & right click functions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public string s;
public string filterString = "http://rapidshare.com/";
StringBuilder content = new System.Text.StringBuilder();
Thread thread;
Thread thread2;
//Function that will right-click at the current position of the mouse
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
}
// So that when user presses CTRL + A, it will select the contents of the textbox
public void SelectALL()
{
while (true)
{
Thread.Sleep(10);
if (GetAsyncKeyState(Keys.A) < 0 && GetAsyncKeyState(Keys.ControlKey) < 0)
{
DoMouseClick();
SendKeys.SendWait("A");
thread.Abort();
}
}
}
public Form1()
{
InitializeComponent();
// Create a new thread for the purpose of having monitor the "ctrl + a" combo to select all
thread = new Thread(new ThreadStart(SelectALL));
thread.Start();
}
public void GetPage()
{
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string link = textBox1.Text;
Stream data = client.OpenRead(link);
StreamReader reader = new StreamReader(data);
s = reader.ReadToEnd();
data.Close();
reader.Close();
thread2.Abort(); //End Thread
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
thread2 = new Thread(new ThreadStart(GetPage));
thread2.Start();
textBox2.Text = s;
}
private void button2_Click(object sender, EventArgs e)
{
while (textBox2.Text.Contains(filterString))
{
//Store instance of string
content.AppendLine(textBox2.Text.Substring(textBox2.Text.IndexOf(filterString), filterString.Length + 50));
//Remove that instance of string
textBox2.Text = textBox2.Text.Remove(textBox2.Text.IndexOf(filterString), filterString.Length);
}
//Clear the textbox
textBox2.Clear();
//Fill with results
textBox2.Text = content.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Rapidshare Link Grabber 1.0\nCreated by GoSu.Speed aka Shoxin aka Akimoko\n\nJust type in the url of the page with the links press 'GO', then 'FILTER' to grab all the links\nit may take a few seconds to filter out all the links as this is all working off one thread\nmaybe add multiple threads in the next version (if there is one).", "About - Rapidshare Link Grabber", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
I'll try to explain anything that looks complicated:
Code:
public void SelectALL()
{
while (true)
{
Thread.Sleep(10);
if (GetAsyncKeyState(Keys.A) < 0 && GetAsyncKeyState(Keys.ControlKey) < 0)
{
DoMouseClick();
SendKeys.SendWait("A");
thread.Abort();
}
}
}
i created this function so that when a user presses "Ctrl + a" it would simulate the "select all" function so that all links would be copied to clipboard, if you would prefer to set it up so that the links get sent straight to the clipboard without the user having to press "Ctrl + a" it's pretty simple to do with the function "Clipboard.SetText" just modify the filtering function like so:
Code:
private void button2_Click(object sender, EventArgs e)
{
while (textBox2.Text.Contains(filterString))
{
//Store instance of string
content.AppendLine(textBox2.Text.Substring(textBox2.Text.IndexOf(filterString), filterString.Length + 50));
//Remove that instance of string
textBox2.Text = textBox2.Text.Remove(textBox2.Text.IndexOf(filterString), filterString.Length);
}
//Clear the textbox
textBox2.Clear();
//Fill with results
textBox2.Text = content.ToString();
Clipboard.SetText(content.ToString, TextDataFormat.Text);
}
next:
Code:
private void button1_Click(object sender, EventArgs e)
{
thread2 = new Thread(new ThreadStart(GetPage));
thread2.Start();
textBox2.Text = s;
}
All this code does is when the button with the value "button1" is clicked, a new thread is created with the name "thread2" which executes the function "GetPage" which gets the contents of the page entered in the first text box then displays them in the second text box. For those of you who don't know what a thread is, i can't really give you a SOLID definition because if you ask 20 different programmers what a thread is, they will all give different explanations, so i will give mine.
In my opinion, a thread is like a duplicate instance of your already existing program, why is this useful you may ask? Think about it this way, if you have your main function (int main(), void main() etc.) and you wish to lets say.....i'll use a game protection example, just say you needed to have an infinite loop running to check if a certain process (lets say "gamehack.exe") is executed but you still wanted your program or users to be able to perform functions, how would we do this? if we run the function to look for process "gamehack.exe" in our main, we will halt ALL other tasks meaning, the program is inaccessible, so what we do is use THREADS, now you probably get what i mean by a duplicate instance, we can create a new thread within our current process of our program and have it perform the infinite loop while the main, can still carry out other procedures.
Code:
public void GetPage()
{
WebClient client = new WebClient();
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
string link = textBox1.Text;
Stream data = client.OpenRead(link);
StreamReader reader = new StreamReader(data);
s = reader.ReadToEnd();
data.Close();
reader.Close();
thread2.Abort(); //End Thread
}
all we really do here is grab the input from textbox1 and place it in a variable named "link" we read the web page located at the address stored inside link then close all readers and finally end the thread, after a thread has been used and is no longer needed, always remember to end it because the more threads you use means, the other programs have less threads to use, so there's no point in having threads around that aren't doing anything.
Finally:
Code:
private void button2_Click(object sender, EventArgs e)
{
while (textBox2.Text.Contains(filterString))
{
//Store instance of string
content.AppendLine(textBox2.Text.Substring(textBox2.Text.IndexOf(filterString), filterString.Length + 50));
//Remove that instance of string
textBox2.Text = textBox2.Text.Remove(textBox2.Text.IndexOf(filterString), filterString.Length);
}
//Clear the textbox
textBox2.Clear();
//Fill with results
textBox2.Text = content.ToString();
}
Ok, so basically what's happening here is we start a while loop which will continue the loop under the condition that the textbox contains the string which is stored in our variable "filterString", which is "http://rapidshare.com/" next all we are doing is removing everything apart from "http://rapidshare.com/" + 50 chars, which is the length of the link of the FULL URL for the page i had chosen, the "+50" will change obviously with length of filename and the chosen extensions (e.g. RAR, WMV, RMVB, MP4, AVI).
-
Last edited by a moderator: