1 |
douglas |
672 |
// Google Tron |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include <cxx/standard.hh> |
8 |
|
|
|
9 |
douglas |
679 |
#include <api/exename.hpp> |
10 |
|
|
#include <api/pcre/regex.hpp> |
11 |
douglas |
673 |
#include <api/process.hpp> |
12 |
douglas |
672 |
#include <app/simple.hpp> |
13 |
douglas |
680 |
#include <hop/bind.hpp> |
14 |
douglas |
673 |
#include <xml/textwriter.hpp> |
15 |
douglas |
672 |
|
16 |
douglas |
676 |
#include "GoogleTron.hpp" |
17 |
douglas |
677 |
#include "Sleep.hpp" |
18 |
douglas |
680 |
#include "Zlib/GzipWriter.hpp" |
19 |
douglas |
674 |
|
20 |
douglas |
688 |
int Usage() |
21 |
|
|
{ |
22 |
douglas |
696 |
api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=.+] -base=.+ [-fs=.*] [-blog=.*] [-wiki=.*] [...]") << ios::NewLine; |
23 |
douglas |
688 |
|
24 |
|
|
return 1; |
25 |
|
|
} |
26 |
|
|
|
27 |
douglas |
672 |
int Main(const app::Options &options) |
28 |
|
|
{ |
29 |
douglas |
688 |
cse::String sitemap(_B("sitemap.gz")), base; |
30 |
douglas |
686 |
_L<_R<Command> > commands; |
31 |
douglas |
676 |
|
32 |
douglas |
696 |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")), base_(_B("^-base=(.+)$")), fs(_B("^-fs=(.*)$")), blog(_B("^-blog=(.*)$")), wiki(_B("^-wiki=(.*)$")); |
33 |
douglas |
679 |
|
34 |
|
|
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
35 |
|
|
{ |
36 |
|
|
api::Pcre::RegEx::Match match; |
37 |
|
|
|
38 |
|
|
if (match = sitemap_(*arg)) |
39 |
|
|
sitemap = match[1]; |
40 |
douglas |
688 |
else if (match = base_(*arg)) |
41 |
|
|
base = match[1]; |
42 |
douglas |
696 |
else if (match = fs(*arg)) |
43 |
douglas |
686 |
commands.InsertLast(new _H<FileSystemCommand>(match[1])); |
44 |
douglas |
696 |
else if (match = blog(*arg)) |
45 |
douglas |
688 |
commands.InsertLast(new _H<BlogCommand>(match[1])); |
46 |
douglas |
696 |
else if (match = wiki(*arg)) |
47 |
|
|
commands.InsertLast(new _H<WikiCommand>(match[1])); |
48 |
douglas |
679 |
else |
49 |
douglas |
688 |
return Usage(); |
50 |
douglas |
679 |
} |
51 |
|
|
|
52 |
douglas |
688 |
if (base.IsEmpty() || commands.IsEmpty()) |
53 |
|
|
return Usage(); |
54 |
douglas |
679 |
|
55 |
douglas |
688 |
GoogleTron tron(sitemap, base, commands); |
56 |
|
|
|
57 |
douglas |
676 |
return 0; |
58 |
|
|
} |
59 |
|
|
|
60 |
douglas |
688 |
GoogleTron::GoogleTron(const cse::String &sitemap, const cse::String &base, const _L<_R<Command> > &commands) : working(true), sitemap(sitemap), base(base), output(hop::BindAll(&GoogleTron::Output, this)) |
61 |
douglas |
676 |
{ |
62 |
douglas |
686 |
_foreach (const _L<_R<Command> >, command_, commands) |
63 |
|
|
if (_R<FileSystemCommand> command = dynamic_cast<FileSystemCommand *>(command_->GetValue())) |
64 |
|
|
threads.Add(hop::BindAll(&GoogleTron::FileSystem_, this, command)); |
65 |
douglas |
688 |
else if (_R<BlogCommand> command = dynamic_cast<BlogCommand *>(command_->GetValue())) |
66 |
|
|
threads.Add(hop::BindAll(&GoogleTron::Blog_, this, command)); |
67 |
douglas |
696 |
else if (_R<WikiCommand> command = dynamic_cast<WikiCommand *>(command_->GetValue())) |
68 |
|
|
threads.Add(hop::BindAll(&GoogleTron::Wiki_, this, command)); |
69 |
douglas |
676 |
} |
70 |
|
|
|
71 |
|
|
GoogleTron::~GoogleTron() |
72 |
|
|
{ |
73 |
douglas |
681 |
threads.Join(); |
74 |
|
|
|
75 |
douglas |
677 |
working = false; |
76 |
|
|
|
77 |
douglas |
681 |
output.Join(); |
78 |
douglas |
676 |
} |
79 |
|
|
|
80 |
|
|
int GoogleTron::Output() |
81 |
|
|
{ |
82 |
douglas |
680 |
_S<Zlib::GzipWriter> gzip(sitemap); |
83 |
douglas |
674 |
_S<xml::TextWriter> writer(gzip); |
84 |
douglas |
673 |
xml::ScopeElement urlset(writer, _B("urlset")); |
85 |
|
|
|
86 |
|
|
writer.SetAttribute(_B("xmlns"), _B("http://www.google.com/schemas/sitemap/0.84")); |
87 |
douglas |
679 |
writer.SetAttribute(_B("xmlns:xsi"), _B("http://www.w3.org/2001/XMLSchema-instance")); |
88 |
|
|
writer.SetAttribute(_B("xsi:schemaLocation"), _B("http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd")); |
89 |
douglas |
673 |
|
90 |
douglas |
677 |
do |
91 |
|
|
{ |
92 |
|
|
Sleep(); |
93 |
|
|
|
94 |
|
|
_synchronized (queueLock) |
95 |
douglas |
695 |
if (queue.GetSize()) |
96 |
douglas |
677 |
{ |
97 |
|
|
xml::ScopeElement url_(writer, _B("url")); |
98 |
douglas |
695 |
const Url &url(queue.Front()); |
99 |
douglas |
677 |
|
100 |
|
|
{ |
101 |
|
|
xml::ScopeElement loc(writer, _B("loc")); |
102 |
|
|
|
103 |
|
|
writer.OutputText(url.GetLocation()); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
{ |
107 |
|
|
xml::ScopeElement lastmod(writer, _B("lastmod")); |
108 |
|
|
|
109 |
|
|
writer.OutputText(url.GetModified()); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
{ |
113 |
|
|
xml::ScopeElement changefreq(writer, _B("changefreq")); |
114 |
|
|
|
115 |
|
|
writer.OutputText(url.GetFrequency()); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
{ |
119 |
|
|
xml::ScopeElement priority(writer, _B("priority")); |
120 |
|
|
|
121 |
|
|
writer.OutputText(url.GetPriority()); |
122 |
|
|
} |
123 |
|
|
|
124 |
douglas |
695 |
queue.Pop(); |
125 |
douglas |
677 |
} |
126 |
|
|
} |
127 |
|
|
while (working); |
128 |
|
|
|
129 |
douglas |
672 |
return 0; |
130 |
|
|
} |
131 |
douglas |
686 |
|
132 |
|
|
int GoogleTron::FileSystem_(const _R<FileSystemCommand> &command) |
133 |
|
|
{ |
134 |
douglas |
688 |
FileSystem fs(sitemap, base, queue, queueLock, command); |
135 |
douglas |
686 |
|
136 |
|
|
return 0; |
137 |
|
|
} |
138 |
douglas |
688 |
|
139 |
|
|
int GoogleTron::Blog_(const _R<BlogCommand> &command) |
140 |
|
|
{ |
141 |
|
|
Blog blog(sitemap, base, queue, queueLock, command); |
142 |
|
|
|
143 |
|
|
return 0; |
144 |
|
|
} |
145 |
douglas |
696 |
|
146 |
|
|
int GoogleTron::Wiki_(const _R<WikiCommand> &command) |
147 |
|
|
{ |
148 |
|
|
Wiki wiki(sitemap, base, queue, queueLock, command); |
149 |
|
|
|
150 |
|
|
return 0; |
151 |
|
|
} |