-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
203 lines (180 loc) · 8.24 KB
/
Main.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
195
196
197
198
199
200
201
202
203
//#define CONFIG_API_SERVER
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Web;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Text;
using Newtonsoft.Json;
using MusicBeePlugin.Api;
namespace MusicBeePlugin
{
public partial class Plugin
{
#if DEBUG
string mode = " Debug";
#else
string mode = "";
# endif
private String name = "QQ";
private MusicBeeApiInterface mbApiInterface;
private PluginInfo about = new PluginInfo();
private static CookieContainer myCookieContainer = new CookieContainer();
private string api_server = "";
public PluginInfo Initialise(IntPtr apiInterfacePtr)
{
mbApiInterface = new MusicBeeApiInterface();
mbApiInterface.Initialise(apiInterfacePtr);
about.PluginInfoVersion = PluginInfoVersion;
about.Name = name + " Music Artwork" + mode;
about.Description = "Get album cover from " + name + " music. " +
"\n从QQ音乐获取专辑封面";
about.Author = "Tumuyan";
about.TargetApplication = ""; // the name of a Plugin Storage device or panel header for a dockable panel
about.Type = PluginType.ArtworkRetrieval;
about.VersionMajor = 1; // your plugin version
about.VersionMinor = 1;
about.Revision = 0;
about.MinInterfaceVersion = MinInterfaceVersion;
about.MinApiRevision = MinApiRevision;
about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents);
#if CONFIG_API_SERVER
about.ConfigurationPanelHeight = 40; // 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
loadConfig();
#else
about.ConfigurationPanelHeight = 0;
// loadCookie("os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true;NMTID=00Om_v;", "http://music.163.com");
#endif
return about;
}
private string loadConfig()
{
// string api_server;
string dataPath = mbApiInterface.Setting_GetPersistentStoragePath() + "VGMdb_album_Config.conf";
if (File.Exists(dataPath))
api_server = File.ReadAllText(dataPath);
else
api_server = "http://vgmdb.info";
Console.WriteLine("Load api_server=" + api_server);
return api_server;
}
public bool Configure(IntPtr panelHandle)
{
// save any persistent settings in a sub-folder of this path
//string dataPath = mbApiInterface.Setting_GetPersistentStoragePath() + "VGMdb_album_Config.conf";
//if (File.Exists(dataPath))
// api_server = File.ReadAllText(dataPath);
//else
// api_server = "http://vgmdb.info";
// 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);
#if CONFIG_API_SERVER
Label prompt = new Label();
prompt.AutoSize = true;
prompt.Location = new Point(0, 6);
prompt.Text = "API Server:";
TextBox textBox = new TextBox();
textBox.Bounds = new Rectangle(prompt.Width+30, 0, 300, textBox.Height);
textBox.Text = api_server;
textBox.TextChanged += new EventHandler(textBox_TextChanged); // 绑定 TextChanged 事件
configPanel.Controls.AddRange(new Control[] { prompt, textBox });
#endif
}
return false;
}
private void textBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
string oldValue = textBox.Tag?.ToString() ?? "";
api_server = textBox.Text;
// textBox.Tag = api_server;
Console.WriteLine($"api_server changed \"{oldValue}\" -> \"{api_server}\"");
}
// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen.
// its up to you to figure out whether anything has changed and needs updating
public void SaveSettings()
{
// save any persistent settings in a sub-folder of this path
#if CONFIG_API_SERVER
string dataPath = mbApiInterface.Setting_GetPersistentStoragePath() + "VGMdb_album_Config.conf";
File.WriteAllText(dataPath, api_server);
#endif
}
// MusicBee is closing the plugin (plugin is being disabled by user or MusicBee is shutting down)
public void Close(PluginCloseReason reason)
{
// saveCookie("http://music.163.com");
// Console.WriteLine("close "+reason);
}
// uninstall this plugin - clean up any persisted files
public void Uninstall()
{
}
// receive event notifications from MusicBee
// you need to set about.ReceiveNotificationFlags = PlayerEvents to receive all notifications, and not just the startup event
public void ReceiveNotification(string sourceFileUrl, NotificationType type)
{
// perform some action depending on the notification type
switch (type)
{
case NotificationType.PluginStartup:
// perform startup initialisation
switch (mbApiInterface.Player_GetPlayState())
{
case PlayState.Playing:
case PlayState.Paused:
// ...
break;
}
break;
case NotificationType.TrackChanged:
string artist = mbApiInterface.NowPlaying_GetFileTag(MetaDataType.Artist);
// ...
break;
}
}
// return an array of lyric or artwork provider names this plugin supports
// the providers will be iterated through one by one and passed to the RetrieveLyrics/ RetrieveArtwork function in order set by the user in the MusicBee Tags(2) preferences screen until a match is found
public string[] GetProviders()
{
return new string[] { name + mode };
}
// return Base64 string representation of the artwork binary data from the requested provider
// only required if PluginType = ArtworkRetrieval
// return null if no artwork is found
public string RetrieveArtwork(string sourceFileUrl, string albumArtist, string album, string provider)
{
// 预处理输入参数,只保留一个艺术家
string[] artists = albumArtist.Split(';', ',', '\\', '/', '&');
string Artist = "";
// 似乎没用上
string artist = "";
foreach (string s1 in artists)
{// Remove Various Artists
string s = s1.ToLower().Trim().Replace(" ", "");
if (s.Equals("variousartist") || s.Equals("variousartists") || s.Equals("群星") || s.Length < 1)
continue;
artist = artist + " " + s1;
if (Artist.Length < 1)
Artist = s1;
}
// 专辑名称同样需要预处理
Console.WriteLine("RetrieveArtwork Provider = " + provider + ", Artist = " + Artist + ", album = " + album);
// return new _163().getCover(Artist, album);
// return new vgmdb().getCover(Artist, album, api_server);
return new qq().getCover(Artist, album);
// return new DoubanApi().getCover(Artist, album);
}
}
}