/// <summary> /// 下载书籍 /// </summary> private void Download_Click(object sender, RoutedEventArgs e) { Thread thread = new Thread(new ThreadStart(delegate { List <string> Text = new List <string>(); foreach (KeyValuePair <string, string> keyValuePair in book.ListUrl) { string html = GetHtml.GetHttpWebRequest(keyValuePair.Value); Text.Add(keyValuePair.Key + "\r\n" + Tool.HtmlFilter(Tool.GetRegexStr(html, bookSource.ContentRegular))); } this.Dispatcher.Invoke(new Action(() => { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = book.Name; dlg.DefaultExt = ".txt"; dlg.Filter = "BookSource (.txt)|*.txt"; Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document string filename = dlg.FileName; using (StreamWriter sw = new StreamWriter(filename)) { sw.Write(string.Join("\r\n", Text)); } } })); })); thread.IsBackground = true; //是否为后台线程 thread.Start(); }
/// <summary> /// 正文获取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TextObtain_Click(object sender, RoutedEventArgs e) { string url = TextLink.Text; //获取范围化的文本 string html = GetHtml.GetHttpWebRequest(url); string Text = Tool.GetRegexStr(html, ContentRegular.Text).Trim(); string Title = Tool.GetRegexStr(html, ContentTitleRegular.Text).Trim(); TextContent.Content = Title; New_Text.Text = Tool.HtmlFilter(Text); if (Text.Length > 10) { state_Text = true; } }
/// <summary> /// 获取书籍内容 /// </summary> private void ObtainBook_Click(object sender, RoutedEventArgs e) { Book book = new Book(); string html = GetHtml.GetHttpWebRequest(Book_url.Text.Trim()); book.Name = Tool.GetRegexStr(html, BookNameRegular.Text).Trim(); book.UpdateState = Tool.GetRegexStr(html, StateRegular.Text).Trim(); book.Newest = Tool.GetRegexStr(html, NewestRegular.Text).Trim(); book.Update = Tool.GetRegexStr(html, UpdateRegular.Text).Trim(); book.Image = Tool.GetRegexStr(html, ImageRegular.Text).Trim(); book.Author = Tool.GetRegexStr(html, AuthorRegular.Text).Trim(); book.Details = Tool.GetRegexStr(html, DetailsRegular.Text).Trim(); //获取所有书籍 this.DataContext = book; if (book.Name.Length > 0 && book.Details.Length > 0) { state_details = true; } }
public void startWork(object state) //工作线程主函数(未完成函数) { while (IsWork != true) { System.Windows.Forms.Application.DoEvents(); //doEvents if (IsWork == false) //没任务,则向任务队列请求 { //IsWork = true; url_Info = getQueue(); if (url_Info != null) { //正在执行任务 DateTime tTmp = DateTime.Now; url_Info.lastTime = tTmp.ToString(); //爬行时间 if (AddUrl(url_Info) == true) { GetHtml htm = new GetHtml(); string tmpHtml = htm.GetPage(url_Info.meUrl); //GetHtml HtmlAnalyzer htmlAnalyzer = new HtmlAnalyzer(tmpHtml, url_Info.meUrl); //HtmlAnalyzer if (htmlAnalyzer.NewUrl != null) //如果分析出url { for (int a = 0; a <= htmlAnalyzer.NewUrl.Count - 1; a++) { Url_info tmp = new Url_info(); tmp.meUrl = htmlAnalyzer.NewUrl[a].ToString(); //把分析器分析出的url传递给临时对象tmp tmp.fromUrl = url_Info.meUrl; //来源设定 tmp.sid = url_Info.sid + 1; //级数+1 postQueue(tmp); //url信息投递到任务队列 } } htm.SavePage(tmpHtml, url_Info.meUrl); } } } if (iListener.OnCloseWork() == true) { IsWork = true; } } }
/// <summary> /// 获取目录 /// </summary> private void GetDirectory_Click(object sender, RoutedEventArgs e) { string url = List_url.Text.Trim(); //获取范围化的文本 string html = Tool.GetRegexStr(GetHtml.GetHttpWebRequest(url), DirectoryScopeRegular.Text).Trim(); //得到被分割的目录 string[] List = html.Split(new string[] { DirectoryCuttingRegular.Text }, StringSplitOptions.RemoveEmptyEntries); //这个是处理的目录 List <GetDirectory> NewList = new List <GetDirectory>(); int Newid = 0; foreach (string chapter in List) { string NewUrl = Tool.GetRegexStr(chapter, DirectoryUrlRegular.Text).Trim(); if (NewUrl.Length > 3) { Newid++; string NewTitle = Tool.GetRegexStr(chapter, DirectoryTieleRegular.Text).Trim(); NewList.Add(new GetDirectory() { Id = Newid, Title = NewTitle, Url = NewUrl }); } } Catalog.ItemsSource = NewList; if (NewList.Count > 1) { state_Catalog = true; } Tips tips = new Tips("目录获取完毕~"); tips.Show(); }
/// <summary> /// 检测书源可用率 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Testing_Click(object sender, RoutedEventArgs e) { MessageBox.Show("状态码如果为1代表书源站正常访问,如果为0代表不可访问", "提示"); Thread thread = new Thread(new ThreadStart(delegate { //遍历源站 foreach (BookSource bookSource in DataFetch.GetBookSources()) { //1代表网站可以访问,0代表不可以访问 int state = GetHtml.GetUnicom(bookSource.Url) ? 1 : 0; DataFetch.UpdateState((int)bookSource.Id, state); } this.Dispatcher.Invoke(new Action(() => { //加载书源 ListSource.ItemsSource = DataFetch.GetBookSources(); })); })); thread.IsBackground = true; thread.Start(); }
private void SearchBook(object Search_Book) { SearchBook searchBook = (SearchBook)Search_Book; string html = GetHtml.GetHttpWebRequest(searchBook.BookUrl); List <Book> books = new List <Book>(); //获取搜索书籍范围 string htmlRange = Tool.GetRegexStr(html, searchBook.AddressRangeRegular).Trim(); //分割搜索书籍 string[] bookList = htmlRange.Split(new string[] { searchBook.AddressCuttingRegular }, StringSplitOptions.RemoveEmptyEntries); int i = 0; foreach (string str in bookList) { string url = Tool.GetRegexStr(str, searchBook.AddressRegular).Trim(); if (url.Length > 3) { i++; books.Add(new Book { Id = i, Url = url }); } } //Invoke是同步 BeginInvoke是异步 Dispatcher.Invoke((Action) delegate { SearchList.ItemsSource = books; }); if (books.Count > 0) { state_search = true; } }
/// <summary> /// 获取网站编码 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetUrlCode(string url) { try { //把网址分割,取出域名部分 string[] Urls = url.Split(new string[] { "://", "/" }, StringSplitOptions.RemoveEmptyEntries); if (UrlCode.ContainsKey(Urls[1])) { return(UrlCode[Urls[1]]); } else { UrlCode[Urls[1]] = GetHtml.GetCode(url); return(UrlCode[Urls[1]]); } } catch (Exception ex) { Console.WriteLine(ex.Message); //Exception ex Tool.TextAdditional(ex.Message); return(""); } }
public TCommandState ExeCommand <TCommandState>(WebCommandVOBase command) where TCommandState : CommandState, new() { string code = web.GetValue("code"); string userSource = web.GetValue("source"); int acctype = web.GetValue <int>("acctype"); //账号类型 int region = web.GetValue <int>("region"); //职位 if (code.StrIsNull()) { return(Show <TCommandState>("CODE是空的,微信认证失败!")); } #region 获取公众号APPID与密钥 string AppId = System.Configuration.ConfigurationManager.AppSettings["AppID"]; string AppSecret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"]; #endregion //通过code换取网页授权access_token var htmlvo = GetHtml.GetHtmlFromUrl("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AppId + "&secret=" + AppSecret + "&code=" + code + "&grant_type=authorization_code", string.Empty, null, null); if (!htmlvo.Item1) { return(Show <TCommandState>("请求微信AccessToken验证时出现了通讯故障!")); } var jsonvo = MakeJson.JsonToLinqObject(htmlvo.Item2); //获取account_key string access_token = jsonvo.TryGetValue <string>("access_token"); string openid = jsonvo.TryGetValue <string>("openid"); string scope = jsonvo.TryGetValue <string>("scope"); if (access_token.StrIsNull()) { return(Show <TCommandState>("AccessToken微信授权失败")); } string scope_str = scope.ToLower(); Tuple <bool, string> userhtmlvo = null; //判断授权类型 switch (scope_str) { case "snsapi_userinfo": //获取用户信息 userhtmlvo = GetHtml.GetHtmlFromUrl("https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN", string.Empty, null, null); break; case "snsapi_base": userhtmlvo = new Tuple <bool, string>(true, string.Empty); break; default: return(Show <TCommandState>("居然返回了一个我从来都不清楚的scope:" + scope_str)); } //判断获取详情数据是否成功 if (!userhtmlvo.Item1) { return(Show <TCommandState>("获取用户详细数据时出现了错误!")); } JObject userjsonvo = null; string my_openid = string.Empty; string unionid = string.Empty; Model.Model.LC_User suser = null; switch (scope_str) { case "snsapi_userinfo": //解析数据 userjsonvo = Tools.MakeJson.JsonToLinqObject(userhtmlvo.Item2); my_openid = userjsonvo.TryGetValue <string>("openid"); unionid = userjsonvo.TryGetValue("unionid", string.Empty); suser = new Model.Model.LC_User() { WX_NickName = userjsonvo.TryGetValue <string>("nickname"), WX_Sex = userjsonvo.TryGetValue <int>("sex"), WX_OpenID = my_openid, WX_Province = userjsonvo.TryGetValue <string>("province"), WX_City = userjsonvo.TryGetValue <string>("city"), WX_Country = userjsonvo.TryGetValue <string>("country"), WX_HeadPic = userjsonvo.TryGetValue <string>("headimgurl"), ZType = acctype, PositionID = region }; //更新或添加账号 (bool, string, Model.Model.LC_User suser)AddOrUpdateVO = BLL.BLL.LC_User.AddOrUpdateUserVO(web, suser); if (!AddOrUpdateVO.Item1) { return(Show <TCommandState>(AddOrUpdateVO.Item2)); } suser = AddOrUpdateVO.suser; break; case "snsapi_base": my_openid = openid; //获取个人数据 (bool, string, Model.Model.LC_User)suser_vo = DAL.DAL.LC_User.GetUserDataFromOpenID(my_openid); if (!suser_vo.Item1) { return(Show <TCommandState>(new { status = -10 })); } else { suser = suser_vo.Item3; } break; } SetLogin(suser); return(Show <TCommandState>(new { status = 1, Open_ID = suser.WX_OpenID, Nick_Name = suser.WX_NickName, Head_Img_Url = suser.WX_HeadPic, ZType = suser.ZType.ConvertData <int>() //账号类型 })); }
/// <summary> /// 跳转到直接章节 /// </summary> /// <param name="page">章节数</param> /// <param name="cache">是否缓存 如果是true,代表只是缓存,前台阅读页并不会因此而改变</param> private void Jump(int page, bool cache = false) { if (NewId.Count < page) { //这里是为了禁止读取不存在的章节 return; } try { //文章html string html = string.Empty; //章节的标题和文本 string Title = string.Empty; string Text = string.Empty; if (!cache) { buffer.Visibility = Visibility.Visible; } Thread thread = new Thread(new ThreadStart(delegate { Title = NewId[page]; //判断本章节是否缓存 if (NewText.ContainsKey(page)) { Text = NewText[page]; } else { html = GetHtml.GetHttpWebRequest(book.ListUrl[NewId[page]]); Text = Tool.GetRegexStr(html, bookSource.ContentRegular); NewText[page] = Text; } if (!cache) { Dispatcher.BeginInvoke((Action) delegate { newTitle.Text = Title; newText.Document.Blocks.Clear(); Run run = new Run(Tool.HtmlFilter(Text)); Paragraph p = new Paragraph(); p.Inlines.Add(run); newText.Document.Blocks.Add(p); ///重置翻页数 this.newText.ScrollToVerticalOffset(0); buffer.Visibility = Visibility.Hidden; //保存已阅章节数 if (book.Id != 0) { DataFetch.UpdateUpdateReadingBook((int)book.Id, page); } NewPages = page; //这里是缓存下面的章节 for (int i = 1; i < 10; i++) { Jump(page + i, true); } }); } })); thread.IsBackground = true; //是否为后台线程 thread.Start(); } catch (Exception ex) { new Tips("打开错误,请检查书源是否存在问题?").Show(); Tool.TextAdditional(ex.Message); buffer.Visibility = Visibility.Hidden; } }
/// <summary> /// 搜索按钮点击 /// </summary> private void Search_Click(object sender, RoutedEventArgs e) { SearchBook searchBook = new SearchBook(); //搜索链接拼凑 searchBook.BookUrl = SearchUrl.Text + Tool.EncodingConvert(SearchBookName.Text, Encoding.GetEncoding(GetHtml.GetCode(SearchUrl.Text))); searchBook.AddressRangeRegular = AddressRangeRegular.Text; searchBook.AddressCuttingRegular = AddressCuttingRegular.Text; searchBook.AddressRegular = AddressRegular.Text; Thread td = new Thread(SearchBook); //把线程设置为后台线程 td.IsBackground = true; td.Start(searchBook); }