Skip to content

Commit

Permalink
Merge pull request #18 from stalinon/feature/get-all-teachers
Browse files Browse the repository at this point in the history
Добавлен метод получения всех преподов
  • Loading branch information
stalinon authored Apr 19, 2022
2 parents 7104b6a + e689912 commit 9f244ad
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 29 deletions.
14 changes: 14 additions & 0 deletions ConsoleTest/ConsoleTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\OrioksDecorator\OrioksDecorator.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using OrioksDecorator;

var client = await OrioksClient.Instance();

var res = await client.Teacher.GetAllTeacherInfos();

foreach (var teacher in res)
{
Console.WriteLine(teacher.Name);
}
7 changes: 7 additions & 0 deletions OrioksDecorator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{DA829EF9
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3D34960A-1C31-4D7B-A3D1-B0487497B1E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTest", "ConsoleTest\ConsoleTest.csproj", "{F49467AA-1085-46AC-9ED7-276ECAD4D93B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -32,12 +34,17 @@ Global
{7A259D32-C41E-41E8-AAB4-E3F0EC89F08D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A259D32-C41E-41E8-AAB4-E3F0EC89F08D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A259D32-C41E-41E8-AAB4-E3F0EC89F08D}.Release|Any CPU.Build.0 = Release|Any CPU
{F49467AA-1085-46AC-9ED7-276ECAD4D93B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F49467AA-1085-46AC-9ED7-276ECAD4D93B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F49467AA-1085-46AC-9ED7-276ECAD4D93B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F49467AA-1085-46AC-9ED7-276ECAD4D93B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7A259D32-C41E-41E8-AAB4-E3F0EC89F08D} = {3D34960A-1C31-4D7B-A3D1-B0487497B1E3}
{F49467AA-1085-46AC-9ED7-276ECAD4D93B} = {DA829EF9-F0E5-43CD-B826-2AFD6D26FB8E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3D64A751-B15A-4F67-96C0-2BB0B41B08C3}
Expand Down
94 changes: 82 additions & 12 deletions src/OrioksDecorator/Categories/Impl/TeacherCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace OrioksDecorator.Categories.Impl
{
/// <inheritdoc cref="ITeacherCategory"/>
internal sealed class TeacherCategory : ITeacherCategory
public sealed class TeacherCategory : ITeacherCategory
{
private HttpClient _client;

Expand All @@ -16,6 +16,65 @@ public TeacherCategory(HttpClient client)
_client = client;
}

public async Task<IEnumerable<Teacher>> GetAllTeacherInfos()
{
var result = new List<Teacher>();

foreach (var baseUrl in Dict.Links.Values)
{
var response = await _client.GetAsync(baseUrl);

var html = await response.Content.ReadAsStringAsync();

var parser = new HtmlParser();
var document = parser.ParseDocument(html);

var links = document.QuerySelectorAll("a.people-list__item-name");

foreach (var item in links)
{
var link = "https://miet.ru" + item.GetAttribute("href");
response = await _client.GetAsync(link);

html = await response.Content.ReadAsStringAsync();

parser = new HtmlParser();
document = parser.ParseDocument(html);

var name = document.QuerySelector("span.people-content__name");
var degree = document.QuerySelector("span.people-content__post");
var email = document.QuerySelector("a.people-content__info-list__item-email");
var phoneNumber = document.QuerySelector("span.people-content__info-list__item-phone");
var auditory = document.QuerySelector("span.people-content__info-list__item-number");
var position = document.QuerySelectorAll("span.people-content__info-list__item-elem");
var chapter = document.QuerySelector("a.people-content__info-list__item-link");
var biography = document.QuerySelector("div.people-content__biography");
var courses = document.QuerySelector("div.people-content__courses");
var science = document.QuerySelector("div.people-content__science");
var imageUrl = document.QuerySelector("div.people-content__image");

var teacher = new Teacher
{
Name = name != null ? name.TextContent : default!,
Degree = degree != null ? degree.TextContent : default!,
Email = email != null ? email.TextContent : default!,
PhoneNumber = phoneNumber != null ? phoneNumber.TextContent : default!,
Auditory = auditory != null ? auditory.TextContent : default!,
Position = position.Length != 0 ? position[1].TextContent : default!,
Chapter = chapter != null ? chapter.TextContent : default!,
Biography = biography != null ? biography.TextContent : default!,
Courses = courses != null ? courses.TextContent : default!,
Science = science != null ? science.TextContent : default!,
ImageUrl = imageUrl != null ? "https://miet.ru" + imageUrl.GetAttribute("style")!.Split(new[] { '(', ')' })[1] : default!
};

result.Add(teacher);
}
}

return result;
}

/// <inheritdoc/>
public async Task<Teacher> GetTeachersInfo(string name)
{
Expand All @@ -28,27 +87,38 @@ public async Task<Teacher> GetTeachersInfo(string name)
var document = parser.ParseDocument(html);

var link = "https://miet.ru" + document.QuerySelectorAll("a").Where(a => a.TextContent == name).First().GetAttribute("href");

response = await _client.GetAsync(link);

html = await response.Content.ReadAsStringAsync();

parser = new HtmlParser();
document = parser.ParseDocument(html);

var degree = document.QuerySelector("span.people-content__post");
var email = document.QuerySelector("a.people-content__info-list__item-email");
var phoneNumber = document.QuerySelector("span.people-content__info-list__item-phone");
var auditory = document.QuerySelector("span.people-content__info-list__item-number");
var position = document.QuerySelectorAll("span.people-content__info-list__item-elem");
var chapter = document.QuerySelector("a.people-content__info-list__item-link");
var biography = document.QuerySelector("div.people-content__biography");
var courses = document.QuerySelector("div.people-content__courses");
var science = document.QuerySelector("div.people-content__science");
var imageUrl = document.QuerySelector("div.people-content__image");

var teacher = new Teacher
{
Name = name,
Degree = document.QuerySelector("span.people-content__post")!.TextContent,
Email = document.QuerySelector("a.people-content__info-list__item-email")!.TextContent,
PhoneNumber = document.QuerySelector("span.people-content__info-list__item-phone")!.TextContent,
Auditory = document.QuerySelector("span.people-content__info-list__item-number")!.TextContent,
Position = document.QuerySelectorAll("span.people-content__info-list__item-elem")[1].TextContent,
Chapter = document.QuerySelector("a.people-content__info-list__item-link")!.TextContent,
Biography = document.QuerySelector("div.people-content__biography")!.TextContent,
Courses = document.QuerySelector("div.people-content__courses")!.TextContent,
Science = document.QuerySelector("div.people-content__science")!.TextContent,
ImageUrl = "https://miet.ru"+document.QuerySelector("div.people-content__image")!.GetAttribute("style")!.Split(new[] { '(', ')' })[1]
Degree = degree != null ? degree.TextContent : default!,
Email = email != null ? email.TextContent : default!,
PhoneNumber = phoneNumber != null ? phoneNumber.TextContent : default!,
Auditory = auditory != null ? auditory.TextContent : default!,
Position = position != null ? position[1].TextContent : default!,
Chapter = chapter != null ? chapter.TextContent : default!,
Biography = biography != null ? biography.TextContent : default!,
Courses = courses != null ? courses.TextContent : default!,
Science = science != null ? science.TextContent : default!,
ImageUrl = imageUrl != null ? "https://miet.ru" + imageUrl.GetAttribute("style")!.Split(new[] { '(', ')' })[1] : default!
};

return teacher;
Expand Down
6 changes: 6 additions & 0 deletions src/OrioksDecorator/Categories/Interfaces/ITeacherCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ public interface ITeacherCategory
/// по полному имени
/// </summary>
Task<Teacher> GetTeachersInfo(string name);

/// <summary>
/// Получить информацию о всех
/// преподавателях
/// </summary>
Task<IEnumerable<Teacher>> GetAllTeacherInfos();
}
}
38 changes: 21 additions & 17 deletions src/OrioksDecorator/OrioksClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,40 @@ public sealed class OrioksClient
/// <summary>
/// Создание объекта <see cref="OrioksClient"/>
/// </summary>
public static async Task<OrioksClient> Instance(OrioksAccount account)
public static async Task<OrioksClient> Instance(OrioksAccount? account = null)
{
var instance = new OrioksClient(account);
await instance.Auth(account);
var instance = new OrioksClient();

instance.News = new NewsCategory(_client);
instance.Disciplines = new DisciplinesCategory(_client);
instance.Teacher = new TeacherCategory(_client);
instance.ScheduleNoApi = new ScheduleNoAPICategory(_client);

if (account.Token != null && account.Token != string.Empty)
if (account != null)
{
_hasToken = true;
_clientRest = new RestClient("https://orioks.miet.ru/")
{
Authenticator = new JwtAuthenticator(account.Token),
};

_clientRest.AddDefaultHeader("Accept", "application/json");
_clientRest.AddDefaultHeader("User-Agent", "PostmanRuntime/7.28.4 Windows 10");
await instance.Auth(account);
instance.News = new NewsCategory(_client);
instance.Disciplines = new DisciplinesCategory(_client);

instance.Schedule = new ScheduleCategory(_clientRest);
instance.Student = new StudentCategory(_clientRest);
if (account.Token != null && account.Token != string.Empty)
{
_hasToken = true;
_clientRest = new RestClient("https://orioks.miet.ru/")
{
Authenticator = new JwtAuthenticator(account.Token),
};

_clientRest.AddDefaultHeader("Accept", "application/json");
_clientRest.AddDefaultHeader("User-Agent", "PostmanRuntime/7.28.4 Windows 10");

instance.Schedule = new ScheduleCategory(_clientRest);
instance.Student = new StudentCategory(_clientRest);
}
}

return instance;
}

/// <inheritdoc cref="OrioksClient"/>
private OrioksClient(OrioksAccount account)
private OrioksClient()
{
var cookies = new CookieContainer();
var handler = new HttpClientHandler();
Expand Down

0 comments on commit 9f244ad

Please sign in to comment.