1 |
// Google Tron |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <api/exename.hpp> |
10 |
#include <api/pcre/regex.hpp> |
11 |
#include <api/process.hpp> |
12 |
#include <app/simple.hpp> |
13 |
#include <xml/textwriter.hpp> |
14 |
|
15 |
#include "GoogleTron.hpp" |
16 |
#include "GzipWriter.hpp" |
17 |
#include "Sleep.hpp" |
18 |
|
19 |
int Main(const app::Options &options) |
20 |
{ |
21 |
cse::String sitemap(_B("sitemap.gz")); |
22 |
|
23 |
api::Pcre::RegEx sitemap_(_B("^-sitemap=(.+)$")); |
24 |
|
25 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
26 |
{ |
27 |
api::Pcre::RegEx::Match match; |
28 |
|
29 |
if (match = sitemap_(*arg)) |
30 |
sitemap = match[1]; |
31 |
else |
32 |
{ |
33 |
api::Cout << _B("Usage: ") << api::GetExecutablePath().GetName() << _B(" [-sitemap=sitemap]") << ios::NewLine; |
34 |
|
35 |
return 1; |
36 |
} |
37 |
} |
38 |
|
39 |
GoogleTron tron(sitemap); |
40 |
|
41 |
return 0; |
42 |
} |
43 |
|
44 |
GoogleTron::GoogleTron(const cse::String &sitemap) : working(true), sitemap(sitemap) |
45 |
{ |
46 |
// XXX: start some threads |
47 |
} |
48 |
|
49 |
GoogleTron::~GoogleTron() |
50 |
{ |
51 |
// XXX: wait on threads |
52 |
|
53 |
working = false; |
54 |
|
55 |
// XXX: wait on output thread |
56 |
} |
57 |
|
58 |
int GoogleTron::Output() |
59 |
{ |
60 |
_S<GzipWriter> gzip(sitemap); |
61 |
_S<xml::TextWriter> writer(gzip); |
62 |
xml::ScopeElement urlset(writer, _B("urlset")); |
63 |
|
64 |
writer.SetAttribute(_B("xmlns"), _B("http://www.google.com/schemas/sitemap/0.84")); |
65 |
writer.SetAttribute(_B("xmlns:xsi"), _B("http://www.w3.org/2001/XMLSchema-instance")); |
66 |
writer.SetAttribute(_B("xsi:schemaLocation"), _B("http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd")); |
67 |
|
68 |
do |
69 |
{ |
70 |
Sleep(); |
71 |
|
72 |
_synchronized (queueLock) |
73 |
if (queue.size()) |
74 |
{ |
75 |
xml::ScopeElement url_(writer, _B("url")); |
76 |
const Url &url(queue.front()); |
77 |
|
78 |
{ |
79 |
xml::ScopeElement loc(writer, _B("loc")); |
80 |
|
81 |
writer.OutputText(url.GetLocation()); |
82 |
} |
83 |
|
84 |
{ |
85 |
xml::ScopeElement lastmod(writer, _B("lastmod")); |
86 |
|
87 |
writer.OutputText(url.GetModified()); |
88 |
} |
89 |
|
90 |
{ |
91 |
xml::ScopeElement changefreq(writer, _B("changefreq")); |
92 |
|
93 |
writer.OutputText(url.GetFrequency()); |
94 |
} |
95 |
|
96 |
{ |
97 |
xml::ScopeElement priority(writer, _B("priority")); |
98 |
|
99 |
writer.OutputText(url.GetPriority()); |
100 |
} |
101 |
|
102 |
queue.pop(); |
103 |
} |
104 |
} |
105 |
while (working); |
106 |
|
107 |
return 0; |
108 |
} |