· 2 years ago · Dec 14, 2022, 12:50 PM
1 private async void DownloadPastebin()
2 {
3 var progress = new Common.Progress<ProgressInfo>(TimeSpan.FromMilliseconds(50), this.view.UpdateProgressAction);
4 await TaskDownloadPastebin(progress);
5 }
6 public async Task TaskDownloadPastebin(IProgress<ProgressInfo> progress)
7 {
8 //before using any class in the api you must enter your api dev key
9 Pastebin.DevKey = "iCv_LuM23pCxy1OUT-OgWkYx7ofrWx4z";
10 //you can see yours here: https://pastebin.com/api#1
11 try
12 {
13 progress.Report(new ProgressInfo((double)(10), "10"));
14
15 User me = await Pastebin.LoginAsync("Landromm", "Pastebin_21!03@1993#");
16
17 progress.Report(new ProgressInfo((double)(50), "50"));
18
19 foreach (Paste paste in await me.ListPastesAsync(10)) // we limmit the results to 3
20 {
21 if (paste.Title.Equals("MyNotebookDB"))
22 {
23 var str = ConvertHexToString(_encryptionData.DencryptionMethod(await paste.GetRawAsync()));
24 Console.WriteLine(str);
25 }
26 progress.Report(new ProgressInfo((double)(100), "100"));
27 }
28
29 ////deletes the paste we just created
30 //await me.DeletePasteAsync(newPaste);
31 }
32 catch (PastebinException ex) //api throws PastebinException
33 {
34 //in the Parameter property you can see what invalid parameter was sent
35 //here we check if the exeption is thrown because of invalid login details
36 if (ex.Parameter == PastebinException.ParameterType.Login)
37 {
38 Console.Error.WriteLine("Invalid username/password");
39 }
40 else
41 {
42 Console.Error.WriteLine(ex);
43 throw; //all other types are rethrown and not swalowed!
44 }
45 view.Message = "Загрузка данных c Pastebin прервалась!\n\n" + ex;
46 }
47 finally
48 {
49 //view.IsPastePastebin = true;
50 view.Message = "Загрузка данных c Pastebin, прошла успешно!";
51 }
52 }
53
54 private async void UploadPastebin()
55 {
56 var progress = new Common.Progress<ProgressInfo>(TimeSpan.FromMilliseconds(50), this.view.UpdateProgressAction);
57 await TaskUploadPastebin(progress);
58 }
59 public async Task TaskUploadPastebin(IProgress<ProgressInfo> progress)
60 {
61 //before using any class in the api you must enter your api dev key
62 Pastebin.DevKey = "iCv_LuM23pCxy1OUT-OgWkYx7ofrWx4z";
63 //you can see yours here: https://pastebin.com/api#1
64 try
65 {
66 progress.Report(new ProgressInfo((double)(10), "10"));
67 // login and get user object
68 User me = await Pastebin.LoginAsync("Landromm", "Pastebin_21!03@1993#");
69 // user contains information like e-mail, location etc...
70 Console.WriteLine("{0}({1}) lives in {2}", me, me.Email, me.Location);
71 // lists all pastes for this user
72 progress.Report(new ProgressInfo((double)(25), "25"));
73
74 string code = _encryptionData.EncryptionMethod(ReadFile("TodoList.xml"));
75
76 //creates a new paste and get paste object
77 Paste newPaste = await me.CreatePasteAsync(ConvertStringToHex(code), "MyNotebookDB", Language.None, Visibility.Public, Expiration.Never);
78 //newPaste now contains the link returned from the server
79 //Console.WriteLine("URL: {0}", newPaste.Url);
80 //Console.WriteLine("Paste key: {0}", newPaste.Key);
81 //Console.WriteLine("Content: {0}", newPaste.Text);
82 progress.Report(new ProgressInfo((double)(50), "50"));
83
84 //deletes the paste we just created
85 await me.DeletePasteAsync(newPaste);
86
87 //lists all currently trending pastes(similar to me.ListPastes())
88 //foreach (Paste paste in await Pastebin.ListTrendingPastesAsync())
89 //{
90 // Console.WriteLine("{0} - {1}", paste.Title, paste.Url);
91 //}
92 //you can create pastes directly from Pastebin static class but they are created as guests and you have a limited number of guest uploads
93 //Paste anotherPaste = await Paste.CreateAsync("another paste", "MyPasteTitle2", Language.CSharp, Visibility.Unlisted, Expiration.OneHour);
94 //Console.WriteLine(anotherPaste.Title);
95 progress.Report(new ProgressInfo((double)(100), "100"));
96
97 }
98 catch (PastebinException ex) //api throws PastebinException
99 {
100 //in the Parameter property you can see what invalid parameter was sent
101 //here we check if the exeption is thrown because of invalid login details
102 if (ex.Parameter == PastebinException.ParameterType.Login)
103 {
104 Console.Error.WriteLine("Invalid username/password");
105 }
106 else
107 {
108 throw; //all other types are rethrown and not swalowed!
109 }
110 view.Message = "Загрузка данных на Pastebin прервалась!\n\n" + ex;
111 }
112 finally
113 {
114 //view.IsPastePastebin = true;
115 view.Message = "Загрузка данных на Pastebin, прошла успешно!";
116 }
117 }
118
119 private string ConvertStringToHex(string str)
120 {
121 byte[] bytes = Encoding.UTF8.GetBytes(str);
122 string[] h = bytes.Select(x => x.ToString("x2")).ToArray();
123 string hex = string.Concat(h);
124 Console.WriteLine(hex);
125
126 return hex;
127 }
128 private string ConvertHexToString(string str)
129 {
130 string[] hexBytes = new string[str.Length / 2];
131 for (int i = 0; i < hexBytes.Length; i++)
132 {
133 hexBytes[i] = str.Substring(i * 2, 2);
134 }
135 byte[] resultBytes = hexBytes.Select(value => Convert.ToByte(value, 16)).ToArray();
136 string result = Encoding.UTF8.GetString(resultBytes);
137 Console.WriteLine(result);
138
139 return result;
140 }
141
142 private string ReadFile(string path)
143 {
144 string text = "";
145 using (StreamReader reader = new StreamReader(path))
146 {
147 text = reader.ReadToEnd();
148 //Console.WriteLine(text);
149 }
150 return text;
151 }