-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCSharpDll.cs
194 lines (168 loc) · 8.04 KB
/
TestCSharpDll.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Threading.Tasks;
using System.Net;
using System.Text;
using System.IO;
namespace MusicBeePlugin
{
public partial class Plugin
{
private MusicBeeApiInterface mbApiInterface;
private PluginInfo about = new PluginInfo();
private string dataPath => mbApiInterface.Setting_GetPersistentStoragePath();
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
mbApiInterface = new MusicBeeApiInterface();
mbApiInterface.Initialise(apiInterfacePtr);
about.PluginInfoVersion = PluginInfoVersion;
about.Name = "Rate Your Genres";
about.Description = "A plugin that syncs your album genres with the genres listed on Rate Your Music. build check";
about.Author = "Slashscreen";
about.TargetApplication = ""; // the name of a Plugin Storage device or panel header for a dockable panel
about.Type = PluginType.General;
about.VersionMajor = 1; // your plugin version
about.VersionMinor = 0;
about.Revision = 1;
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
about.ConfigurationPanelHeight = 0; // height in pixels that musicbee should reserve in a panel for config settings. When set, a handle to an empty panel will be passed to the Configure function
Console.WriteLine("Initializing!");
CreateMenuItem();
return about;
}
public bool Configure(IntPtr panelHandle)
{
// save any persistent settings in a sub-folder of this path
string dataPath = mbApiInterface.Setting_GetPersistentStoragePath();
// panelHandle will only be set if you set about.ConfigurationPanelHeight to a non-zero value
// keep in mind the panel width is scaled according to the font the user has selected
// if about.ConfigurationPanelHeight is set to 0, you can display your own popup window
if (panelHandle != IntPtr.Zero)
{
Panel configPanel = (Panel)Panel.FromHandle(panelHandle);
Label prompt = new Label();
prompt.AutoSize = true;
prompt.Location = new Point(0, 0);
prompt.Text = "prompt:";
TextBox textBox = new TextBox();
textBox.Bounds = new Rectangle(60, 0, 100, textBox.Height);
configPanel.Controls.AddRange(new Control[] { prompt, textBox });
}
return false;
}
public void ReceiveNotification(string sourceFileUrl, NotificationType type) { }
// uninstall this plugin - clean up any persisted files
public void Uninstall() => Directory.Delete(Path.Combine(dataPath, "rateyourgenres"));
private void CreateMenuItem()
{
mbApiInterface.MB_AddMenuItem("mnuTools/Fetch genres for selected songs", "Begin", MenuClicked);
Console.WriteLine("Menu Item");
}
private void MenuClicked(object sender, EventArgs args)
{
Console.WriteLine("Clicked menu");
ClearLog();
string[] allFiles = { };
mbApiInterface.Library_QueryFilesEx("domain=SelectedFiles", out allFiles);
MetaDataType[] fields = {
MetaDataType.Album,
MetaDataType.AlbumArtist,
MetaDataType.Artist,
MetaDataType.TrackNo,
MetaDataType.Rating,
MetaDataType.TrackTitle,
MetaDataType.RatingLove,
};
// Group all songs by albums because we only need to get genres per album, not songs. Lots of wasted requests.
Console.WriteLine($"number of selected items: {allFiles.Length}");
var albumGroups = allFiles.GroupBy(file =>
{
mbApiInterface.Library_GetFileTags(file, fields, out var fileTags);
return fileTags[0];
});
foreach (var albumGroup in albumGroups)
{
mbApiInterface.Library_GetFileTags(albumGroup.ElementAt(0), fields, out var fileTags);
string artist = fileTags[1];
string album = fileTags[0];
string genres = GetAlbumGenres(artist, album);
ReportLogError($"Test test {album} by {artist}");
if (genres == "")
{
ReportLogError($"Could not find album {album} by {artist} - skipping.");
continue;
}
foreach(var file in albumGroup)
{
mbApiInterface.Library_SetFileTag(file, MetaDataType.Genre, genres);
mbApiInterface.Library_CommitTagsToFile(file);
}
}
}
public static List<string> ParseHTMLDoc(HtmlAgilityPack.HtmlDocument htmlDoc)
{
Console.WriteLine("Parsing document...");
var programmerLinks = htmlDoc.DocumentNode.SelectNodes("//a[contains(concat(' ', @class, ' '), ' genre ')]"); // Looking for a.genre
if (programmerLinks == null)
{
return new List<string>();
}
Console.WriteLine($"found {programmerLinks.Count} genres");
return programmerLinks.Select(link => link.InnerText).ToList();
}
private static HtmlAgilityPack.HtmlDocument CallUrl(string fullUrl)
{
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(fullUrl);
return doc;
}
string GetAlbumGenres(string artist, string album)
{
Console.WriteLine($"Getting genres for {artist} - {album}");
// All possible URLs for albums, singles, EPs
string[] urls = {
$"https://rateyourmusic.com/release/album/{Sanitize(artist)}/{Sanitize(album)}/",
$"https://rateyourmusic.com/release/album/{Sanitize(artist)}/{Sanitize(album)}-1/", // for multiple versions
$"https://rateyourmusic.com/release/single/{Sanitize(artist)}/{Sanitize(album)}/",
$"https://rateyourmusic.com/release/single/{Sanitize(artist)}/{Sanitize(album)}-1/",
$"https://rateyourmusic.com/release/ep/{Sanitize(artist)}/{Sanitize(album)}/",
$"https://rateyourmusic.com/release/ep/{Sanitize(artist)}/{Sanitize(album)}-1/",
};
HtmlAgilityPack.HtmlDocument pageData;
// If we get data, act on it
foreach (string url in urls)
{
Console.WriteLine(url);
pageData = CallUrl(url);
if (pageData != null)
{
List<string> genres = ParseHTMLDoc(pageData);
if (genres.Count > 0)
{
genres.ForEach(s => Console.WriteLine(s));
return genres.Aggregate((sum, next) => sum + ";" + next.Trim(' '));
}
}
}
Console.WriteLine("No page loaded.");
return "";
}
private static string Sanitize(string input) => input.Replace(" ", "-").Replace(".", "_").ToLower(); // could use a regex and may need to.
void ReportLogError(string text)
{
File.AppendAllText(Path.Combine(Path.Combine(dataPath, "rateyourgenres"), "log.txt"), text);
}
void ClearLog()
{
Console.WriteLine(Path.Combine(dataPath, "rateyourgenres"));
Directory.CreateDirectory(Path.Combine(dataPath, "rateyourgenres"));
File.WriteAllText(Path.Combine(Path.Combine(dataPath, "rateyourgenres"), "log.txt"), "");
}
}
}