批量下载google 字体小工具

需要最新源码,或技术提问,请加QQ群:538327407

我的各种github 开源项目和代码:https://github.com/linbin524



在项目开发中,我们经常用外国的框架,如bootstrap、nodejs、angularjs 时候经常要配套google 字体等资源, 但是由于国内网络原因,经常框架跑起来,网页在请求google 字体时候,由于得不到响应,导致页面挂掉了,这是很尴尬的事情。

解决方法:1、代理

     2、写一个小软件,批量下载google 字体,应用到项目中,解决这个问题。

基于这个需求,写了一个小软件,只要把涉及到google 字体的js copy 软件Textbox中,点击请求,下载google 字体,放到自己的项目中, 批量修改 js中google字体路径,就解决。

 

使用步骤:1、将 js 文件放到Textbox 中,选择解析下载字体,提示解析完成

 

                2、点击下载字体,字体自动下载bin 文件下

 

 

原理解析: 通过正则表达式对 js 代码 解析,提炼 字体 下载地址,将地址存放到队列中,在调用 http 请求 下载

 

备注:下载时候,本地网络环境需要有FQ

 

 

using AOP.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DownLoadGoogleFonts
{
    public partial class Form1 : Form
    {

        public static Queue<string> downUrlQueue;
        public delegate void InvokeHandler();//多线程委托调用
        public Form1()
        {
            downUrlQueue = new Queue<string>();
            InitializeComponent();
        }
        public Thread t1;

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_downloadFont_Click(object sender, EventArgs e)
        {
            t1 = new Thread(new ThreadStart(() =>
               {
                   while (true)
                   {
                       this.Invoke(new InvokeHandler(() =>
                       {

                           if (downUrlQueue.Count() > 0)
                           {
                               string url = downUrlQueue.Dequeue();
                               string[] tempArray = url.Split('/');
                               string fileName = tempArray[tempArray.Length - 1];
                               HttpRequestHelper.DownloadFile(url, System.Environment.CurrentDirectory + "/font/" + fileName);
                               tb_downLoadRecord.AppendText(System.Environment.CurrentDirectory + "/font/" + fileName + "\r\n");
                           }

                       }));
                       Thread.Sleep(100);//线程100 0.1 正好
                    }

               }));

            t1.Start();



        }
        /// <summary>
        /// 解析源文件字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_analysisUrl_Click(object sender, EventArgs e)
        {
            string requestText = tb_fileContent.Text.Trim();
            string RegexStr = "(?<=url\\()[^\\)]+";

            MatchCollection mc;
            Regex r = new Regex(RegexStr);

            mc = r.Matches(requestText);

            List<string> list = new List<string>();

            tb_fileContent.Text = "";
            for (int i = 0; i < mc.Count; i++)
            {
                if (!mc[i].Value.Contains("../font/"))
                {
                    WriteDownloadQueue(mc[i].Value);
                    tb_fileContent.AppendText(mc[i].Value+ "\r\n");
                }

            }

            MessageBox.Show("解析完成!");

        }

        public static void WriteDownloadQueue(string url)
        {
            lock (downUrlQueue)
            {
                downUrlQueue.Enqueue(url);
            }

        }
    }
}

 

 

 

源码已经放到github 上面了:源码地址

 

笔者原创,转载请添加原博客连接,谢谢!

posted @ 2017-12-11 11:30  linbin524  阅读(604)  评论(0编辑  收藏  举报