-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
executable file
·172 lines (162 loc) · 6.79 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using HtmlAgilityPack;
using RestSharp;
using RestSharp.Extensions.MonoHttp;
using System.Text.RegularExpressions;
namespace Moodle
{
class Program
{
static void AskUser(string output, bool isNewline)
{
if (!isNewline)
{
Program.ConsoleSetColor(ConsoleColor.Cyan);
Console.Write(output);
Program.ConsoleSetColor(ConsoleColor.White);
return;
}
Program.ConsoleSetColor(ConsoleColor.Cyan);
Console.WriteLine(output);
Program.ConsoleSetColor(ConsoleColor.White);
}
static void ConsoleSetColor(ConsoleColor consoleColor)
{
Console.ForegroundColor = consoleColor;
}
static void DownloadAllFilesLocal(Dictionary<string, string> downloadList, string chosenFilePath, string chosenFolderName, IRestResponse loginResponse)
{
string filePath;
Console.WriteLine("");
//Regex setup for removing multiple spaces
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
if (chosenFolderName == string.Empty)
{
filePath = string.Concat(chosenFilePath, "");
}
else
{
Directory.CreateDirectory(string.Concat(chosenFilePath, "\\", chosenFolderName));
filePath = string.Concat(chosenFilePath, "\\", chosenFolderName, "\\");
}
foreach (KeyValuePair<string, string> keyValuePair in downloadList)
{
string tempFilePath = filePath;
RestClient client = new RestClient(keyValuePair.Value);
RestRequest request = new RestRequest(Method.GET);
request.AddCookie(loginResponse.Cookies[0].Name, loginResponse.Cookies[0].Value);
Uri uri = client.Execute(request).ResponseUri;
tempFilePath = string.Concat(tempFilePath, uri.Segments.Last<string>());
WebClient webClient = new WebClient();
webClient.Headers.Add(HttpRequestHeader.Cookie, string.Concat(loginResponse.Cookies[0].Name, "=", loginResponse.Cookies[0].Value));
webClient.DownloadFile(uri, regex.Replace(HttpUtility.HtmlDecode(HttpUtility.UrlDecode(tempFilePath)),@" "));
Program.ConsoleSetColor(ConsoleColor.Gray);
Console.WriteLine(HttpUtility.UrlDecode(string.Concat(uri.Segments.Last<string>(), " complete")));
}
}
static Dictionary<string, string> GetDownloadUrl(IRestResponse loginResponse, Dictionary<string, string> courseDictionary, int courseKeyValuePairIndex)
{
Dictionary<string, string> urlList = new Dictionary<string, string>();
KeyValuePair<string, string> keyValuePair = courseDictionary.ElementAt<KeyValuePair<string, string>>(courseKeyValuePairIndex);
RestClient client = new RestClient(keyValuePair.Value);
RestRequest request = new RestRequest(Method.GET);
request.AddCookie(loginResponse.Cookies[0].Name, loginResponse.Cookies[0].Value);
IRestResponse response = client.Execute(request);
HtmlDocument document = new HtmlDocument();
document.Load(Utils.GenerateStreamFromString(response.Content));
foreach (HtmlNode htmlNode in (IEnumerable<HtmlNode>)document.DocumentNode.SelectNodes("//*[@class=\"activityinstance\"]"))
{
if (urlList.ContainsKey(htmlNode.InnerText))
{
continue;
}
urlList.Add(htmlNode.InnerText, htmlNode.ChildNodes[0].GetAttributeValue("href", ""));
}
return urlList;
}
static IRestResponse LoginToMoodle(string userName, string password)
{
RestClient client = new RestClient("https://courses.iitm.ac.in/login/index.php")
{
CookieContainer = new CookieContainer()
};
RestRequest request = new RestRequest(Method.POST);
request.AddParameter("username", userName, ParameterType.GetOrPost);
request.AddParameter("password", password, ParameterType.GetOrPost);
request.AddHeader("HTTPonly", "true");
IRestResponse response = client.Execute(request);
string xCookie = client.CookieContainer.GetCookieHeader(new Uri("http://courses.iitm.ac.in"));
string[] parsedStrings = xCookie.Split(new char[] { '=' });
IList<RestResponseCookie> cookies = response.Cookies;
RestResponseCookie restResponseCookie = new RestResponseCookie()
{
Name = parsedStrings[0],
Value = parsedStrings[1]
};
cookies.Add(restResponseCookie);
return response;
}
static void Main(string[] args)
{
IRestResponse loginResponse;
int chosenCourseIndex;
do
{
Program.AskUser("\nRoll No : ", false);
string userName = Console.ReadLine();
Program.AskUser("Password : ", false);
string password = Utils.GetStringPassword();
Program.ConsoleSetColor(ConsoleColor.Cyan);
loginResponse = Program.LoginToMoodle(userName, password);
} while (loginResponse.Content.Contains("Invalid login, please try again"));
Console.WriteLine("\n");
HtmlDocument homeDocument = new HtmlDocument();
homeDocument.Load(Utils.GenerateStreamFromString(loginResponse.Content));
Dictionary<string, string> courseDictionary = Program.ParseCourses(homeDocument);
int i = 0;
foreach (KeyValuePair<string, string> keyValuePair in courseDictionary)
{
Program.ConsoleSetColor(ConsoleColor.Gray);
Console.Write(i + 1);
Console.Write(") ");
Program.ConsoleSetColor(ConsoleColor.White);
Console.WriteLine(HttpUtility.HtmlDecode(keyValuePair.Key));
i++;
}
Program.AskUser("\nChoose a course for bulk downloading course contents -> ", false);
int.TryParse(Console.ReadLine(), out chosenCourseIndex);
chosenCourseIndex--;
Program.ConsoleSetColor(ConsoleColor.Cyan);
KeyValuePair<string, string> keyValuePair1 = courseDictionary.ElementAt<KeyValuePair<string, string>>(chosenCourseIndex);
Program.AskUser(string.Concat("You have chosen ", HttpUtility.HtmlDecode(keyValuePair1.Key)), true);
Console.WriteLine("");
Program.AskUser("Enter the folder path for download -> ", false);
string chosenFilePath = Console.ReadLine();
Program.AskUser("Enter the folder name for download -> ", false);
string chosenFolderName = Console.ReadLine();
Dictionary<string, string> downloadList = Program.GetDownloadUrl(loginResponse, courseDictionary, chosenCourseIndex);
Program.DownloadAllFilesLocal(downloadList, chosenFilePath, chosenFolderName, loginResponse);
}
static Dictionary<string, string> ParseCourses(HtmlDocument htmlDocument)
{
Dictionary<string, string> courseList = new Dictionary<string, string>();
HtmlNodeCollection registeredCoursesCollection = htmlDocument.DocumentNode.SelectNodes("//*[@class=\"courses frontpage-course-list-enrolled\"]");
HtmlDocument document = new HtmlDocument();
document.Load(Utils.GenerateStreamFromString(registeredCoursesCollection[0].InnerHtml));
foreach (HtmlNode htmlNode in document.DocumentNode.SelectNodes ("//*[@class=\"coursename\"]"))
{
if (courseList.ContainsKey (htmlNode.InnerText))
{
continue;
}
courseList.Add (htmlNode.InnerText, htmlNode.ChildNodes [0].GetAttributeValue ("href", ""));
}
return courseList;
}
}
}