#129 importing donations data from html text of donations page

This commit is contained in:
Hamid Reza Mohammadi 2021-06-04 22:05:16 +04:30
parent 1b7427fa89
commit a0dacaef55
7 changed files with 116 additions and 18 deletions

View File

@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RMuseum.Models.Auth.Memory;
using RMuseum.Services;
using System;
using System.Net;
using System.Threading.Tasks;
namespace RMuseum.Controllers
{
[Produces("application/json")]
[Route("api/donations")]
public class DonationAccountingController : Controller
{
/// <summary>
/// one time import
/// </summary>
/// <returns></returns>
[HttpPost("onetimeimport")]
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.Donations)]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> Import()
{
try
{
var res = await _donationService.InitializeRecords();
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok();
}
catch (Exception exp)
{
return BadRequest(exp.ToString());
}
}
/// <summary>
/// donation service
/// </summary>
private readonly IDonationService _donationService;
public DonationAccountingController(IDonationService donationService)
{
_donationService = donationService;
}
}
}

View File

@ -43,7 +43,7 @@ namespace RMuseum.Models.Accounting
public string DonorName { get; set; }
/// <summary>
/// donor link
/// donor link (unused)
/// </summary>
public string DonorLink { get; set; }

View File

@ -88,6 +88,11 @@ namespace RMuseum.Models.Auth.Memory
/// </summary>
public const string Banners = "banners";
/// <summary>
/// donations
/// </summary>
public const string Donations = "donations";
/// <summary>
/// list of forms and their permissions
@ -161,9 +166,12 @@ namespace RMuseum.Models.Auth.Memory
new SecurableItemOperation(ModerateOperationShortName, "مدیریت حاشیه‌ها", false),
new SecurableItemOperation(ModifyOperationShortName, "ویرایش محتوا", false),
new SecurableItemOperation(Banners, "مدیریت آگاهی‌ها", false),
new SecurableItemOperation(Donations, "مدیریت کمکهای مالی", false),
}
},
});
return lst.ToArray();
}

View File

@ -584,6 +584,17 @@
<param name="auditLogService">
</param>
</member>
<member name="M:RMuseum.Controllers.DonationAccountingController.Import">
<summary>
one time import
</summary>
<returns></returns>
</member>
<member name="F:RMuseum.Controllers.DonationAccountingController._donationService">
<summary>
donation service
</summary>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoets">
<summary>
get list of published poets without their biography
@ -1561,7 +1572,7 @@
</member>
<member name="P:RMuseum.Models.Accounting.GanjoorDonation.DonorLink">
<summary>
donor link
donor link (unused)
</summary>
</member>
<member name="P:RMuseum.Models.Accounting.GanjoorDonation.Remaining">
@ -2382,6 +2393,11 @@
manage footer bannaers
</summary>
</member>
<member name="F:RMuseum.Models.Auth.Memory.RMuseumSecurableItem.Donations">
<summary>
donations
</summary>
</member>
<member name="P:RMuseum.Models.Auth.Memory.RMuseumSecurableItem.Items">
<summary>
list of forms and their permissions
@ -6734,6 +6750,17 @@
<param name="checkJobs"></param>
<returns></returns>
</member>
<member name="T:RMuseum.Services.IDonationService">
<summary>
donation service
</summary>
</member>
<member name="M:RMuseum.Services.IDonationService.InitializeRecords">
<summary>
parse html of https://ganjoor.net/donate/ and fill the records
</summary>
<returns></returns>
</member>
<member name="T:RMuseum.Services.IGanjoorService">
<summary>
Ganjoor Poems Content Privider Service

View File

@ -0,0 +1,17 @@
using RSecurityBackend.Models.Generic;
using System.Threading.Tasks;
namespace RMuseum.Services
{
/// <summary>
/// donation service
/// </summary>
public interface IDonationService
{
/// <summary>
/// parse html of https://ganjoor.net/donate/ and fill the records
/// </summary>
/// <returns></returns>
Task<RServiceResult<bool>> InitializeRecords();
}
}

View File

@ -13,7 +13,7 @@ namespace RMuseum.Services.Implementation
/// <summary>
/// donation service
/// </summary>
public class DonationService
public class DonationService : IDonationService
{
/// <summary>
@ -68,31 +68,31 @@ namespace RMuseum.Services.Implementation
DonationPageRow row = new DonationPageRow();
nStartIndex += "<td class=\"ddate\">".Length;
row.Date = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex));
row.Date = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex) - nStartIndex);
nStartIndex = htmlText.IndexOf("<td class=\"damount\">", nStartIndex);
if (nStartIndex == -1)
return new RServiceResult<bool>(false, $"{rowNumber} : damount");
nStartIndex += "<td class=\"damount\">".Length;
row.Amount = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex));
row.Amount = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex) - nStartIndex);
nStartIndex = htmlText.IndexOf("<td class=\"ddonator\">", nStartIndex);
if (nStartIndex == -1)
return new RServiceResult<bool>(false, $"{rowNumber} : ddonator");
nStartIndex += "<td class=\"ddonator\">".Length;
row.Donor = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex));
row.Donor = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex) - nStartIndex);
nStartIndex = htmlText.IndexOf("<td class=\"dusage\">", nStartIndex);
if (nStartIndex == -1)
return new RServiceResult<bool>(false, $"{rowNumber} : dusage");
nStartIndex += "<td class=\"dusage\">".Length;
row.Usage = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex));
row.Usage = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex) - nStartIndex);
nStartIndex = htmlText.IndexOf("<td class=\"drem\">", nStartIndex);
if (nStartIndex == -1)
return new RServiceResult<bool>(false, $"{rowNumber} : drem");
nStartIndex += "<td class=\"drem\">".Length;
row.Remaining = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex));
row.Remaining = htmlText.Substring(nStartIndex, htmlText.IndexOf("</td>", nStartIndex) - nStartIndex);
rows.Add(row);
@ -121,25 +121,18 @@ namespace RMuseum.Services.Implementation
rows[i].Remaining.ToEnglishNumbers() != "0"
)
{
donation.Remaining = decimal.Parse(rows[i].Remaining.Replace("٬", "").Replace("تومان", "").Trim());
}
if(donation.DonorName.IndexOf("href") != -1)
{
nStartIndex = donation.DonorName.IndexOf("href") + "href".Length + 1;
donation.DonorLink = donation.DonorName.Substring(nStartIndex, donation.DonorName.IndexOf("</a>") - nStartIndex);
donation.DonorName = donation.DonorName.Substring(0, donation.DonorName.IndexOf("<a")) + donation.DonorName.Replace("</a>", "").Substring(donation.DonorName.IndexOf(">") + 1);
donation.Remaining = decimal.Parse(rows[i].Remaining.Replace("٬", "").Replace("تومان", "").Trim().ToEnglishNumbers());
}
if(donation.AmountString.Contains("تومان"))
{
donation.Amount = decimal.Parse(donation.AmountString.Replace("٬", "").Replace("تومان", "").Trim());
donation.Amount = decimal.Parse(donation.AmountString.Replace("٬", "").Replace("تومان", "").Trim().ToEnglishNumbers());
donation.Unit = "تومان";
}
if (donation.AmountString.Contains("دلار"))
{
donation.Amount = decimal.Parse(donation.AmountString.Replace("٬", "").Replace("دلار", "").Trim());
donation.Amount = decimal.Parse(donation.AmountString.Replace("٬", "").Replace("دلار", "").Trim().ToEnglishNumbers());
donation.Unit = "دلار";
}

View File

@ -277,6 +277,9 @@ namespace RMuseum
//site banner service
services.AddTransient<ISiteBannersService, SiteBannersService>();
//donation service
services.AddTransient<IDonationService, DonationService>();
//upload limit for IIS
services.Configure<IISServerOptions>(options =>
{