Witam!
Piszę sobie programik, który wyświetla mi statystyki filmów youtube. Dąże do tego, bym mógł importować listę z pliku tekstowego i program pokazuje mi w kolumnach wyświetlenia, długość filmiku, łapki, na której pozycji się znajduje itd.
na razie mój program wygląda tak:
class Worker
{
public static string getSourceCode(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string sourceCode = sr.ReadToEnd();
sr.Close();
resp.Close();
return sourceCode;
}
}
private void button2_Click(object sender, EventArgs e)
{
string url = textBox3.Text; //tutaj pobieram url filmiku
string sourceCode = Worker.getSourceCode(url);
//tutaj scrapuje wyświetlenia
string sourceCode1 = sourceCode;
int WyswP = sourceCode1.IndexOf("view_count") + 13;
int WyswK = sourceCode1.IndexOf("\"", WyswP);
textBox4.Text = sourceCode1.Substring(WyswP, WyswK - WyswP);
//tutaj scrapuje łapki w górę
string sourceCode2 = sourceCode;
int startIndex = sourceCode2.IndexOf("watch-view-count");
sourceCode2 = sourceCode2.Substring(startIndex, sourceCode2.Length - startIndex);
int LapkiGoraP = sourceCode2.IndexOf("<span class=\"yt-uix-button-content\">") + 36;
int LapkiGoraK = sourceCode2.IndexOf("<", LapkiGoraP);
textBox5.Text = sourceCode2.Substring(LapkiGoraP, LapkiGoraK - LapkiGoraP);
//tutaj scrapuje łapki w dół
string sourceCode3 = sourceCode;
int startIndex2 = sourceCode3.IndexOf("like-button-renderer-dislike-button like-button-renderer-dislike-button-unclicked")+81;
sourceCode3 = sourceCode3.Substring(startIndex2, sourceCode3.Length - startIndex2);
int LapkiDolP = sourceCode3.IndexOf("<span class=\"yt-uix-button-content\">") + 36;
int LapkiDolK = sourceCode3.IndexOf("<", LapkiDolP);
textBox6.Text = sourceCode3.Substring(LapkiDolP, LapkiDolK - LapkiDolP);
}
Czyli podaję url filmiku, klikam przycisk i pokazuje mi wyświetlenia, łapki w górę i łapki w dół. Dopiero zaczynam programowanie, więc nie wiem, czy mój kod jest w ogóle optymalny. Da się to zrobić, by działało szybciej? Ma ktoś jakieś propozycje?
@@
Hmm.. jak tak teraz na to patrzę, to zamiast tworzyć kolejnych sourceCode1, sourceCode2 itd mogę mieć jeden sourceCode jako string const, więc zawsze będę mieć go całego. Zgadza się?