/
Wiki.cs
189 lines (161 loc) · 6.1 KB
/
Wiki.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using LyricsEngine.org.lyricwiki;
using LyricsEngine;
namespace MyLyricWiki
{
public class Wiki
{
private LyricWiki lyricWiki;
private LyricsResult lyricsResult = null;
private string artist = "";
private string title = "";
private int noOfTries = 0;
public static bool Abort = false;
private string[] commonReplacementsLyricWiki = new string[16] { "<br>", "", "<BR>", "", "?d", "'d", "?s", "'s", "?t", "'t", "?v", "'v", "?e", "'e", "?r", "'r" };
private string[] commonRemoveFromLyricWiki = new string[2] { "[[category", "[[Category" };
public Wiki()
{
lyricWiki = new LyricWiki();
}
delegate LyricsResult DelegateClass(string artist, string title);
private bool searchForWiki(string artist, string title)
{
DelegateClass del = lyricWiki.getSongResult;
IAsyncResult ar = del.BeginInvoke(this.artist, this.title, null, null);
while (noOfTries < 15)
{
// If the user has aborted stop the search and return (false)
if (Abort)
return false;
if (ar.AsyncWaitHandle.WaitOne(0, true))
{
lyricsResult = del.EndInvoke(ar);
break;
}
else
{
// if we don't allow this pause of 2 sec the webservice behaves in a strange maneur
Thread.Sleep(2000);
}
++noOfTries;
}
if (lyricsResult != null && isLyric(lyricsResult.lyrics))
{
return true;
}
else
{
noOfTries = 0;
return false;
}
}
public string getLyricAsynch(string artist, string title)
{
this.artist = artist;
this.title = title;
try
{
// Step 1: search title as it is
if (Abort == false && searchForWiki(this.artist, this.title))
{
return makeLyricFit(lyricsResult.lyrics);
}
// Step 2: search with parentheses and other disturbance removed
optimizeString(ref this.artist, ref this.title);
if (searchForWiki(this.artist, this.title))
{
return (lyricsResult.lyrics);
}
// Step 3: search with altered and strings if any
this.artist = LyricUtil.changeAnd_and_and(this.artist);
this.title = LyricUtil.changeAnd_and_and(this.title);
if (searchForWiki(this.artist, this.title))
{
return (lyricsResult.lyrics);
}
// final step: return "Not found" if no lyric found
return "Not found";
}
catch (Exception e)
{
//System.Windows.Forms.MessageBox.Show(e.ToString());
//System.Windows.Forms.MessageBox.Show(lyricsResult.);
Console.Write(e.Message);
return "";
}
}
public string getLyricSynchron(string artist, string title)
{
this.artist = artist;
this.title = title;
try
{
// Step 1: search title as it is
lyricsResult = lyricWiki.getSong(this.artist, this.title);
if (isLyric(lyricsResult.lyrics))
{
return lyricsResult.lyrics;
}
//Thread.Sleep(1000);
//// Step 2: search with parentheses and other disturbance removed
//optimizeString(ref this.artist, ref this.title);
//lyricsResult = lyricWiki.getSongResult(this.artist, this.title);
//if (isLyric(lyricsResult.lyrics))
//{
// return lyricsResult.lyrics;
//}
//Thread.Sleep(1000);
//// Step 3: search with altered and strings if any
//this.artist = LyricUtil.changeAnd_and_and(this.artist);
//this.title = LyricUtil.changeAnd_and_and(this.title);
//lyricsResult = lyricWiki.getSongResult(this.artist, this.title);
//if (isLyric(lyricsResult.lyrics))
//{
// return lyricsResult.lyrics;
//}
//Thread.Sleep(3000);
// final step: return "Not found" if no lyric found
return "Not found";
}
catch (Exception e)
{
//System.Windows.Forms.MessageBox.Show(e.ToString());
//System.Windows.Forms.MessageBox.Show(lyricsResult.);
Console.Write(e.Message);
return "";
}
}
private bool isLyric(string lyric)
{
if (lyricsResult != null && !lyricsResult.lyrics.Equals("Not found") && !lyricsResult.lyrics.Equals(""))
return true;
else
return false;
}
private void optimizeString(ref string artist, ref string title)
{
LyricUtil.trimForParenthesis(ref artist);
LyricUtil.trimForParenthesis(ref title);
}
private string makeLyricFit(string lyric)
{
for (int i = 0; i < commonReplacementsLyricWiki.Length; i = i + 2)
{
lyric = lyric.Replace(commonReplacementsLyricWiki[i], commonReplacementsLyricWiki[i + 1]);
}
for (int i = 0; i < commonRemoveFromLyricWiki.Length; i++)
{
int index = lyric.IndexOf(commonRemoveFromLyricWiki[i]);
if (index != -1)
{
lyric = lyric.Substring(0, index);
break;
}
}
return lyric;
}
}
}