-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cache.cs
141 lines (130 loc) · 4.32 KB
/
Cache.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
namespace Claymore.SharpMediaWiki
{
public static class WikiCache
{
public static void Login(Wiki wiki, string username, string password)
{
if (!WikiCache.LoadCookies(wiki))
{
wiki.Login(username, password);
WikiCache.CacheCookies(wiki);
}
else
{
wiki.Login();
if (!wiki.IsBot)
{
wiki.Logout();
wiki.Login(username, password);
WikiCache.CacheCookies(wiki);
}
}
}
public static void Login(Wiki wiki, string username, string password, string fileName)
{
if (!WikiCache.LoadCookies(wiki, fileName))
{
wiki.Login(username, password);
WikiCache.CacheCookies(wiki, fileName);
}
else
{
wiki.Login();
if (!wiki.IsBot)
{
wiki.Logout();
wiki.Login(username, password);
WikiCache.CacheCookies(wiki, fileName);
}
}
}
public static void CacheCookies(Wiki wiki, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
{
byte[] data = wiki.CookiesToArray();
gs.Write(data, 0, data.Length);
}
}
public static void CacheCookies(Wiki wiki)
{
Directory.CreateDirectory("Cache");
string filename = @"Cache\cookie.jar";
CacheCookies(wiki, filename);
}
public static bool LoadCookies(Wiki wiki)
{
return LoadCookies(wiki, @"Cache\cookie.jar");
}
public static bool LoadCookies(Wiki wiki, string fileName)
{
if (!File.Exists(fileName))
{
return false;
}
using (FileStream fs = new FileStream(fileName, FileMode.Open))
using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
using (BinaryReader sr = new BinaryReader(gs))
{
List<byte> data = new List<byte>();
int b;
while ((b = sr.BaseStream.ReadByte()) != -1)
{
data.Add((byte)b);
}
wiki.LoadCookies(data.ToArray());
}
return true;
}
public static bool LoadNamespaces(Wiki wiki)
{
return LoadNamespaces(wiki, @"Cache\namespaces.dat");
}
public static void CacheNamespaces(Wiki wiki)
{
CacheNamespaces(wiki, @"Cache\namespaces.dat");
}
public static bool LoadNamespaces(Wiki wiki, string filename)
{
if (!File.Exists(filename))
{
return false;
}
using (FileStream fs = new FileStream(filename, FileMode.Open))
using (GZipStream gs = new GZipStream(fs, CompressionMode.Decompress))
using (BinaryReader sr = new BinaryReader(gs))
{
List<byte> data = new List<byte>();
int b;
while ((b = sr.BaseStream.ReadByte()) != -1)
{
data.Add((byte)b);
}
wiki.LoadNamespaces(data);
}
return true;
}
public static void CacheNamespaces(Wiki wiki, string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create))
using (GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
{
byte[] data = wiki.NamespacesToArray();
gs.Write(data, 0, data.Length);
}
}
public static string EscapePath(string path)
{
Regex charsRE = new Regex(@"[:/\*\?<>\|\n]");
return charsRE.Replace(path, "_").Replace('"', '_').Replace('\\', '_');
}
}
}