using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using xNet;
namespace MusicAppMP3
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool isCheckVN;
private bool isCheckEU;
private bool isCheckKO;
private ObservableCollection<Song> listVN;
private ObservableCollection<Song> listEU;
private ObservableCollection<Song> listKO;
private Song currentSong;
public bool IsCheckVN { get => isCheckVN; set { isCheckVN = value; lsbTopSongs.ItemsSource = ListVN; isCheckEU = false; isCheckKO = false; OnPropertyChanged("IsCheckVN"); OnPropertyChanged("IsCheckEU"); OnPropertyChanged("IsCheckKO"); } }
public bool IsCheckEU { get => isCheckEU; set { isCheckEU = value; lsbTopSongs.ItemsSource = ListEU; isCheckVN = false; isCheckKO = false; OnPropertyChanged("IsCheckVN"); OnPropertyChanged("IsCheckEU"); OnPropertyChanged("IsCheckKO"); } }
public bool IsCheckKO { get => isCheckKO; set { isCheckKO = value; lsbTopSongs.ItemsSource = ListKO; isCheckEU = false; isCheckVN = false; OnPropertyChanged("IsCheckVN"); OnPropertyChanged("IsCheckEU"); OnPropertyChanged("IsCheckKO"); } }
public ObservableCollection<Song> ListVN { get => listVN; set => listVN = value; }
public ObservableCollection<Song> ListEU { get => listEU; set => listEU = value; }
public ObservableCollection<Song> ListKO { get => listKO; set => listKO = value; }
public Song CurrentSong { get => currentSong; set => currentSong = value; }
public MainWindow()
{
InitializeComponent();
ucSongInfo.BackToMain += UcSongInfo_BackToMain;
this.DataContext = this;
ListVN = new ObservableCollection<Song>();
ListEU = new ObservableCollection<Song>();
ListKO = new ObservableCollection<Song>();
CrawlBXH();
IsCheckVN = true;
}
void CrawlBXH()
{
HttpRequest http = new HttpRequest();
string htmlBXH = http.Get(@"http://mp3.zing.vn/bang-xep-hang/index.html").ToString();
string bxhPattern = @"<div class=""box-chart-ov bordered non-bg-rank"">(.*?)</ul>";
var listBXH = Regex.Matches(htmlBXH, bxhPattern, RegexOptions.Singleline);
string bxhVN = listBXH[0].ToString();
AddSongToListSong(ListVN, bxhVN);
string bxhEU = listBXH[1].ToString();
AddSongToListSong(ListEU, bxhEU);
string bxhKO = listBXH[2].ToString();
AddSongToListSong(ListKO, bxhKO);
}
void AddSongToListSong(ObservableCollection<Song> listSong, string html)
{
var listSongHTML = Regex.Matches(html, @"<li>(.*?)</li>", RegexOptions.Singleline);
for (int i = 0; i < listSongHTML.Count; i++)
{
var songandsinger = Regex.Matches(listSongHTML[i].ToString(), @"<a\s\S*\stitle=""(.*?)""", RegexOptions.Singleline);
string songString = songandsinger[0].ToString();
int indexSong = songString.IndexOf("title=\"");
string songName = songString.Substring(indexSong, songString.Length - indexSong - 1).Replace("title=\"", "");
string singerString = songandsinger[1].ToString();
int indexSinger = singerString.IndexOf("title=\"");
string singerName = singerString.Substring(indexSinger, singerString.Length - indexSinger - 1).Replace("title=\"", "");
int indexURL = songString.IndexOf("href=\"");
string URL = songString.Substring(indexURL, indexSong - indexURL - 2).Replace("href=\"", "");
HttpRequest http = new HttpRequest();
string htmlSong = http.Get(@"http://mp3.zing.vn" + URL).ToString();
var lirycs = Regex.Matches(htmlSong, @"<p class=""fn-wlyrics fn-content""(.*?)</p>", RegexOptions.Singleline);
string tempLiryc = "Chưa có lyric";
if (lirycs.Count > 0)
{
tempLiryc = lirycs[0].ToString();
string tempToCut = tempLiryc.Substring(0, tempLiryc.IndexOf('>') + 1);
tempLiryc = tempLiryc.Replace(tempToCut, "").Replace("<br>", "").Replace("</p>", "");
}
string getJsonURL = Regex.Match(htmlSong, @"<div id=""zplayerjs-wrapper"" class=""player"" data-xml=""(.*?)""", RegexOptions.Singleline).Value.Replace(@"<div id=""zplayerjs-wrapper"" class=""player"" data-xml=""", "").Replace("\"","");
string jsonInfo = http.Get(@"http://mp3.zing.vn" + getJsonURL).ToString();
JObject jObject = JObject.Parse(jsonInfo);
string name = jObject["data"][0]["name"].ToString();
string downloadURL = jObject["data"][0]["source_list"].ToString();
downloadURL = downloadURL.Substring(downloadURL.IndexOf("http"), downloadURL.IndexOf(",") - downloadURL.IndexOf("http") - 1);
string photoURL = jObject["data"][0]["cover"].ToString();
string savePath = AppDomain.CurrentDomain.BaseDirectory + "Song\\" + songName + ".mp3";
listSong.Add(new Song() { SingerName = singerName, SongName = songName, SongURL = URL, STT = i + 1, Lyric=tempLiryc, DownloadURL=downloadURL, PhotoURL = photoURL, SavePath = savePath });
}
}
private void UcSongInfo_BackToMain(object sender, EventArgs e)
{
gridTop10.Visibility = Visibility.Visible;
ucSongInfo.Visibility = Visibility.Hidden;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Song song = (sender as Button).DataContext as Song;
CurrentSong = song;
gridTop10.Visibility = Visibility.Hidden;
ucSongInfo.Visibility = Visibility.Visible;
ucSongInfo.SongInfo = song;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string newName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(newName));
}
}
void ChangeToNextWSong(ObservableCollection<Song> listSong, int position, int addCount)
{
int index = listSong.IndexOf(CurrentSong);
if (index == position)
{
return;
}
else
{
CurrentSong = listSong[index + addCount];
ucSongInfo.SongInfo = CurrentSong;
}
}
private void ucSongInfo_PrevioursClicked(object sender, EventArgs e)
{
if (IsCheckVN)
{
ChangeToNextWSong(ListVN, 0, -1);
}
else if (IsCheckEU)
{
ChangeToNextWSong(ListEU, 0, -1);
}
else
{
ChangeToNextWSong(ListKO, 0, -1);
}
}
private void ucSongInfo_NextClicked(object sender, EventArgs e)
{
if (IsCheckVN)
{
ChangeToNextWSong(ListVN, 9, 1);
}
else if (IsCheckEU)
{
ChangeToNextWSong(ListEU, 9, 1);
}
else
{
ChangeToNextWSong(ListKO, 9, 1);
}
}
}
}
Mong a làm thêm app Android java hoặc Kotlin về chủ đề app nhạc này ạ.
không có Project à anh
mong cái bạn vote cho mình được 100đ với cần bài viết này lắm đề làm đồ án mà kiếm 100đ khó quá huhu
mong cái bạn vote cho mình được 100đ với cần bài viết này lắm đề làm đồ án mà kiếm 100đ khó quá huhu
Bài sau đâu rồi anh.. =))))