From 93dffd31f2f893bec3f2c40f0ef5a78d9250eac3 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Fri, 20 Mar 2020 14:57:50 +0330 Subject: [PATCH] Add More Server --- UrlShortener/App.xaml.cs | 2 + UrlShortener/Data/AppConfig.cs | 2 +- UrlShortener/Data/Extensions.cs | 14 ++ UrlShortener/Model/ComboModel.cs | 8 + UrlShortener/Model/ShorterListModel.cs | 8 + UrlShortener/UrlShortener.csproj | 9 +- .../ViewModels/LeftMainContentViewModel.cs | 21 +- .../ViewModels/MainShortenerViewModel.cs | 203 ++++++++++++++++-- UrlShortener/Views/MainShortener.xaml | 20 +- 9 files changed, 257 insertions(+), 30 deletions(-) create mode 100644 UrlShortener/Data/Extensions.cs create mode 100644 UrlShortener/Model/ComboModel.cs create mode 100644 UrlShortener/Model/ShorterListModel.cs diff --git a/UrlShortener/App.xaml.cs b/UrlShortener/App.xaml.cs index 163a4ce..9b2041f 100644 --- a/UrlShortener/App.xaml.cs +++ b/UrlShortener/App.xaml.cs @@ -1,5 +1,6 @@ using HandyControl.Data; using Prism.Ioc; +using Prism.Regions; using System; using System.Windows; using UrlShortener.Views; @@ -42,6 +43,7 @@ public override void Initialize() { UpdateSkin(GlobalData.Config.Skin); } + Container.Resolve().RequestNavigate("ContentRegion", "MainShortener"); } } } diff --git a/UrlShortener/Data/AppConfig.cs b/UrlShortener/Data/AppConfig.cs index facb1dc..b51fa6f 100644 --- a/UrlShortener/Data/AppConfig.cs +++ b/UrlShortener/Data/AppConfig.cs @@ -7,9 +7,9 @@ internal class AppConfig { public static readonly string SavePath = $"{AppDomain.CurrentDomain.BaseDirectory}AppConfig.json"; - public bool IsShowNotifyIcon { get; set; } = false; public bool IsFirstRun { get; set; } = true; + public int SelectedIndex { get; set; } = 0; public SkinType Skin { get; set; } = SkinType.Default; } diff --git a/UrlShortener/Data/Extensions.cs b/UrlShortener/Data/Extensions.cs new file mode 100644 index 0000000..8e1f026 --- /dev/null +++ b/UrlShortener/Data/Extensions.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; +using System.Net.Http; +using System.Text; + +namespace UrlShortener.Data +{ + public static class Extensions + { + public static StringContent AsJson(this object o) + { + return new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json"); + } + } +} diff --git a/UrlShortener/Model/ComboModel.cs b/UrlShortener/Model/ComboModel.cs new file mode 100644 index 0000000..ed2b395 --- /dev/null +++ b/UrlShortener/Model/ComboModel.cs @@ -0,0 +1,8 @@ +namespace UrlShortener.Model +{ + public class ComboModel + { + public string Name { get; set; } + public bool IsCustomTextAvailable { get; set; } + } +} diff --git a/UrlShortener/Model/ShorterListModel.cs b/UrlShortener/Model/ShorterListModel.cs new file mode 100644 index 0000000..6a34021 --- /dev/null +++ b/UrlShortener/Model/ShorterListModel.cs @@ -0,0 +1,8 @@ +namespace UrlShortener.Model +{ + public class ShorterListModel + { + public string Link { get; set; } + public string ShortLink { get; set; } + } +} diff --git a/UrlShortener/UrlShortener.csproj b/UrlShortener/UrlShortener.csproj index 4e233d0..f5573b5 100644 --- a/UrlShortener/UrlShortener.csproj +++ b/UrlShortener/UrlShortener.csproj @@ -8,8 +8,8 @@ 1.0.0.0 Debug;Release Copyright © Mahdi 2018-2020 - 4.0.0.0 - 4.0.0.0 + 4.1.0.0 + 4.1.0.0 latest Resources\Img\icon.ico Coded by Mahdi Hosseini @@ -26,7 +26,7 @@ - + @@ -42,7 +42,4 @@ Never - - - \ No newline at end of file diff --git a/UrlShortener/ViewModels/LeftMainContentViewModel.cs b/UrlShortener/ViewModels/LeftMainContentViewModel.cs index 1202846..e360b32 100644 --- a/UrlShortener/ViewModels/LeftMainContentViewModel.cs +++ b/UrlShortener/ViewModels/LeftMainContentViewModel.cs @@ -1,7 +1,9 @@ using Prism.Commands; using Prism.Mvvm; using Prism.Regions; +using System.Linq; using System.Windows.Controls; +using UrlShortener.Views; namespace UrlShortener.ViewModels { @@ -23,7 +25,24 @@ public LeftMainContentViewModel(IRegionManager regionManager) private void UrlShortener() { - _regionManager.RequestNavigate("ContentRegion", "MainShortener"); + object activeView = _regionManager.Regions["ContentRegion"].ActiveViews.FirstOrDefault(); + if (activeView != null) + { + bool IsViewMainShortener = activeView.GetType() == typeof(MainShortener); + + if (IsViewMainShortener) + { + _regionManager.Regions["ContentRegion"].RemoveAll(); + } + else + { + _regionManager.RequestNavigate("ContentRegion", "MainShortener"); + } + } + else + { + _regionManager.RequestNavigate("ContentRegion", "MainShortener"); + } } private void Switch(SelectionChangedEventArgs e) diff --git a/UrlShortener/ViewModels/MainShortenerViewModel.cs b/UrlShortener/ViewModels/MainShortenerViewModel.cs index 651f60f..ccb5708 100644 --- a/UrlShortener/ViewModels/MainShortenerViewModel.cs +++ b/UrlShortener/ViewModels/MainShortenerViewModel.cs @@ -16,23 +16,22 @@ using System.Web; using System.Windows; using System.Windows.Controls; +using UrlShortener.Data; +using UrlShortener.Model; namespace UrlShortener.ViewModels { public class MainShortenerViewModel : BindableBase { - public class ShorterListModel - { - public string Link { get; set; } - public string ShortLink { get; set; } - } - #region Single Url #region API private const string OpizoApiKey = "3DD3A7CD39B37BC8CBD9EFEEAC0B03DA"; private const string PlinkApiKey = "Mhl5uGRbVYUm"; private const string BitlyApiKey = "R_c597e397b606436fa6a9179626da61bb"; private const string BitlyLoginKey = "o_1i6m8a9v55"; + private const string CuttlyApiKey = "3f2e3ef2f0597c089cd1af605680c962629c0"; + private const string RebrandlyApiKey = "b54644c5ea5147ea97a7095e354ca2bc"; + private const string MakhlasApiKey = "b64cc0ab-f274-486e-ac23-74dd3e10d9d1"; #endregion @@ -42,10 +41,9 @@ public class ShorterListModel #endregion - #region Property - private ObservableCollection _DataServer = new ObservableCollection(); - public ObservableCollection DataServer + private ObservableCollection _DataServer = new ObservableCollection(); + public ObservableCollection DataServer { get => _DataServer; set => SetProperty(ref _DataServer, value); @@ -69,7 +67,15 @@ public string SelectedCustomText public int SelectedItemIndex { get => _SelectedItemIndex; - set => SetProperty(ref _SelectedItemIndex, value); + set + { + if (value != _SelectedItemIndex) + { + SetProperty(ref _SelectedItemIndex, value); + GlobalData.Config.SelectedIndex = value; + GlobalData.Save(); + } + } } private string _ShortedUrl; @@ -81,8 +87,6 @@ public string ShortedUrl #endregion - - #endregion #region Multiple Url @@ -109,14 +113,20 @@ public ObservableCollection ShorterList } #endregion #endregion + public MainShortenerViewModel() { #region Single Url LongUrlChangedCmd = new DelegateCommand(UrlChanged); ShortenCmd = new DelegateCommand(Shorten); - string[] serverList = new string[] { "Opizo", "Bitly", "PLink" }; - DataServer.AddRange(serverList); + DataServer.Add(new ComboModel { Name = "Opizo", IsCustomTextAvailable = false }); + DataServer.Add(new ComboModel { Name = "Bitly", IsCustomTextAvailable = false }); + DataServer.Add(new ComboModel { Name = "PLink", IsCustomTextAvailable = true }); + DataServer.Add(new ComboModel { Name = "Cuttly", IsCustomTextAvailable = true }); + DataServer.Add(new ComboModel { Name = "Rebrandly", IsCustomTextAvailable = true }); + DataServer.Add(new ComboModel { Name = "TinyUrl [Need VPN From Iran]", IsCustomTextAvailable = false }); + DataServer.Add(new ComboModel { Name = "Makhlas", IsCustomTextAvailable = false }); #endregion #region Multiple Url @@ -124,6 +134,8 @@ public MainShortenerViewModel() ButtonHelpCmd = new DelegateCommand(Help); ButtonStartCmd = new DelegateCommand(Start); #endregion + + SelectedItemIndex = GlobalData.Config.SelectedIndex; } #region Multiple Url @@ -150,6 +162,18 @@ private async void Start(IList shorterSelectedList) case 2: ShorterList.Add(new ShorterListModel { ShortLink = await PlinkShorten(longLink) }); break; + case 3: + ShorterList.Add(new ShorterListModel { ShortLink = await CuttlyShorten(longLink) }); + break; + case 4: + ShorterList.Add(new ShorterListModel { ShortLink = await RebrandlyShorten(longLink) }); + break; + case 5: + ShorterList.Add(new ShorterListModel { ShortLink = await TinyUrlShorten(longLink) }); + break; + case 6: + ShorterList.Add(new ShorterListModel { ShortLink = await MakhlasShorten(longLink) }); + break; } } IsBusy = false; @@ -206,6 +230,7 @@ private void OpenFile() } } #endregion + #region Single Url private void UrlChanged(TextChangedEventArgs e) { @@ -238,7 +263,22 @@ private async void Shorten(string longUrl) case 2: ShortedUrl = await PlinkShorten(longUrl); + break; + + case 3: + ShortedUrl = await CuttlyShorten(longUrl); + break; + + case 4: + ShortedUrl = await RebrandlyShorten(longUrl); + break; + + case 5: + ShortedUrl = await TinyUrlShorten(longUrl); + break; + case 6: + ShortedUrl = await MakhlasShorten(longUrl); break; } IsButtonEnable = true; @@ -360,6 +400,141 @@ public async Task BitlyShorten(string longUrl) return "error"; } + + public async Task CuttlyShorten(string longUrl) + { + + try + { + string url = string.Format("https://cutt.ly/api/api.php?key={0}&short={1}&name={2}", CuttlyApiKey, HttpUtility.UrlEncode(longUrl), SelectedCustomText); + + using (HttpClient client = new HttpClient()) + using (HttpResponseMessage response = await client.GetAsync(url)) + using (HttpContent content = response.Content) + { + dynamic root = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + + string statusCode = root.url.status; + if (statusCode.Equals("7")) + { + string link = root.url.shortLink; + + return link; + } + else + { + string error = root.status; + Growl.Error(error); + } + + } + } + catch (Exception ex) + { + Growl.Error(ex.Message); + } + + + return "error"; + } + + public async Task RebrandlyShorten(string longUrl) + { + try + { + var data = new { destination = longUrl, slashtag = SelectedCustomText }; + + using HttpClient client = new HttpClient(); + client.DefaultRequestHeaders.Add("apikey", RebrandlyApiKey); + HttpResponseMessage response = await client.PostAsync("https://api.rebrandly.com/v1/links", data.AsJson()); + dynamic root = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + + string link = root.shortUrl; + + if (link != null) + { + return link; + } + else + { + string error = root.message; + Growl.Error(error); + } + } + catch (Exception ex) + { + Growl.Error(ex.Message); + } + + return "error"; + } + + public async Task TinyUrlShorten(string longUrl) + { + + try + { + string url = string.Format("https://tinyurl.com/api-create.php?url={0}", HttpUtility.UrlEncode(longUrl)); + + using (HttpClient client = new HttpClient()) + using (HttpResponseMessage response = await client.GetAsync(url)) + using (HttpContent content = response.Content) + { + string root = response.Content.ReadAsStringAsync().Result; + + if (!root.Contains("Error")) + { + return root; + } + else + { + Growl.Error(root); + } + + } + } + catch (Exception ex) + { + Growl.Error(ex.Message); + } + + + return "error"; + } + + public async Task MakhlasShorten(string longUrl) + { + try + { + var data = new { long_url = longUrl }; + + using HttpClient client = new HttpClient(); + client.DefaultRequestHeaders.Add("authorization", $"Bearer {MakhlasApiKey}"); + + HttpResponseMessage response = await client.PostAsync( + $"http://api.makhlas.com/v1/url", data.AsJson()); + dynamic root = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + + string status = root.success; + if (status.ToLower().Contains("true")) + { + string link = root.data[0].short_url; + return link; + } + else + { + string error = root.detail; + Growl.Error(error); + } + } + catch (Exception ex) + { + Growl.Error(ex.Message); + } + + return "error"; + } + #endregion } } diff --git a/UrlShortener/Views/MainShortener.xaml b/UrlShortener/Views/MainShortener.xaml index 2a19293..39e228b 100644 --- a/UrlShortener/Views/MainShortener.xaml +++ b/UrlShortener/Views/MainShortener.xaml @@ -16,6 +16,9 @@ x:Name="cmbService" Width="240" Margin="0,10,0,0" + DisplayMemberPath="Name" + SelectedValuePath="IsCustomTextAvailable" + SelectedValue="true" SelectedIndex="{Binding SelectedItemIndex, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" ItemsSource="{Binding DataServer}" @@ -35,10 +38,10 @@ @@ -60,10 +63,10 @@ @@ -128,6 +131,7 @@