-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSave_Blogger_XML_WithFixes.linq
102 lines (91 loc) · 4.14 KB
/
Save_Blogger_XML_WithFixes.linq
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
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Text.RegularExpressions.dll</Reference>
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference>
<Namespace>System.Windows.Forms</Namespace>
</Query>
void Main()
{
bool WriteTitleOnConsole = false;
bool TraceMode = false;
int maxLatestPost = 20;
string defaultFont = "Noto Sans";
Console.WriteLine("WriteTitleOnConsole is " + WriteTitleOnConsole + "; Set as true to see post titles");
Console.WriteLine("\tPost with changes will appear here");
Console.WriteLine("\tIf edit from Blogger img tags will be missing self-enclosing slash, format on web version to fix");
Console.WriteLine("==================================================================================================");
string archivepath = @"C:\Users\KAINENG\Documents\LINQPad Queries\blog-archive\";
string filepath = "";
string domainLink = "https://knwebreports.blogspot.com/";
//Get xml file from source, move to archivepath
//If not found in source, will run file in archivepath
string sourcepath = @"C:\Users\KAINENG\Downloads\";
string[] sources = Directory.GetFiles(sourcepath, "blog-*.xml");
if(sources.Length == 1)
{
Console.WriteLine("Source found; Moving to archivepath");
string[] dests = Directory.GetFiles(Path.GetDirectoryName(archivepath), "blog-*.xml");
if(dests.Length == 1)
{
if(TraceMode) Console.WriteLine("Destination file found; Moving to archive");
File.Delete(dests[0].Replace(archivepath, archivepath + @"archive\"));
File.Move(dests[0], dests[0].Replace(archivepath, archivepath + @"archive\"));
}
File.Move(sources[0], sources[0].Replace(sourcepath,archivepath));
}
else if(sources.Length == 0)
{
Console.WriteLine("No xml source found; proceed in " + archivepath);
}
else
{
Console.WriteLine("More than 1 source files found; proceed in " + archivepath);
}
//Get xml file to process
//Can only have exactly one file per query, else fail, require manual intervention
string[] xmls = Directory.GetFiles(Path.GetDirectoryName(archivepath), "blog-*.xml");
if(xmls.Length == 1)
{
Console.WriteLine("File found");
filepath = xmls[0];
}
else if(xmls.Length == 0)
{
Console.WriteLine("No xml files found");
return;
}
else
{
Console.WriteLine("More than 1 xml files found: Remove all but one source file in " + archivepath);
return;
}
//Read file
string text = File.ReadAllText(filepath);
//General text replace
text = text.Replace("\n ", " ").Replace(" ", " ").Replace(" ", " ").Replace(">\n<", "><").Replace("</a\n>", "</a>")
.Replace("The Entertainment News 2022 Edition Issue", "The Entertainment News '22 Issue"); // string literal in single quotes, use unicode
//Console.WriteLine(text.Substring(2416600, 40));
//Parse XML document
XDocument doc = XDocument.Parse(text);
// Use XNamespaces to deal with those pesky "xmlns" attributes.
// The underscore represents the default namespace.
var _ = XNamespace.Get("http://www.w3.org/2005/Atom");
var app = XNamespace.Get("http://purl.org/atom/app#");
// Filter posts from XML
doc.Root.Elements(_+"entry")
.Where(entry => !entry.Element(_+"category").Attribute("term").ToString().Contains("#template"))
.Where(entry => !entry.Element(_+"category").Attribute("term").ToString().Contains("#settings"))
.Where(entry => !entry.Element(_+"category").Attribute("term").ToString().Contains("#page"))
.Where(entry => !entry.Descendants(app+"draft").Any(draft => draft.Value != "no"))
.Where(entry =>
entry.Element(_+"published") != null &&
DateTime.TryParse(entry.Element(_+"published").Value, out DateTime publishDate) &&
publishDate.Year != 2022)
.Remove();
// Save
var outputFilename = filepath.Replace(".xml","-edit.xml");
if(File.Exists(outputFilename)) File.Delete(outputFilename);
doc.Save(outputFilename);
File.Delete(outputFilename.Replace(archivepath, archivepath + @"archive\"));
File.Move(outputFilename, outputFilename.Replace(archivepath, archivepath + @"archive\"));
Console.WriteLine("Saved.");
}