JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Collections.Generic;
using System.Text;
using Shared;
using Shared.SimpleControl.R;
using Shared.SimpleControl.Phone.Music;
using Shared.SimpleControl.Phone;
using Shared.SimpleControl;
 
namespace SmartHome.UI.SimpleControl.Phone.Music
{
    class A31Search : FrameLayout
    {
        /// <summary>
        /// 输入文本
        /// </summary>
        EditText devicename;
 
        public void Show(A31MusicModel a31)
        {
            BackgroundColor = 0xffcccccc;
 
            Dialog window = new Dialog();
            window.Show();
 
            var frameLayout = new FrameLayout
            {
                Height = Application.GetRealHeight(250),
                Y = Application.GetRealHeight(300),
                BackgroundColor = 0xffffffff,
            };
            window.AddChidren(frameLayout);
 
            var name = new Button
            {
                //TextID = MyInternationalizationString.cancel,
                Text = "输入关键字:",
                TextColor = 0xff000000,
                TextSize = 18,
                X = Application.GetRealWidth(10),
                Y = Application.GetRealHeight(20),
                Height = Application.GetRealHeight(75),
                Width = Application.GetRealWidth(200),
                TextAlignment = TextAlignment.CenterLeft,
            };
            frameLayout.AddChidren(name);
 
            devicename = new EditText
            {
                Width = Application.GetRealWidth(620),
                Height = Application.GetRealHeight(80),
                Y = Application.GetRealHeight(80),
                X = Application.GetRealWidth(10),
                Gravity = Gravity.CenterVertical,
                BackgroundColor = 0xffcccccc,
                TextColor = 0xff000000,
            };
            frameLayout.AddChidren(devicename);
 
            var searchname = new Button
            {
                //TextID = MyInternationalizationString.cancel,
                Text = "搜索",
                TextColor = 0xff000000,
                TextSize = 18,
                Width = Application.GetRealHeight(120),
                Height = Application.GetRealHeight(80),
                X = Application.GetRealWidth(350),
                Y = Application.GetRealHeight(170),
            };
            frameLayout.AddChidren(searchname);
            searchname.MouseUpEventHandler += (sender, e) =>
            {
                searchname.TextColor = 0xff000000;
                MainPage.Loading.Start(Language.StringByID(MyInternationalizationString.load) + "...");
                System.Threading.Tasks.Task.Run(() =>
                {
                    var airablelist = serarchSong(devicename.Text);
                    if (airablelist == null)
                    {
                        return;
                    }
                    Application.RunOnMainThread(() =>
                    {
                        MainPage.Loading.Hide();
                        RemoveFromParent();
                        window.Close();
                        A31RadioList a31RadioList = new A31RadioList();
                        MainPage.MainFrameLayout.AddChidren(a31RadioList);
                        a31RadioList.show(a31, airablelist, "");
                    });
                });
            };
            searchname.MouseDownEventHandler += (sender, e) =>
            {
                searchname.TextColor = 0xffFE5E00;
            };
                var cancel = new Button
            {
                //TextID = MyInternationalizationString.cancel,
                Text = "取消",
                TextColor = 0xff333333,
                TextSize = 18,
                Width = Application.GetRealHeight(120),
                Height = Application.GetRealHeight(80),
                X = Application.GetRealWidth(500),
                Y = Application.GetRealHeight(170),
            };
            frameLayout.AddChidren(cancel);
            cancel.MouseUpEventHandler += (sender1, e1) =>
            {
                window.Close();
                RemoveFromParent();
            };
 
        }
 
 
        static string userName = "ieast";
        static string password = "60b25158ee94";
        public static Airable serarchSong(string songName)
        {
            System.IO.StringReader sr = null;
            Airable airable = null;
            Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient();
            try
            {
                webClient.Headers.Add("CONTENT-TYPE", "text/xml;charset=\"utf-8\"");
                webClient.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + password)));
 
                byte[] recevieBytes = webClient.DownloadData(new Uri("http://meta.airable.io/view/airable/radios/search?q=" + songName));
 
                sr = new System.IO.StringReader(Encoding.UTF8.GetString(recevieBytes, 0, recevieBytes.Length));
                string line = null;
                StringBuilder stringBuilder = null;
                while ((line = sr.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (stringBuilder == null)
                    {
                        if (line.EndsWith("<div class=\"col-50\">") && sr.ReadLine().Trim() == "<code>{")
                        {
                            stringBuilder = new StringBuilder();
                            stringBuilder.AppendLine("{");
                        }
                    }
                    else
                    {
                        if (line.EndsWith("</code>"))
                        {
                            stringBuilder.AppendLine("}");
                            string s = @stringBuilder.ToString().Replace(",}", "}").Replace("\"content\": []", "\"content\": \"\"");
                            airable = Newtonsoft.Json.JsonConvert.DeserializeObject<Airable>(@s);
                            break;
                        }
                        else
                        {
                            if (line.Contains("</a>"))
                            {
                                line = line.Remove(line.IndexOf("<a"), line.IndexOf("\">") - line.IndexOf("<a") + 2).Replace("</a>", "");
                            }
                            stringBuilder.AppendLine(@line);
                        }
                    }
                }
            }
            catch { }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
            return airable;
        }   
        
            
    }
}