Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9. using System.Diagnostics;
  10. using System.Web.Script.Serialization;
  11. using System.Windows.Forms;
  12. using HundredMilesSoftware.UltraID3Lib;
  13. using AudioUtils;
  14. using System.Net;
  15.  
  16. namespace mp3taghandler
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.         string currdirectory = null;
  25.         RootObject rootjson;
  26.         string[] currpaths;
  27.         int i = 0;
  28.         int jsonindex = 0;
  29.  
  30.         private void nextbutton_Click(object sender, EventArgs e)
  31.         {
  32.             try
  33.             {
  34.                 if (i < currpaths.Length)
  35.                 {
  36.                     if (currpaths[i].EndsWith(".mp3"))
  37.                     {
  38.                         UltraID3 mp3 = new UltraID3();
  39.                         if (currpaths[i] != null)
  40.                         {
  41.                             mp3.Read(currpaths[i]);
  42.                             FileInfo info = new FileInfo(currpaths[i]);
  43.                            
  44.                             if (neugenre.Text != string.Empty)
  45.                                 mp3.ID3v23Tag.Genre = neugenre.Text;
  46.                             if(neuautor.Text != string.Empty)
  47.                                 mp3.ID3v23Tag.Artist = neuautor.Text;
  48.                             if (neutitel.Text != string.Empty)
  49.                                 mp3.ID3v23Tag.Title = neutitel.Text;
  50.                             else
  51.                                 mp3.ID3v23Tag.Title = info.Name.Remove(info.Name.Length - 4, 4);
  52.                             mp3.ID3v23Tag.WriteFlag = true;
  53.                            
  54.                             mp3.Write();
  55.  
  56.                             if (neutitel.Text != string.Empty && neutitel.Text.ToLower() + ".mp3" != info.Name.ToLower())
  57.                             {
  58.                                 File.Copy(info.FullName,currdirectory + "\\" + neutitel.Text + ".mp3");
  59.                                 File.Delete(info.FullName);
  60.                             }
  61.                             i++;
  62.  
  63.                             getcurrpath();
  64.  
  65.                             neuautor.Text = string.Empty;
  66.                             neugenre.Text = string.Empty;
  67.                             neutitel.Text = string.Empty;
  68.                            
  69.                         }
  70.                         else
  71.                             i++;
  72.                     }
  73.                     else
  74.                         i++;
  75.                 }
  76.             }
  77.             catch (Exception ex)
  78.             {
  79.                 MessageBox.Show(ex.Message);
  80.                 i++;
  81.             }
  82.         }
  83.  
  84.         private void initbutton_Click(object sender, EventArgs e)
  85.         {
  86.             FolderBrowserDialog dialog = new FolderBrowserDialog();
  87.             if (dialog.ShowDialog() == DialogResult.OK)
  88.             {
  89.                 currdirectory = dialog.SelectedPath;
  90.                 currpaths = Directory.GetFiles(currdirectory);
  91.                 i = 0;
  92.  
  93.                 foreach (String s in currpaths)
  94.                 {
  95.                     String[] temp = s.Split('\\');
  96.                     treeView1.Nodes.Add(new TreeNode(temp[temp.Length-1]));
  97.                 }
  98.                 getcurrpath();
  99.  
  100.             }
  101.         }
  102.  
  103.         private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  104.         {
  105.             i = e.Node.Index;
  106.             getcurrpath();
  107.         }
  108.  
  109.         private void getcurrpath()
  110.         {
  111.             UltraID3 mp3 = new UltraID3();
  112.             mp3.Read(currpaths[i]);
  113.             FileInfo info = new FileInfo(currpaths[i]);
  114.             alttitel.Text = info.Name;
  115.             altautor.Text = mp3.ID3v23Tag.Artist;
  116.             altGenre.Text = mp3.ID3v23Tag.Genre;
  117.             textBoxquery.Text = info.Name.Substring(0,info.Name.Length-4);
  118.             try
  119.             {
  120.                 treeView1.SelectedNode = treeView1.Nodes[i];
  121.             }
  122.             catch(ArgumentOutOfRangeException e)
  123.             {
  124.                 Debug.WriteLine(e.Message);
  125.             }
  126.         }
  127.  
  128.         private RootObject getroot(int page,string query)
  129.         {
  130.             RootObject root = null;
  131.             query.Replace(' ', '+');
  132.             string url = "https://api.discogs.com/database/search?q="+query+ "&type=release&per_page=3&page="+page.ToString();
  133.  
  134.             WebClient client = new WebClient();
  135.  
  136.             client.Headers.Add(HttpRequestHeader.Host, "api.discogs.com");
  137.             client.Headers.Add(HttpRequestHeader.Authorization, "Discogs key=fvjuYhGjrQYIGmdeSVZd, secret=UyHaEdePsdCzGcINqZvEoJPBfrzcwnbn");
  138.             client.Headers.Add(HttpRequestHeader.UserAgent, "MP3Taghandler/0.2 +phil-susen@gmx.de");
  139.  
  140.             Stream stream = client.OpenRead(url);
  141.             StreamReader objReader = new StreamReader(stream);
  142.             string response = objReader.ReadToEnd();
  143.  
  144.             root = new JavaScriptSerializer().Deserialize<RootObject>(response);
  145.  
  146.             return root;
  147.         }
  148.  
  149.         private void setProposal(int index)
  150.         {
  151.             try
  152.             {
  153.                 neutitel.Text = rootjson.results[index].title.Split('-')[1].Trim(' ');
  154.                 neuautor.Text = rootjson.results[index].title.Split('-')[0].Trim(' ');
  155.                 string genres = string.Empty;
  156.                 foreach (string genre in rootjson.results[index].genre)
  157.                 {
  158.                     genres += genre;
  159.                     genres += " ";
  160.                 }
  161.                 neugenre.Text = genres;
  162.             }
  163.             catch(ArgumentOutOfRangeException e)
  164.             {
  165.                 MessageBox.Show("There are no more Proposals. Try changing query.");
  166.             }
  167.         }
  168.  
  169.         private void reloadbutton_Click(object sender, EventArgs e)
  170.         {
  171.             rootjson = getroot(1, textBoxquery.Text);
  172.             jsonindex = 0;
  173.             setProposal(0);
  174.         }
  175.  
  176.         private void lastproposal_Click(object sender, EventArgs e)
  177.         {
  178.             if (jsonindex > 0)
  179.                 jsonindex--;
  180.             else
  181.                 MessageBox.Show("You are at the Start");
  182.             int site = jsonindex / 3 + 1;
  183.             int index = jsonindex % 3;
  184.             if (rootjson.pagination.page != site)
  185.                 rootjson = getroot(site, textBoxquery.Text);
  186.             setProposal(index);
  187.         }
  188.  
  189.         private void nextproposal_Click(object sender, EventArgs e)
  190.         {
  191.             if (jsonindex < rootjson.pagination.items)
  192.                 jsonindex++;
  193.             if (jsonindex < rootjson.pagination.items)
  194.             {
  195.                 int site = jsonindex / 3 + 1;
  196.                 int index = jsonindex % 3;
  197.                 if (rootjson.pagination.page != site)
  198.                     rootjson = getroot(site, textBoxquery.Text);
  199.                 setProposal(index);
  200.             }
  201.             else
  202.             {
  203.                 MessageBox.Show("You are through all Proposals");
  204.             }
  205.         }
  206.  
  207.         private void skipbutton_Click(object sender, EventArgs e)
  208.         {
  209.             i++;
  210.             getcurrpath();
  211.         }
  212.  
  213.         private void button1_Click_1(object sender, EventArgs e)
  214.         {
  215.             waveControl1.Filename = "C:\\Users\\phil-\\Downloads\\Best_Of_You.wav";
  216.             waveControl1.Read();
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement