11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4- using System . Text . RegularExpressions ;
4+ using System . Text ;
5+ using CnBlogPublishTool . Processor ;
6+ using CnBlogPublishTool . Util ;
7+ using MetaWeblogClient ;
8+ using Newtonsoft . Json ;
59
610namespace CnBlogPublishTool
711{
@@ -10,77 +14,130 @@ class Program
1014 private static string _filePath ;
1115 private static string _fileDir ;
1216 private static string _fileContent ;
17+ private static byte [ ] _teaKey = new byte [ ] { 21 , 52 , 33 , 78 , 52 , 45 } ;
18+ private const string ConfigFilePath = "config.json" ;
19+ private static BlogConnectionInfo _connInfo ;
1320 private static readonly Dictionary < string , string > ReplaceDic = new Dictionary < string , string > ( ) ;
1421 static void Main ( string [ ] args )
1522 {
23+ //加载配置
24+ LoadConfig ( ) ;
25+
1626 if ( args . Length == 0 )
1727 {
1828 Console . WriteLine ( "请输入文件路径!" ) ;
1929 }
2030 else
2131 {
22- try
32+ ProcessFile ( args ) ;
33+ }
34+
35+ Console . WriteLine ( "按任意键退出..." ) ;
36+ Console . ReadKey ( ) ;
37+ }
38+
39+ static void LoadConfig ( )
40+ {
41+ if ( File . Exists ( ConfigFilePath ) )
42+ {
43+ _connInfo = JsonConvert . DeserializeObject < BlogConnectionInfo > ( File . ReadAllText ( ConfigFilePath ) ) ;
44+ _connInfo . Password =
45+ Encoding . UTF8 . GetString ( TeaHelper . Decrypt ( Convert . FromBase64String ( _connInfo . Password ) , _teaKey ) ) ;
46+ ImageUploader . Init ( _connInfo ) ;
47+ }
48+ else
49+ {
50+ SetConfig ( ) ;
51+ }
52+ }
53+
54+ static void SetConfig ( )
55+ {
56+ Console . WriteLine ( "您是第一次运行本程序,请配置以下参数:" ) ;
57+
58+ Console . WriteLine ( "请输入博客ID:(如:https://www.cnblogs.com/stulzq 的博客id为 stulzq )" ) ;
59+ string blogid = Console . ReadLine ( ) ;
60+
61+ Console . WriteLine ( "请输入用户名:" ) ;
62+ string uname = Console . ReadLine ( ) ;
63+
64+ Console . WriteLine ( "请输入密 码:" ) ;
65+ string pwd = Console . ReadLine ( ) ;
66+
67+ _connInfo = new BlogConnectionInfo (
68+ "https://www.cnblogs.com/" + blogid ,
69+ "https://rpc.cnblogs.com/metaweblog/" + blogid ,
70+ blogid ,
71+ uname ,
72+ Convert . ToBase64String ( TeaHelper . Encrypt ( Encoding . UTF8 . GetBytes ( pwd ) , _teaKey ) ) ) ;
73+
74+ File . WriteAllText ( ConfigFilePath , JsonConvert . SerializeObject ( _connInfo ) ) ;
75+ }
76+
77+ static void ProcessFile ( string [ ] args )
78+ {
79+ try
80+ {
81+ _filePath = args [ 0 ] ;
82+ if ( ! File . Exists ( _filePath ) )
2383 {
24- _filePath = args [ 0 ] ;
25- if ( ! File . Exists ( _filePath ) )
26- {
27- Console . WriteLine ( "指定的文件不存在!" ) ;
28- }
29- else
84+ Console . WriteLine ( "指定的文件不存在!" ) ;
85+ }
86+ else
87+ {
88+ _fileDir = new FileInfo ( _filePath ) . DirectoryName ;
89+ _fileContent = File . ReadAllText ( _filePath ) ;
90+ var imgProcessor = new ImageProcessor ( ) ;
91+ var imgList = imgProcessor . Process ( _fileContent ) ;
92+ Console . WriteLine ( $ "提取图片成功,共{ imgList . Count } 个.") ;
93+
94+ //循环上传图片
95+ foreach ( var img in imgList )
3096 {
31- _fileDir = new FileInfo ( _filePath ) . DirectoryName ;
32- _fileContent = File . ReadAllText ( _filePath ) ;
33- var imgProcessor = new ImageProcessor ( ) ;
34- var imgList = imgProcessor . Process ( _fileContent ) ;
35- Console . WriteLine ( $ "提取图片成功,共{ imgList . Count } 个.") ;
36-
37- //循环上传图片
38- foreach ( var img in imgList )
97+ if ( img . StartsWith ( "http" ) )
3998 {
40- if ( img . StartsWith ( "http" ) ) { continue ; }
99+ Console . WriteLine ( $ "{ img } 跳过.") ;
100+ continue ;
101+ }
41102
42- try
103+ try
104+ {
105+ string imgPhyPath = Path . Combine ( _fileDir , img ) ;
106+ if ( File . Exists ( imgPhyPath ) )
43107 {
44- string imgPhyPath = Path . Combine ( _fileDir , img ) ;
45- if ( File . Exists ( imgPhyPath ) )
46- {
47- var imgUrl = ImageUploader . Upload ( imgPhyPath ) ;
48- ReplaceDic . Add ( img , imgUrl ) ;
49- Console . WriteLine ( $ "{ img } upload success. { imgUrl } ") ;
50- }
51- else
52- {
53- Console . WriteLine ( $ "{ img } Not Found.") ;
54- }
55-
108+ var imgUrl = ImageUploader . Upload ( imgPhyPath ) ;
109+ ReplaceDic . Add ( img , imgUrl ) ;
110+ Console . WriteLine ( $ "{ img } 上传成功. { imgUrl } ") ;
56111 }
57- catch ( Exception e )
112+ else
58113 {
59- Console . WriteLine ( e . Message ) ;
114+ Console . WriteLine ( $ " { img } 未发现文件." ) ;
60115 }
61- }
62116
63- //替换
64- foreach ( var key in ReplaceDic . Keys )
117+ }
118+ catch ( Exception e )
65119 {
66- _fileContent = _fileContent . Replace ( key , ReplaceDic [ key ] ) ;
120+ Console . WriteLine ( e . Message ) ;
67121 }
122+ }
68123
69- string newFileName = _filePath . Substring ( 0 , _filePath . LastIndexOf ( '.' ) ) + "-cnlog-" +
70- new FileInfo ( _filePath ) . Extension ;
71- File . WriteAllText ( newFileName , _fileContent , TxtFileEncoder . GetEncoding ( _filePath ) ) ;
72-
73- Console . WriteLine ( $ "处理完成!文件保存在:{ newFileName } ") ;
124+ //替换
125+ foreach ( var key in ReplaceDic . Keys )
126+ {
127+ _fileContent = _fileContent . Replace ( key , ReplaceDic [ key ] ) ;
74128 }
75- }
76- catch ( Exception e )
77- {
78- Console . WriteLine ( e . Message ) ;
129+
130+ string newFileName = _filePath . Substring ( 0 , _filePath . LastIndexOf ( '.' ) ) + "-cnblog-" +
131+ new FileInfo ( _filePath ) . Extension ;
132+ File . WriteAllText ( newFileName , _fileContent , TxtFileEncoder . GetEncoding ( _filePath ) ) ;
133+
134+ Console . WriteLine ( $ "处理完成!文件保存在:{ newFileName } ") ;
79135 }
80136 }
81-
82- Console . WriteLine ( "按任意键退出..." ) ;
83- Console . ReadKey ( ) ;
137+ catch ( Exception e )
138+ {
139+ Console . WriteLine ( e . Message ) ;
140+ }
84141 }
85142 }
86143}
0 commit comments