public void AddThemeTest() { IThemeManageRepository target = CreateIThemeManageRepository(); // TODO: Initialize to an appropriate value Theme theme = new Theme() { AddTime = DateTime.Now, AuthorId = 007, RateNumbers = 1, CategoryId = 0, CheckerId = 007, CheckState = CheckStateOption.Waitting, CommendIndex = 1, Comments = 3, Description = "test", DisplayState = DisplayStateOption.Deleted, Downloads = 100, FileSize = 100, RateScore = 2, LastMonthDownloads = 1, LastWeekDownloads = 10, ParentCategoryId = 0, Source = SourceOption.M, ThumbnailName = Guid.NewGuid().ToString(), Title = "test", UpdateTime = DateTime.Now, Views = 1000, }; bool expected = true; // TODO: Initialize to an appropriate value bool actual; actual = target.AddTheme(theme); Assert.AreEqual(expected, actual); }
public void Start() { XPathDocument document = new XPathDocument("http://panda.sj.91.com/Service/GetResourceData.aspx?mt=1&qt=103&ps=5000&pi=1"); var navRoot = document.CreateNavigator(); XPathNodeIterator items = navRoot.Select("result/data/item"); int index = 1; while (items.MoveNext()) { try { var currItem = items.Current; if (currItem.SelectSingleNode("id").Value.ToInt32() > 18045) { Theme theme = new Theme() { AddTime = DateTime.Now, AuthorId = 0, CategoryId = currItem.SelectSingleNode("type").Value.ToInt32(), CheckerId = 0, CheckState = CheckStateOption.Waitting, CommendIndex = 1, Comments = 0, Description = currItem.SelectSingleNode("descript").Value, DisplayState = DisplayStateOption.Display, Downloads = 0, DownloadUrl = GetFileName("ThemeFiles/201007/" + (3860+index) + ".zip"), FileSize = currItem.SelectSingleNode("size").Value.ToInt64() * 1024, LastMonthDownloads = 0, LastWeekDownloads = 0, ParentCategoryId = 0, RateNumbers = 0, RateScore = 0, Source = SourceOption.M, ThumbnailName = GetFileName("ThemeThumbnails/201007/" + (3860 + index) + ".jpg"), Title = currItem.SelectSingleNode("name").Value, UpdateTime = DateTime.Now, Views = 0, AuthorName = currItem.SelectSingleNode("id").Value, }; _webClient.Headers.Add(HttpRequestHeader.Referer, "http://m.91.com"); _webClient.DownloadFile("http://mobile.91.com/t" + theme.AuthorName, Path.Combine(SAVE_PATH, theme.DownloadUrl)); _webClient.DownloadFile(currItem.SelectSingleNode("previewUrl").Value, Path.Combine(SAVE_PATH, theme.ThumbnailName)); ImageHelper.MakeThumbnail(Path.Combine(SAVE_PATH, theme.ThumbnailName), Path.Combine(SAVE_PATH, theme.ThumbnailName.Replace(".jpg", "_112x168.jpg")), 112, 168, "wh"); IThemeManageRepository repository = ThemeRepositoryFactory.Default.GetThemeManageRepository(); repository.AddTheme(theme); Console.WriteLine("#" + index + ":" + theme.Title); File.AppendAllText(SAVE_PATH + "Log.txt", index + "\r\n"); index++; } } catch (Exception ex) { Console.WriteLine(ex.Message); File.AppendAllText(SAVE_PATH + "Error.txt", items.Current.SelectSingleNode("id").Value + "\r\n" + ex.ToString() + "\r\n\r\n"); File.AppendAllText(SAVE_PATH + "ErrorUrl.txt", items.Current.SelectSingleNode("id").Value + "\r\n"); } } }
public void Start() { List<Theme> themes = SerializeHelper.BinaryDeserialize<List<Theme>>(Path.Combine(SAVE_PATH, "rewrite.dll")); int index = 1; foreach (Theme sourceTheme in themes) { try { Theme theme = new Theme() { AddTime = sourceTheme.AddTime, AuthorId = 0, CategoryId = sourceTheme.CategoryId, CheckerId = 0, CheckState = CheckStateOption.Waitting, CommendIndex = sourceTheme.CommendIndex, Comments = 0, Description = sourceTheme.Description, DisplayState = DisplayStateOption.Display, Downloads = sourceTheme.Downloads, DownloadUrl = GetFileName("ThemeFiles/201004/" + sourceTheme.Title + ".zip"), FileSize = sourceTheme.FileSize, LastMonthDownloads = 0, LastWeekDownloads = 0, ParentCategoryId = 0, RateNumbers = 0, RateScore = 0, Source = SourceOption.M, ThumbnailName = GetFileName("ThemeThumbnails/201004/" + sourceTheme.Title + ".jpg"), Title = sourceTheme.Title, UpdateTime = sourceTheme.AddTime, Views = sourceTheme.Downloads, AuthorName = sourceTheme.AuthorName, }; _webClient.Headers.Add(HttpRequestHeader.Referer, "http://m.91.com"); _webClient.DownloadFile(sourceTheme.DownloadUrl, Path.Combine(SAVE_PATH, theme.DownloadUrl)); _webClient.DownloadFile(sourceTheme.ThumbnailName, Path.Combine(SAVE_PATH, theme.ThumbnailName)); ImageHelper.MakeThumbnail(Path.Combine(SAVE_PATH, theme.ThumbnailName), Path.Combine(SAVE_PATH, theme.ThumbnailName.Replace(".jpg", "_112x168.jpg")), 112, 168, "wh"); IThemeManageRepository repository = ThemeRepositoryFactory.Default.GetThemeManageRepository(); repository.AddTheme(theme); Console.WriteLine("#" + index + ":" + theme.Title); File.AppendAllText(SAVE_PATH + "Log.txt", index + "\r\n"); } catch (Exception ex) { File.AppendAllText(SAVE_PATH + "Error.txt", sourceTheme.AuthorName + "\r\n" + ex.ToString() + "\r\n\r\n"); File.AppendAllText(SAVE_PATH + "ErrorUrl.txt", sourceTheme.AuthorName + "\r\n"); } index++; } }
/// <summary> /// 添加新的主题 /// </summary> /// <param name="theme">主题对象</param> /// <returns></returns> public bool AddTheme(Theme theme) { SqlParameter[] parameters = new SqlParameter[] { SqlParameterHelper.BuildInputParameter("@CategoryId",SqlDbType.Int, 4, theme.CategoryId), SqlParameterHelper.BuildInputParameter("@ParentCategoryId",SqlDbType.Int, 4, theme.ParentCategoryId), SqlParameterHelper.BuildInputParameter("@Title",SqlDbType.NVarChar, 300, theme.Title), SqlParameterHelper.BuildInputParameter("@FileSize",SqlDbType.BigInt, 8, theme.FileSize), SqlParameterHelper.BuildInputParameter("@Description",SqlDbType.NVarChar, 1000, theme.Description), SqlParameterHelper.BuildInputParameter("@DisplayState",SqlDbType.SmallInt, 2, theme.DisplayState.ToInt32()), SqlParameterHelper.BuildInputParameter("@CheckState",SqlDbType.SmallInt, 2, theme.CheckState.ToInt32()), SqlParameterHelper.BuildInputParameter("@AuthorId",SqlDbType.Int, 4, theme.AuthorId), SqlParameterHelper.BuildInputParameter("@CheckerId",SqlDbType.Int, 4, theme.CheckerId), SqlParameterHelper.BuildInputParameter("@CommendIndex",SqlDbType.Int, 4, theme.CommendIndex), SqlParameterHelper.BuildInputParameter("@ThumbnailName",SqlDbType.NVarChar, 100, theme.ThumbnailName), SqlParameterHelper.BuildInputParameter("@AddTime",SqlDbType.DateTime, 8, theme.AddTime), SqlParameterHelper.BuildInputParameter("@UpdateTime",SqlDbType.DateTime, 8, theme.UpdateTime), SqlParameterHelper.BuildInputParameter("@RateScore",SqlDbType.Int, 4, theme.RateScore), SqlParameterHelper.BuildInputParameter("@RateNumbers",SqlDbType.Int, 4, theme.RateNumbers), SqlParameterHelper.BuildInputParameter("@Comments",SqlDbType.Int, 4, theme.Comments), SqlParameterHelper.BuildInputParameter("@Downloads",SqlDbType.Int, 4, theme.Downloads), SqlParameterHelper.BuildInputParameter("@Views",SqlDbType.Int, 4, theme.Views), SqlParameterHelper.BuildInputParameter("@LastWeekDownloads",SqlDbType.Int, 4, theme.LastWeekDownloads), SqlParameterHelper.BuildInputParameter("@LastMonthDownloads",SqlDbType.Int, 4, theme.LastMonthDownloads), SqlParameterHelper.BuildInputParameter("@Source",SqlDbType.Int, 4, theme.Source.ToInt32()), SqlParameterHelper.BuildInputParameter("@DownloadUrl", SqlDbType.NVarChar, 300, theme.DownloadUrl), SqlParameterHelper.BuildInputParameter("@AuthorName", SqlDbType.NVarChar, 128, theme.AuthorName), SqlParameterHelper.BuildInputParameter("@AuthorMail", SqlDbType.NVarChar, 128, theme.AuthorMail), SqlParameterHelper.BuildInputParameter("@SupportIPhone4",SqlDbType.Int, 4, theme.SupportIPhone4 ? 1 : 0), SqlParameterHelper.BuildInputParameter("@Price",SqlDbType.Decimal, 16, theme.Price), }; string cmdText = @" INSERT INTO Theme (CategoryId,ParentCategoryId,Title,FileSize,Description,DisplayState,CheckState,AuthorId,CheckerId,CommendIndex,ThumbnailName,AddTime,UpdateTime,RateScore,RateNumbers,Comments,Downloads,Views,LastWeekDownloads,LastMonthDownloads,Source,DownloadUrl,AuthorName,AuthorMail,SupportIPhone4,Price) VALUES (@CategoryId,@ParentCategoryId,@Title,@FileSize,@Description,@DisplayState,@CheckState,@AuthorId,@CheckerId,@CommendIndex,@ThumbnailName,@AddTime,@UpdateTime,@RateScore,@RateNumbers,@Comments,@Downloads,@Views,@LastWeekDownloads,@LastMonthDownloads,@Source,@DownloadUrl,@AuthorName,@AuthorMail,@SupportIPhone4,@Price);SELECT @@IDENTITY"; int themeId = Convert.ToInt32(SqlHelper.ExecuteScalar(_connectionProvider.GetWriteConnectionString(), CommandType.Text, cmdText, parameters)); if (themeId > 0) { theme.ThemeId = themeId; return true; } return false; }
protected void btnSave_Click(object sender, EventArgs e) { if (hidThemeId.Value.ToInt32() > 0) { IThemeSky.Model.Theme theme = _repositoryView.GetTheme(hidThemeId.Value.ToInt32()); theme.ThemeId = hidThemeId.Value.ToInt32(); theme.ThumbnailName = txtThumbnailName.Text.StartsWith("/") ? txtThumbnailName.Text.Substring(1) : txtThumbnailName.Text; theme.Title = txtTitle.Text; theme.Price = Convert.ToDouble(txtPrice.Text); theme.AuthorId = 0; theme.AuthorMail = txtAuthorMail.Text; theme.AuthorName = txtAuthorName.Text; theme.CategoryId = ddlCategoryId.SelectedValue.ToInt32(); theme.CheckerId = UserContext.UserId; theme.CheckState = ddlCheckState.SelectedValue.ToEnum<CheckStateOption>(CheckStateOption.Waitting); theme.CommendIndex = ddlCommendIndex.SelectedValue.ToInt32(); theme.Comments = 0; theme.Description = txtDescription.Text; theme.Downloads = txtDownloads.Text.ToInt32(0); theme.DisplayState = chkDisplayState.Checked ? DisplayStateOption.Display : DisplayStateOption.Hidden; theme.DownloadUrl = txtDownloadUrl.Text; theme.LastMonthDownloads = Convert.ToInt32(txtLastMonthDownloads.Text); theme.LastWeekDownloads = Convert.ToInt32(txtLastWeekDownloads.Text); theme.RateNumbers = Convert.ToInt32(txtRateNumbers.Text); theme.RateScore = Convert.ToInt32(txtRateScore.Text); theme.FileSize = txtFileSize.Text.ToInt64(); theme.UpdateTime = Convert.ToDateTime(txtUpdateTime.Text); theme.AddTime = Convert.ToDateTime(txtAddTime.Text); theme.Views = Convert.ToInt32(txtViews.Text); theme.SupportIPhone4 = chkSupportIPhone4.Checked; _repositoryManage.UpdateTheme(theme); //增加主题图片 string images = Request.Form["hidThemeImages"]; if (!string.IsNullOrEmpty(images)) { string[] arrImages = images.Split(','); foreach (string image in arrImages) { _repositoryManage.AddThemeImage(hidThemeId.Value.ToInt32(), image); } } ltlMessage.Text = "修改主题成功"; } else { IThemeSky.Model.Theme theme = new IThemeSky.Model.Theme() { ThumbnailName = txtThumbnailName.Text.StartsWith("/") ? txtThumbnailName.Text.Substring(1) : txtThumbnailName.Text, Title = txtTitle.Text, Price = Convert.ToDouble(txtPrice.Text), AddTime = DateTime.Now, AuthorId = 0, AuthorMail = txtAuthorMail.Text, AuthorName = txtAuthorName.Text, CategoryId = ddlCategoryId.SelectedValue.ToInt32(), CheckerId = UserContext.UserId, CheckState = ddlCheckState.SelectedValue.ToEnum<CheckStateOption>(CheckStateOption.Waitting), CommendIndex = ddlCommendIndex.SelectedValue.ToInt32(), Comments = 0, Description = txtDescription.Text, Downloads = txtDownloads.Text.ToInt32(0), DisplayState = chkDisplayState.Checked ? DisplayStateOption.Display : DisplayStateOption.Hidden, DownloadUrl = txtDownloadUrl.Text, LastMonthDownloads = Convert.ToInt32(txtLastMonthDownloads.Text), LastWeekDownloads = Convert.ToInt32(txtLastWeekDownloads.Text), RateNumbers = Convert.ToInt32(txtRateNumbers.Text), RateScore = Convert.ToInt32(txtRateScore.Text), FileSize = txtFileSize.Text.ToInt64(), UpdateTime = DateTime.Now, Views = Convert.ToInt32(txtViews.Text), Source = SourceOption.IThemeSky, SupportIPhone4 = chkSupportIPhone4.Checked, }; _repositoryManage.AddTheme(theme); //增加主题图片 string images = Request.Form["hidThemeImages"]; if (!string.IsNullOrEmpty(images)) { string[] arrImages = images.Split(','); foreach (string image in arrImages) { _repositoryManage.AddThemeImage(theme.ThemeId, image); } } ltlMessage.Text = "添加主题成功"; } rptThemeImages.DataSource = _repositoryView.GetThemeImages(Request.QueryString["themeId"].ToInt32()); rptThemeImages.DataBind(); }
public void AnalyseThemeInfo(string detailUrl) { try { Stopwatch watch = new Stopwatch(); watch.Start(); string detailContent = _webClient.DownloadString(BASE_URL + detailUrl); Match m = Regex.Match(detailContent, @"<h1>(?<Title>[^<]+)</h1>"); string title = m.Groups[1].Value; m = Regex.Match(detailContent, @"uploads/userup/\d+/[^.]+\.jpg"); string imageUrl = ""; if (m.Success) { imageUrl = BASE_URL + m.Value; } else { m = Regex.Match(detailContent, @"/theme//[^.]+\.(jpg|png)", RegexOptions.IgnoreCase); imageUrl = BASE_URL + m.Value; } m = Regex.Match(detailContent, @"<p>(?<Content>[^<]*)</p>"); string content = m.Groups["Content"].Value; m = Regex.Match(detailContent, @"/plus/download\.php\?open=\d+&aid=\d+&cid=\d+"); string downUrl = BASE_URL + m.Value; string downPageContent = _webClient.DownloadString(downUrl); m = Regex.Match(downPageContent, @"<div class=""downloadlinks""><a href='(?<ThemeFileUrl>[^']+)' style='color:red' target='_blank'>"); string themeFileUrl = BASE_URL + m.Groups["ThemeFileUrl"].Value; int downloads = (new Random()).Next(10, 500); Theme theme = new Theme() { AddTime = DateTime.Now, AuthorId = 0, CategoryId = 0, CheckerId = 0, CheckState = CheckStateOption.CheckSuccess, CommendIndex = 2, Comments = 0, Description = content, DisplayState = DisplayStateOption.Display, Downloads = downloads, DownloadUrl = GetFileName("ThemeFiles/" + title.Replace(" ", "-") + ".rar"), FileSize = 0, LastMonthDownloads = downloads, LastWeekDownloads = downloads, ParentCategoryId = 0, RateNumbers = 0, RateScore = 0, Source = SourceOption.IPhoneThemes, ThumbnailName = GetFileName("ThemeThumbnails/" + title.Replace(" ", "-") + ".jpg"), Title = title, UpdateTime = DateTime.Now, Views = downloads * 2, }; _webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6"); _webClient.Headers.Add(HttpRequestHeader.Referer, downUrl); _webClient.Headers.Add("Cookie", "UniProc1224141825=112691391510413393; bsau=12691391673546121804; bsas=12691391673556384879; UserInteraction4=KonaBase; AJSTAT_ok_pages=2; AJSTAT_ok_times=1; __utma=106534879.1209003502.1269139179.1269139179.1269139179.1; __utmb=106534879.2.10.1269139179; __utmc=106534879; __utmz=106534879.1269139193.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"); downPageContent = _webClient.DownloadString(themeFileUrl); m = Regex.Match(downPageContent, @"""(?<ThemeFileUrl>[^""]+)"""); themeFileUrl = m.Groups["ThemeFileUrl"].Value; if (!themeFileUrl.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase)) { themeFileUrl = BASE_URL + themeFileUrl; } if (themeFileUrl.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase)) { theme.DownloadUrl.Replace(".rar", ".zip"); } _webClient.DownloadFile(themeFileUrl, Path.Combine(SAVE_PATH, theme.DownloadUrl)); _webClient.DownloadFile(imageUrl, Path.Combine(SAVE_PATH, theme.ThumbnailName)); //生成缩略图 ImageHelper.MakeThumbnail(Path.Combine(SAVE_PATH, theme.ThumbnailName), Path.Combine(SAVE_PATH, theme.ThumbnailName.Replace(".jpg", "_112x168.jpg")), 112, 168, "wh"); theme.FileSize = new FileInfo(Path.Combine(SAVE_PATH, theme.DownloadUrl)).Length; IThemeManageRepository repository = ThemeRepositoryFactory.Default.GetThemeManageRepository(); repository.AddTheme(theme); MatchCollection mc = Regex.Matches(detailContent, @"<a href='/tag\.php\?/(?<TagName>[^/]+)/'>"); foreach (Match mTag in mc) { repository.MappingThemeTag(theme.ThemeId, mTag.Groups["TagName"].Value); } watch.Stop(); Console.WriteLine("抓取" + detailUrl + "完成,用时:" + watch.Elapsed); } catch (Exception ex) { File.AppendAllText(SAVE_PATH + "Error.txt", detailUrl + "\r\n" + ex.ToString() + "\r\n\r\n"); File.AppendAllText(SAVE_PATH + "ErrorUrl.txt", detailUrl + "\r\n"); } }
} /// <summary> /// 更新一条数据 /// </summary> /// <param name="theme">主题实体</param> public bool UpdateTheme(Theme theme) { string cmdText = @" update Theme set CategoryId=@CategoryId, ParentCategoryId=@ParentCategoryId, Title=@Title, FileSize=@FileSize, Description=@Description, DisplayState=@DisplayState, CheckState=@CheckState, AuthorId=@AuthorId, CheckerId=@CheckerId, CommendIndex=@CommendIndex, ThumbnailName=@ThumbnailName, AddTime=@AddTime, UpdateTime=@UpdateTime, RateScore=@RateScore, RateNumbers=@RateNumbers, Comments=@Comments, Downloads=@Downloads, Views=@Views, LastWeekDownloads=@LastWeekDownloads, LastMonthDownloads=@LastMonthDownloads, Source=@Source, DownloadUrl=@DownloadUrl, AuthorName=@AuthorName, AuthorMail=@AuthorMail, SupportIPhone4=@SupportIPhone4, Price=@Price where ThemeId=@ThemeId "; SqlParameter[] parameters = new SqlParameter[] { SqlParameterHelper.BuildInputParameter("@ThemeId", SqlDbType.Int, 4, theme.ThemeId), SqlParameterHelper.BuildInputParameter("@CategoryId", SqlDbType.Int, 4, theme.CategoryId), SqlParameterHelper.BuildInputParameter("@ParentCategoryId", SqlDbType.Int, 4, theme.ParentCategoryId), SqlParameterHelper.BuildInputParameter("@Title", SqlDbType.NVarChar, 300, theme.Title), SqlParameterHelper.BuildInputParameter("@FileSize", SqlDbType.BigInt, 8, theme.FileSize), SqlParameterHelper.BuildInputParameter("@Description", SqlDbType.NVarChar, 1000, theme.Description), SqlParameterHelper.BuildInputParameter("@DisplayState", SqlDbType.SmallInt, 2, theme.DisplayState), SqlParameterHelper.BuildInputParameter("@CheckState", SqlDbType.SmallInt, 2, theme.CheckState), SqlParameterHelper.BuildInputParameter("@AuthorId", SqlDbType.Int, 4, theme.AuthorId), SqlParameterHelper.BuildInputParameter("@CheckerId", SqlDbType.Int, 4, theme.CheckerId), SqlParameterHelper.BuildInputParameter("@CommendIndex", SqlDbType.Int, 4, theme.CommendIndex), SqlParameterHelper.BuildInputParameter("@ThumbnailName", SqlDbType.NVarChar, 100, theme.ThumbnailName), SqlParameterHelper.BuildInputParameter("@AddTime", SqlDbType.DateTime, 8, theme.AddTime), SqlParameterHelper.BuildInputParameter("@UpdateTime", SqlDbType.DateTime, 8, theme.UpdateTime), SqlParameterHelper.BuildInputParameter("@RateScore", SqlDbType.Int, 4, theme.RateScore), SqlParameterHelper.BuildInputParameter("@RateNumbers", SqlDbType.Int, 4, theme.RateNumbers), SqlParameterHelper.BuildInputParameter("@Comments", SqlDbType.Int, 4, theme.Comments), SqlParameterHelper.BuildInputParameter("@Downloads", SqlDbType.Int, 4, theme.Downloads), SqlParameterHelper.BuildInputParameter("@Views", SqlDbType.Int, 4, theme.Views), SqlParameterHelper.BuildInputParameter("@LastWeekDownloads", SqlDbType.Int, 4, theme.LastWeekDownloads), SqlParameterHelper.BuildInputParameter("@LastMonthDownloads", SqlDbType.Int, 4, theme.LastMonthDownloads), SqlParameterHelper.BuildInputParameter("@Source", SqlDbType.Int, 4, theme.Source), SqlParameterHelper.BuildInputParameter("@DownloadUrl", SqlDbType.NVarChar, 300, theme.DownloadUrl), SqlParameterHelper.BuildInputParameter("@AuthorName", SqlDbType.NVarChar, 128, theme.AuthorName), SqlParameterHelper.BuildInputParameter("@AuthorMail", SqlDbType.NVarChar, 128, theme.AuthorMail), SqlParameterHelper.BuildInputParameter("@SupportIPhone4",SqlDbType.Int, 4, theme.SupportIPhone4 ? 1 : 0), SqlParameterHelper.BuildInputParameter("@Price",SqlDbType.Decimal, 16, theme.Price), };