Guest User

Untitled

a guest
Jul 12th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using AdflyUrlShrinker.Classes;
  11. using AdflyUrlShrinker.Properties;
  12.  
  13. namespace AdflyUrlShrinker
  14. {
  15.     public partial class MainForm : Form
  16.     {
  17.         private List<string> _linkList = new List<string>();
  18.  
  19.         private CancellationTokenSource _cts;
  20.  
  21.         public MainForm()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private string MakeAdFlyUrl(string url)
  27.         {
  28.             try
  29.             {
  30.                 string requestUrl = string.Format("http://api.adf.ly/api.php?key={0}&uid={1}&advert_type=int&domain=adf.ly&url={2}", adFlyKeyText.Text, adFlyUidText.Text, url);
  31.  
  32.                 using (var client = new WebClient())
  33.                 {
  34.                     string shrunk = client.DownloadString(requestUrl);
  35.  
  36.                     return shrunk != "error" ? shrunk : string.Empty;
  37.                 }
  38.             }
  39.             catch (Exception ex)
  40.             {
  41.  
  42.                 return string.Empty;
  43.             }
  44.            
  45.         }
  46.  
  47.         private void SaveLinks()
  48.         {
  49.             var savefile = new SaveFileDialog
  50.             {
  51.                 Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*",
  52.                 InitialDirectory = "%SystemDrive%",
  53.                 Title = "Save Shrunk URL List"
  54.             };
  55.             if (savefile.ShowDialog() == DialogResult.OK)
  56.             {
  57.                 if (!String.IsNullOrEmpty(savefile.FileName))
  58.                 {
  59.                     var saveList = (from object item in shrunkBox.Items select item.ToString()).ToList();
  60.                     File.WriteAllLines(savefile.FileName, saveList);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         private void clearToolStripMenuItem_Click(object sender, EventArgs e)
  66.         {
  67.             shrinkBox.Items.Clear();
  68.             totalShrinkLinkLabel.Text = "0";
  69.         }
  70.  
  71.         private void saveBtn_Click(object sender, EventArgs e)
  72.         {
  73.             SaveLinks();
  74.         }
  75.  
  76.         private void loadShrinkListBtn_Click(object sender, EventArgs e)
  77.         {
  78.             _linkList = Global.LoadLinks();
  79.  
  80.             if (_linkList != null)
  81.             {
  82.                 shrinkBox.Items.AddRange(_linkList.ToArray());
  83.                 totalShrinkLinkLabel.Text = shrinkBox.Items.Count.ToString();
  84.             }
  85.         }
  86.  
  87.         private void stopBtn_Click(object sender, EventArgs e)
  88.         {
  89.             if (_cts != null)
  90.             {
  91.                 _cts.Cancel();
  92.             }
  93.         }
  94.  
  95.         private void shrinkBtn_Click(object sender, EventArgs e)
  96.         {
  97.             _cts = new CancellationTokenSource();
  98.             CancellationToken ct = _cts.Token;
  99.             int t = 0;
  100.  
  101.             Task.Factory.StartNew(() =>
  102.             {
  103.                 try
  104.                 {
  105.                     ct.ThrowIfCancellationRequested();
  106.                     Parallel.ForEach(_linkList,
  107.                         new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(threadCount.Text), CancellationToken = ct }, link =>
  108.                         {
  109.                             string shrunkLink = MakeAdFlyUrl(link);
  110.  
  111.                             if (!String.IsNullOrEmpty(shrunkLink))
  112.                             {
  113.                                 Interlocked.Increment(ref t);
  114.                                 Invoke(new MethodInvoker(
  115.                                     () =>
  116.                                     {
  117.                                         shrunkBox.Items.Add(shrunkLink);
  118.                                         totalShrunkLabel.Text = t.ToString();
  119.                                     }));
  120.                             }
  121.                         });
  122.                 }
  123.                 // catch cancel exceptions
  124.                 catch (OperationCanceledException)
  125.                 {
  126.                 }
  127.                 catch (ObjectDisposedException)
  128.                 {
  129.                 }
  130.                 catch (Exception)
  131.                 {
  132.                 }
  133.             }, ct);
  134.         }
  135.  
  136.         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
  137.         {
  138.             SaveLinks();
  139.         }
  140.  
  141.         private void copySelectedToolStripMenuItem_Click(object sender, EventArgs e)
  142.         {
  143.             var clipData = new StringBuilder();
  144.             foreach (object value in shrunkBox.SelectedItems)
  145.             {
  146.                 clipData.AppendLine(value.ToString());
  147.             }
  148.             Clipboard.SetText(clipData.ToString());
  149.         }
  150.  
  151.         private void clearToolStripMenuItem1_Click(object sender, EventArgs e)
  152.         {
  153.             shrunkBox.Items.Clear();
  154.             totalShrunkLabel.Text = "0";
  155.         }
  156.  
  157.         private void MainForm_Load(object sender, EventArgs e)
  158.         {
  159.             saveCheckBox.Checked = (bool)Settings.Default["SaveBool"];
  160.             if (saveCheckBox.Checked)
  161.             {
  162.                 adFlyKeyText.Text = Settings.Default["KeyString"].ToString();
  163.                 adFlyUidText.Text = Settings.Default["UIDString"].ToString();
  164.             }
  165.         }
  166.  
  167.         private void saveCheckBox_Click(object sender, EventArgs e)
  168.         {
  169.             Settings.Default["SaveBool"] = saveCheckBox.Checked;
  170.  
  171.             if (saveCheckBox.Checked)
  172.             {
  173.                 Settings.Default["KeyString"] = adFlyKeyText.Text;
  174.                 Settings.Default["UIDString"] = adFlyUidText.Text;
  175.             }
  176.  
  177.             Settings.Default.Save();
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment