-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlbumInfo.cs
168 lines (141 loc) · 6.72 KB
/
AlbumInfo.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace WrekRip {
public class AlbumInfo {
//change this to real database when done
private const string DB_GET_URL = "http://opdesk.wrek.org/automation/wrekrip_get.php";
private const string DB_ADD_URL = "http://opdesk.wrek.org/automation/wrekrip_av_add.php";
public static string UserID;
public static string Password;
public static string SessionCookie;
public int ID { get; set; }
public string Artist { get; protected set; }
public string Title { get; protected set; }
public string Formats { get; protected set; }
public TrackCollection Tracks { get; protected set; }
//Example XML to expect
//Artist is in both for V/A albums
//<WREKAlbum artist="Artist-album" title="Title-album" formats="Weekend,RRR-other????" albumid="324234">
// <WREKTrack artist="Artiest-track" category="MRR" trackid="12345" tracknum="1" title="Title-track" av_name="AUT12345" duration="3124???" />
//</WREKAlbum>
//Allow user to enter an album ID, if not entered then get the next random one?
//random to allow for skipping?
public bool GetNext(String inputAlbum) {
string albumXml;
//Grab data from database
InitializeWebClient();
if(m_web == null){
return false;
}
try {
albumXml = m_web.DownloadString(DB_GET_URL + "?album_id=" + inputAlbum);
} catch (WebException ex) {
m_web = null;
UserID = null;
Password = null;
//make sure this gets caught in the UI section
throw ex;
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(albumXml);
//int albumCount = 0;
foreach (var erType in doc.GetElementsByTagName("Error")) {
XmlElement erMess = erType as XmlElement;
MessageBox.Show(erMess.InnerText, "Error", MessageBoxButtons.OK);
return false;
}
//Populate Album info
foreach (var album in doc.GetElementsByTagName("WREKAlbum")) {
//if (albumCount >= 1) {
XmlElement xmlAlbum = album as XmlElement;
//Does this ID need to be generated for the AV database, or is it copied from the other database?
ID = int.Parse(xmlAlbum.Attributes["albumid"].Value);
Artist = xmlAlbum.Attributes["artist"].Value;
Title = xmlAlbum.Attributes["title"].Value;
//Formats = xmlAlbum.Attributes["formats"].Value;
//albumCount++;
//} else {
//In this case, the interface returned more than one album
//for now, this is unexpected behavior
// return false;
//}
}
Tracks = new TrackCollection();
foreach (var track in doc.GetElementsByTagName("WREKTrack")) {
XmlElement xmlTrack = track as XmlElement;
if (track == null) continue;
TrackInfo t = Tracks.AddNew();
t.Artist = xmlTrack.Attributes["artist"].Value;
t.Category = xmlTrack.Attributes["category"].Value;
//make sure this is indeed the track number
t.Index = int.Parse(xmlTrack.Attributes["tracknum"].Value);
//Is this ID taken from the database or is it generated for the new entry in the AV database?
t.ID = int.Parse(xmlTrack.Attributes["track_id"].Value);
t.AVName = xmlTrack.Attributes["av_name"].Value;
t.Title = xmlTrack.Attributes["title"].Value;
//how do we determine the audio length? it might need to be ripped FIRST
t.AudioLength = new MyAVTime(int.Parse(xmlTrack.Attributes["duration"].Value));
}
return true;
}
void InitializeWebClient() {
if (m_web != null) return;
m_web = new WebClient();
byte[] data = null;
DialogResult dR;
if (string.IsNullOrEmpty(UserID)) {
Login frm = new Login();
dR = frm.ShowDialog();
if (dR == DialogResult.Cancel) {
m_web = null;
UserID = null;
return;
}
NameValueCollection loginPosts = new NameValueCollection();
loginPosts.Add("initials", UserID);
loginPosts.Add("password", Password);
data = m_web.UploadValues("http://opdesk.wrek.org/new_logon.php", "POST", loginPosts);
SessionCookie = m_web.ResponseHeaders[HttpResponseHeader.SetCookie];
}
while(data == null || data.Length != 0) {
MessageBox.Show("Login failed", "Error", MessageBoxButtons.OK);
m_web = null;
UserID = null;
InitializeWebClient();
return;
}
m_web.Credentials = new NetworkCredential(UserID, Password);
m_web.Headers[HttpRequestHeader.Cookie] = SessionCookie;
}
WebClient m_web;
//Example XML to expect
//Artist is in both for V/A albums
//Sends in this order, 1 POST per track
// <WREKTrack track_id="512313" av_category="MRR" av_name="AUT512313" duration="3124567" />
//</WREKAlbum>
public bool UpdateTracks() {
//TODO Put code here to send AVName, AudioLength, etc back to database
//doc.DocumentElement.AppendChild()
foreach (var track in Tracks) {
if (track == null) continue;
NameValueCollection sendNVC = new NameValueCollection();
sendNVC.Add("track_id", track.ID.ToString());
sendNVC.Add("av_name", track.AVName);
sendNVC.Add("av_category", track.Category);
sendNVC.Add("duration", track.AudioLength.ToString());
byte[] data = m_web.UploadValues(DB_ADD_URL, "POST", sendNVC);
}
//m_web.UploadData(DB_ADD_URL, byte ARRAY?);
return true;
}
}
}