-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stackoverflow.cs
171 lines (142 loc) · 4.95 KB
/
Stackoverflow.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
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Net;
using System.Text;
namespace iSOFlair
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
private int stackoverflowId = -1;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
var prefs = NSUserDefaults.StandardUserDefaults;
Console.WriteLine("Prefs: soid=" + prefs.StringForKey("soid"));
string stackoverflowIdString = prefs.StringForKey("soid");
if (!int.TryParse(stackoverflowIdString, out stackoverflowId))
{
stackoverflowId = 22656; // Jon Skeet
// "25673"; // Craig Dunn
}
buttonUpdate.TouchDown += delegate {
Update();
};
if (System.IO.File.Exists("cache.txt"))
{
Load();
} else
{
Update();
}
window.MakeKeyAndVisible ();
return true;
}
private string profileUrl = "";
protected void Load()
{
Console.WriteLine("Loading from cache.txt");
string cache = System.IO.File.ReadAllText("cache.txt");
string[] cacheItems = cache.Split('?');
labelUsername.Text = cacheItems[0];
labelPoints.Text = cacheItems[1];
profileUrl = cacheItems[2];
labelLastUpdated.Text=cacheItems[3];
}
protected void Update(){
Console.WriteLine("Loading from web");
WebClient wc = new WebClient();
Uri uri = new Uri(
String.Format("http://stackoverflow.com/users/flair/{0}.html"
, stackoverflowId));
byte[] bytes = null;
try
{
bytes = wc.DownloadData(uri);
}
catch (Exception ex)
{
Console.WriteLine("Internet connection failed: " + ex.Message);
labelConnectionError.Text = "Update failed: cannot connect to the Internet.";
return;
}
string result = Encoding.UTF8.GetString(bytes);
labelUsername.Text = result;
int startpos = result.IndexOf("<a");
startpos = result.IndexOf("href=",startpos) + 6;
int endpos = result.IndexOf("\"",startpos);
profileUrl = result.Substring(startpos, endpos-startpos);
startpos = result.IndexOf("img src",endpos) + 9;
endpos = result.IndexOf("\"",startpos);
string gravatarUrl = result.Substring(startpos, endpos-startpos);
uri = new Uri(gravatarUrl);
wc.DownloadFile(uri, "gravatar.png");
UIImage img = UIImage.FromFile("gravatar.png");
imageviewGravatar.AnimationImages = new UIImage[] {img};
imageviewGravatar.AnimationDuration = 10;
imageviewGravatar.StartAnimating();
startpos = result.IndexOf("username",endpos);
startpos = result.IndexOf("_blank",endpos) + 8;
endpos = result.IndexOf("<",startpos);
string username = result.Substring(startpos, endpos-startpos);
labelUsername.Text = username;
startpos = result.IndexOf("reputation score",endpos) + 18;
endpos = result.IndexOf("<",startpos);
string points = result.Substring(startpos, endpos-startpos);
string goldBadges="", silverBadges="", bronzeBadges="";
startpos = result.IndexOf("gold badge",endpos);
if (startpos > 0)
{
startpos --; // remove preceding space
endpos = result.IndexOf("\"",startpos-10) + 1;
goldBadges = result.Substring(endpos, startpos-endpos);
Console.WriteLine(" " + goldBadges);
labelGoldBadges.Text = goldBadges + " gold badges";
}
startpos = result.IndexOf("silver badge",endpos);
if (startpos > 0)
{
startpos --; // remove preceding space
endpos = result.IndexOf("\"",startpos-10) + 1;
silverBadges = result.Substring(endpos, startpos-endpos);
labelSilverBadges.Text = silverBadges + " silver badges";
}
startpos = result.IndexOf("bronze badge",endpos);
if (startpos > 0)
{
startpos --; // remove preceding space
endpos = result.IndexOf("\"",startpos-10) + 1;
bronzeBadges = result.Substring(endpos, startpos-endpos);
labelBronzeBadges.Text = bronzeBadges + " bronze badges";
}
Console.WriteLine("gold " + goldBadges);
Console.WriteLine("silver " + silverBadges);
Console.WriteLine("bronze " + bronzeBadges);
labelPoints.Text = points;
string dateUpdated = DateTime.Now.ToString("dd-MMM-yy hh:mm:ss");
labelLastUpdated.Text = dateUpdated;
string cache = String.Format("{0}?{1}?{2}?{3}"
, username
, points
, profileUrl
, dateUpdated);
System.IO.File.WriteAllText("cache.txt", cache);
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
}
}