-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (85 loc) · 3.35 KB
/
Program.cs
File metadata and controls
110 lines (85 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
namespace VkSaver2
{
class Program
{
static void Main(string[] args)
{
HtmlDocument htmlDoc = new HtmlDocument();
var webClient = new WebClient();
var helpers = new Helpers();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encoding = Encoding.GetEncoding("windows-1251");
Console.OutputEncoding = Encoding.Unicode;
var fileNumber = 0;
foreach (var folder in Directory.GetDirectories("/Projects/VkSaver2/Archive/messages"))
{
foreach (var file in Directory.GetFiles(folder).Where(file => file.EndsWith(".html")))
{
fileNumber++;
var fullHtml = File.ReadAllText(file, encoding);
htmlDoc.LoadHtml(fullHtml);
var nameNode = htmlDoc.DocumentNode.Descendants(0)
.Where(n => n.HasClass("ui_crumb") && n.Name == "div");
string name = "";
if (nameNode.Any())
name = nameNode.FirstOrDefault().InnerText;
Console.WriteLine(fileNumber + ": " + name);
var nodes = htmlDoc.DocumentNode.Descendants(0)
.Where(n => n.HasClass("attachment__link"));
bool needToSave = false;
foreach (var node in nodes.ToList())
{
var link = node.InnerText;
if (link.EndsWith(".jpg") && !node.ParentNode.ChildNodes.Any(m => m.Name == "img"))
{
var imageName = Guid.NewGuid().ToString() + ".jpg";
var downloaded = helpers.Download(webClient, link, folder + "/" + imageName);
if (!downloaded)
continue;
var imgHtml = @"<img src=""" + imageName + @""">";
var imgNode = HtmlNode.CreateNode(imgHtml);
node.ParentNode.AppendChild(imgNode);
needToSave = true;
}
}
if (needToSave)
{
string result = null;
using (StringWriter writer = new StringWriter())
{
htmlDoc.Save(writer);
result = writer.ToString();
}
File.WriteAllText(file, result, encoding);
}
}
}
}
public class Helpers
{
public bool Download(WebClient webClient, string link, string path)
{
int sleep = 0;
try
{
webClient.DownloadFile(new Uri(link), path);
Thread.Sleep(sleep);
return true;
}
catch
{
return false;
}
}
}
}
}