欢迎光临

木子屋

标题: 淘宝助理辅助工具

作者: dnawo

日期: 2011-08-18 09:30 AM

分类: Win编程

内容: 复制内容到剪贴板 程序代码using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// csv文件编码,(4.6=Unicode,5.0=Default)
/// </summary>
static Encoding csvEncoding = Encoding.Unicode;
static void Main(string[] args)
{
if (args.Length == 1)
{
BackupTBImgage(args[0]);
Console.WriteLine("图片备份完成.");
}
else if (args.Length == 2)
{
UpdateTBImage(args[0], args[1]);
Console.WriteLine("图片搬家完成.");
}
else if (args.Length == 3)
{
UpdateTBImage(args[0], args[1], int.Parse(args[2]));
Console.WriteLine("图片搬家完成.");
}
else
{
Console.WriteLine(@"调用语法：");
Console.WriteLine(@"1).图片备份 tb.exe c:\test.csv");
Console.WriteLine(@"2).图片搬家 tb.exe c:\test.csv http://www.abc.com/ [number]");
}
Console.ReadKey();
}
/// <summary>
/// 图片备份
/// </summary>
/// <param name="csvfile"></param>
/// <remarks>将宝贝图片按结构备份到Temp文件夹</remarks>
static void BackupTBImgage(string csvfile)
{
if (!File.Exists(csvfile))
throw new FileNotFoundException(csvfile + "文件不存在.");
//1.变量
string csvcontent = File.ReadAllText(csvfile, csvEncoding);
string destDir = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
string tmpfile = string.Empty;
//2.正则搜索
MatchCollection matches = Regex.Matches(csvcontent, "http://[^\";]+(jpg|gif|png|bmp|jpeg)", RegexOptions.IgnoreCase);
var query = from Match item in matches
group item by item.Value into g
select g.First().Value;
//3.准备文件夹
if (Directory.Exists(destDir))
Directory.Delete(destDir, true);
//4.图片备份
foreach (var item in query)
{
tmpfile = item.Replace("http://", destDir).Replace("/", "\\");
try
{
if (!Directory.Exists(Path.GetDirectoryName(tmpfile)))
Directory.CreateDirectory(Path.GetDirectoryName(tmpfile));
(new WebClient()).DownloadFile(item, tmpfile);
Console.WriteLine(Path.GetFileName(item) + ",ok.");
}
catch (Exception ex)
{
File.AppendAllText(destDir + "error.log", item + "\r\n" + ex.Message + "\r\n");
Console.WriteLine(Path.GetFileName(item) + ",error.");
}
}
}
/// <summary>
/// 图片搬家
/// </summary>
/// <param name="csvfile"></param>
/// <param name="url"></param>
/// <remarks>
/// 1).淘宝助理导出csv;
/// 2).下载图片并生成新csv;
/// 3).将下载的图片上传至服务器;
/// 4).csv导入淘宝助理更新至店铺;
/// </remarks>
static void UpdateTBImage(string csvfile, string url)
{
UpdateTBImage(csvfile, url, 0);
}
/// <summary>
/// 图片搬家
/// </summary>
/// <param name="csvfile"></param>
/// <param name="url"></param>
/// <param name="init"></param>
static void UpdateTBImage(string csvfile, string url, int init)
{
if (!File.Exists(csvfile))
throw new FileNotFoundException(csvfile + "文件不存在.");
//1.变量
string csvcontent = File.ReadAllText(csvfile, csvEncoding);
string destDir = AppDomain.CurrentDomain.BaseDirectory + "Temp\\tbimg\\";
string tmpfile = string.Empty;
int counter = init;
url += url.EndsWith("/") ? "tbimg/" : "/tbimg/";
//2.正则搜索
MatchCollection matches = Regex.Matches(csvcontent, "http://[^\";]+(jpg|gif|png|bmp|jpeg)", RegexOptions.IgnoreCase);
var query = from Match item in matches
group item by item.Value into g
select g.First().Value;
//3.准备文件夹
if (Directory.Exists(destDir))
Directory.Delete(destDir, true);
Directory.CreateDirectory(destDir);
//4.图片搬家
foreach (var item in query)
{
tmpfile = string.Format("{0}{1:D4}{2}", destDir, ++counter, Path.GetExtension(item));
try
{
(new WebClient()).DownloadFile(item, tmpfile);
csvcontent = csvcontent.Replace(item, url + Path.GetFileName(tmpfile));
Console.WriteLine(Path.GetFileName(item) + ",ok.");
}
catch (Exception ex)
{
File.AppendAllText(destDir + "error.log", item + "\r\n" + ex.Message + "\r\n");
Console.WriteLine(Path.GetFileName(item) + ",error.");
}
}
File.WriteAllText(destDir + "new.csv", csvcontent, csvEncoding);
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.387 ms

返回

标题: 淘宝助理辅助工具

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.387 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.449 ms

返回