using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Net; using System.Web; using System.IO; namespace FLocal.Migration.Gateway { public class ShallerGateway { public static readonly Encoding encoding = ShallerConnector.encoding; public static string getUserInfoAsString(string userName) { //if(userName != HttpUtility.UrlEncode(userName, ShallerConnector.encoding)) throw new ApplicationException("'" + userName + "':showprofile.php?User=" + HttpUtility.UrlEncode(userName, ShallerConnector.encoding) + "&What=login&showlite=l"); return ShallerConnector.getPageContent("showprofile.php?User=" + HttpUtility.UrlEncode(userName, ShallerConnector.encoding) + "&What=login&showlite=l", new Dictionary(), new System.Net.CookieContainer()); } private static Dictionary regexInfoCache = new Dictionary(); private static Regex getInfoRegexByCaption(string caption) { if(!regexInfoCache.ContainsKey(caption)) { lock(caption) { if(!regexInfoCache.ContainsKey(caption)) { regexInfoCache[caption] = new Regex("]*>\\s*" + caption + "\\s*\\s*\\s*([^<>]*)\\s*", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline); } } } return regexInfoCache[caption]; } private static Regex avatarRegex = new Regex("", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline); private static Dictionary userImportStructure { get { return new Dictionary() { { "regDate", "Дата\\s+регистрации" }, { "signature", "Подпись" }, { "title", "Титул" }, { "location", "Расположение" }, { "biography", "Биография" }, }; } } public static Dictionary getUserInfo(string userName) { string content = getUserInfoAsString(userName); Dictionary result = userImportStructure.ToDictionary, string, string>( kvp => kvp.Key, kvp => HttpUtility.HtmlDecode(getInfoRegexByCaption(kvp.Value).Match(content).Groups[1].Value).Trim() ); result["avatar"] = avatarRegex.Match(content).Groups[1].Value; return result; } public static IEnumerable getUserNames(int pageNum) { string content = ShallerConnector.getPageContent("showmembers.php?Cat=&sb=13&page=" + pageNum + "&showlite=l", new Dictionary(), new System.Net.CookieContainer()); Regex matcher = new Regex(";User=([^&]+)&", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase); MatchCollection matches = matcher.Matches(content); HashSet result = new HashSet(); foreach(Match match in matches) { result.Add(HttpUtility.UrlDecode(match.Groups[1].Value, ShallerConnector.encoding).Trim()); } return result; } private static FileInfo getFileInfo(string path, int attempt) { try { return ShallerConnector.getPageInfo(path, new Dictionary(), new CookieContainer()); } catch(Exception) { if(attempt > 3) { throw; } else { return getFileInfo(path, attempt + 1); } } } public static FileInfo getFileInfo(string path) { return getFileInfo(path, 1); } } }