Skip to content

Instantly share code, notes, and snippets.

@albertoxamin
Created February 8, 2019 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertoxamin/6aa919668081a527181bf1366bcb2ff1 to your computer and use it in GitHub Desktop.
Save albertoxamin/6aa919668081a527181bf1366bcb2ff1 to your computer and use it in GitHub Desktop.
ITIS Barsanti Backend
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using Windows.Networking.Connectivity;
using System.Net.Http;
using System.Xml.Linq;
using HtmlAgilityPack;
using static ITISBarsantiUWP.Backend.Models;
namespace ITISBarsantiUWP.Backend
{
public class Client
{
public string Classe
{
get
{
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
if (roamingSettings.Values.ContainsKey("Classe"))
return roamingSettings.Values["Classe"].ToString();
else
return "";
}
set
{
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values["Classe"] = value;
}
}
public async Task<List<CirRSS>> GetCircolari()
{
if (NetworkInformation.GetInternetConnectionProfile() == null)
{
Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("Connettiti ad internet!");
await messageDialog.ShowAsync();
return null;
}
string pag;
using (var pagin = new HttpClient())
{
pag = await pagin.GetStringAsync(new Uri("http://www.barsanti.gov.it/feed/", UriKind.Absolute));
}
string res = pag;
IEnumerable<CirRSS> RSSData = from rss in XElement.Parse(res).Descendants("item")
select new CirRSS
{
Titolo = rss.Element("title").Value.ToUpper(),
data = rss.Element("pubDate").Value,
desc = rss.Element("description").Value,
link = rss.Element("link").Value,
};
return RSSData.ToList();
}
public async Task<List<List<Materia>>> GetOrario()
{
if (NetworkInformation.GetInternetConnectionProfile() == null)
{
Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("Connettiti ad internet!");
await messageDialog.ShowAsync();
return null;
}
string pag;
using (var pagin = new HttpClient())
{
//puntava a http://orario.barsanti.gov.it/ o e veniva aggiornato causa gli orari provvisori
pag = await pagin.GetStringAsync(new Uri("http://xamin.it/orariBarsanti.txt", UriKind.Absolute));
//Questo ottiene veramente l'orario
pag = await pagin.GetStringAsync(new Uri(pag+Classe, UriKind.Absolute));
}
List<List<Materia>> giorni = new List<List<Materia>>();
HtmlDocument html = new HtmlDocument();
html.LoadHtml(pag);
int[] skip = new int[7];
for (int i = 0; i < 7; i++)
skip[i] = 1;
bool notSkip = true;
List<Materia> lun = new List<Materia>();
for (int i = 2; i < 7; i++)
{
string now = ".//tr[" + i + "]/td[" + skip[i] + "]";
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
lun.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
try
{
if (html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[1].InnerText.Trim().Replace("&nbsp;", "") != "")//2
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[1].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[1].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[1].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
lun.Add(m);
}
}
catch { }
giorni.Add(lun);
for (int i = 0; i < 7; i++)
skip[i]++;
notSkip = true;
List<Materia> mar = new List<Materia>();
for (int i = 2; i < 7; i++)
{
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
mar.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
try
{
if (html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[2].InnerText.Trim().Replace("&nbsp;", "") != "")//3
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[2].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[2].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[2].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
mar.Add(m);
}
}
catch { }
giorni.Add(mar);
for (int i = 0; i < 7; i++)
skip[i]++;
notSkip = true;
List<Materia> mer = new List<Materia>();
for (int i = 2; i < 7; i++)
{
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
mer.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
try
{
if (html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[3].InnerText.Trim().Replace("&nbsp;", "") != "")//4
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[3].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[3].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[3].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
mer.Add(m);
}
}
catch { }
giorni.Add(mer);
for (int i = 0; i < 7; i++)
skip[i]++;
notSkip = true;
List<Materia> gio = new List<Materia>();
for (int i = 2; i < 7; i++)
{
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
gio.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
try
{
if (html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[4].InnerText.Trim().Replace("&nbsp;", "") != "")//5
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[4].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[4].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[4].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
gio.Add(m);
}
}
catch { }
giorni.Add(gio);
for (int i = 0; i < 7; i++)
skip[i]++;
notSkip = true;
List<Materia> ven = new List<Materia>();
for (int i = 2; i < 7; i++)
{
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
ven.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
try
{
if (html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[6].InnerText.Trim().Replace("&nbsp;", "") != "")//6
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[6].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[6].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[6].Descendants("td").ToArray<HtmlNode>()[6].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
ven.Add(m);
}
}
catch { }
giorni.Add(ven);
for (int i = 0; i < 7; i++)
skip[i]++;
notSkip = true;
List<Materia> sab = new List<Materia>();
for (int i = 2; i < 7; i++)
{
try
{
if (notSkip)
{
Materia m = new Materia();
m.colore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("bgcolor", null);
m.desc = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].InnerText.Trim().Replace("&nbsp;", "");
m.ore = html.DocumentNode.Descendants("tr").ToArray<HtmlNode>()[i - 1].Descendants("td").ToArray<HtmlNode>()[skip[i]].GetAttributeValue("rowspan", null);
m.alt = (80 * Convert.ToInt32(m.ore)).ToString();
sab.Add(m);
if (m.ore == "2")
{
notSkip = false;
skip[i + 1]--;
}
}
else
notSkip = true;
}
catch { }
}
giorni.Add(sab);
return giorni;
}
public async Task<string> GetCircolarePDF(string Link)
{
if (NetworkInformation.GetInternetConnectionProfile() == null)
{
Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("Connettiti ad internet!");
await messageDialog.ShowAsync();
return null;
}
string result;
using (var pagin = new HttpClient())
{
result = await pagin.GetStringAsync(new Uri(Link, UriKind.Absolute));
}
HtmlDocument html = new HtmlDocument();
html.LoadHtml(result);
return html.DocumentNode.Descendants("a").Where(x => x.Attributes["href"].Value.Contains("http://www.barsanti.gov.it/wp-content/uploads/")).FirstOrDefault().GetAttributeValue("href", null);
}
public async Task<List<HrefText>> GetClassi()
{
if (NetworkInformation.GetInternetConnectionProfile() == null)
{
Windows.UI.Popups.MessageDialog messageDialog = new Windows.UI.Popups.MessageDialog("Connettiti ad internet!");
await messageDialog.ShowAsync();
return null;
}
List<HrefText> cir = new List<HrefText>();
string pag;
using (var pagin = new HttpClient())
{
pag = await pagin.GetStringAsync(new Uri("http://xamin.it/orariBarsanti.txt", UriKind.Absolute));
pag = await pagin.GetStringAsync(new Uri(pag, UriKind.Absolute));
}
HtmlDocument html = new HtmlDocument();
html.LoadHtml(pag);
foreach (HtmlNode n in html.DocumentNode.Descendants("a"))
{
HrefText hr = new HrefText();
hr.link = n.GetAttributeValue("href", null);
hr.text = n.InnerText.Trim();
cir.Add(hr);
}
return cir;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ITISBarsantiUWP.Backend
{
public class Models
{
public class Materia
{
public string colore { get; set; }
public string ore { get; set; }
public string desc { get; set; }
//La altezza della cella non vi serve molto probabilmente
public string alt { get; set; }
}
public class CirRSS
{
public string Titolo { get; set; }
public string data { get; set; }
public string desc { get; set; }
public string link { get; set; }
}
public class HrefText
{
public string text { get; set; }
public string link { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment