PageSpeed API-Java中的批处理

时间:2019-06-18 11:11:14

标签: java pagespeed google-pagespeed

我在Java应用程序中使用PageSpeed API。它工作正常,但是由于我需要检查许多URL,因此需要进行一些批处理。我一直在网上寻找他们的文档,但找不到任何相关方法。 有人知道吗?

我正在连接一个普通的HTTP请求,因为事实证明它比使用他们的包要快,但是我认为对于批处理来说,他们的包会更好,所以这是我在两种情况下使用的代码:

HTTP请求:

String call = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + SITE + "&key=" + GOOGLE_KEY + "&strategy=" + DEVICE;

URL url = new URL(call);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(response.toString());

PageSpeed Java软件包:

JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
    @Override
    public void initialize(HttpRequest httpRequest) throws IOException {
        httpRequest.setReadTimeout(60000); // 60 seconds
    }
};

Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName("APP_NAME").build();
Pagespeedonline.Pagespeedapi.Runpagespeed runpagespeed = p.pagespeedapi().runpagespeed(SITE).setKey(GOOGLE_KEY).setStrategy(DEVICE);
PagespeedApiPagespeedResponseV5 response = runpagespeed.execute();

两者都工作正常,我只需要知道如何将批处理集成到两者中即可。

有人可以帮助我吗?

非常感谢您!

1 个答案:

答案 0 :(得分:0)

我设法做到了,但是我忘了分享,这里是:

import csv
import os
import subprocess

startdir = '.'   
outdir = '.'
suffix = '.csv'

def decode_to_file(cmd, in_file, new_suffix):
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    filename = outdir + '/' + in_file[len(startdir):-len(suffix)] + new_suffix
    os.makedirs(os.path.dirname(filename), exist_ok=True)
    csv_writer = csv.writer(open(filename, 'w'))
    for line_bytes in proc.stdout:
        line_str = line_bytes.decode('utf-8')
        csv_writer.writerow(line_str.strip().split(','))

for root, dirs, files in os.walk(startdir):
    for name in files:
        if not name.endswith(suffix):
            continue
        in_file = os.path.join(root, name)

        decode_to_file(
            cmd= [source== "ac:37:43:9b:92:24" && Receiver address== "8c:15:c7:3a:d0:1a"],
            in_file=in_file,
            new_suffix='.out.csv'
        )
        decode_to_file(
            cmd= [Transmitter address == "8c:15:c7:3a:d0:1a" && Destination== "ac:37:43:9b:92:24"],
            in_file=in_file,
            new_suffix='.in.csv'
        )

这是回调:

JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
   @Override
   public void initialize(HttpRequest httpRequest) throws IOException {
        httpRequest.setReadTimeout(60000); // 60 seconds
    }
};

BatchRequest batch = p.batch(httpRequestInitializer);
Pagespeedonline p = new Pagespeedonline.Builder(transport, jsonFactory, httpRequestInitializer).setApplicationName(APP_NAME).build();

//Loop to add as many as you want
for (int i = 0; i < urls.size(); ++i) {
    Pagespeedonline.Pagespeedapi.Runpagespeed check = p.pagespeedapi().runpagespeed(urls.get(i)).setKey(GOOGLE_KEY).setStrategy(DEVICE);
    check.queue(batch, callback);
}

batch.execute();