public Map getBareGmailMessageDetails(String messageId) { Map<String, Object> messageDetails = new HashMap<String, Object>(); try { Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName("Gmail Quickstart") .build(); Message message = service .users() .messages() .get("me", messageId) .setFormat("full") .setFields("id,payload,sizeEstimate,snippet,threadId") .execute(); List<MessagePartHeader> headers = message.getPayload().getHeaders(); for (MessagePartHeader header : headers) { if (header.getName().equals("From") || header.getName().equals("Date") || header.getName().equals("Subject") || header.getName().equals("To") || header.getName().equals("CC")) { messageDetails.put(header.getName().toLowerCase(), header.getValue()); } } messageDetails.put("snippet", message.getSnippet()); messageDetails.put("threadId", message.getThreadId()); messageDetails.put("id", message.getId()); // messageDetails.put("body",message.getPayload().getBody().getData()); } catch (IOException ex) { Logger.getLogger(GoogleAuthHelper.class.getName()).log(Level.SEVERE, null, ex); } return messageDetails; }
@Override public File download(Date date) { LOGGER.fine("==starting gmail download...."); try { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, -1); Date after = calendar.getTime(); calendar.setTime(date); calendar.add(Calendar.DATE, 1); Date before = calendar.getTime(); String query = "after:" + dateFormat.format(after) + " AND before:" + dateFormat.format(before) + " AND has:attachment AND (filename:.puz OR filename:.jpz)"; LOGGER.info("Running query: " + query); ListMessagesResponse response = gmailService.users().messages().list("me").setQ(query).execute(); LOGGER.fine("==Found " + neverNull(response.getMessages()).size() + " messages."); for (Message message : neverNull(response.getMessages())) { String singleFilename = ""; Message fetched = gmailService.users().messages().get("me", message.getId()).execute(); LOGGER.fine("==Fetched message " + fetched.getId()); HashMap<String, MessagePart> toDownload = new HashMap<>(); scanParts(fetched.getPayload().getParts(), toDownload); for (Map.Entry<String, MessagePart> entry : toDownload.entrySet()) { LOGGER.info("==Reading : " + singleFilename); String source = getSender(fetched.getPayload().getHeaders()); String filename = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + source + "-" + singleFilename.replaceAll(" ", "") + ".puz"; File destination = new File(CROSSWORDS, filename); if (!destination.exists()) { byte[] data = entry.getValue().getBody().getAttachmentId() != null ? gmailService .users() .messages() .attachments() .get("me", message.getId(), entry.getValue().getBody().getAttachmentId()) .execute() .decodeData() : entry.getValue().getBody().decodeData(); Puzzle puzzle = singleFilename.endsWith("jpz") ? JPZIO.readPuzzle(new ByteArrayInputStream(data)) : IO.loadNative(new DataInputStream(new ByteArrayInputStream(data))); puzzle.setDate(date); puzzle.setSource(source); puzzle.setSourceUrl("gmail://" + fetched.getId()); puzzle.setUpdatable(false); date = getSentDate(fetched.getPayload().getHeaders()); LOGGER.info("Downloaded " + filename); IO.save(puzzle, destination); } } } } catch (IOException e) { e.printStackTrace(); } return null; }