Skip to content

Commit 2eed028

Browse files
author
Jonathan Kim
committedJan 29, 2014
Script for backing up any URL to Google Drive.
1 parent 54ae49d commit 2eed028

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
 

‎backup-url.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
Create backups of the data returned by any URL and save them to Google Drive.
3+
This is going to create a folder structure that looks like this:
4+
5+
/Backups
6+
/2014-01-01
7+
/2014-01-01T00:00:00.html
8+
/2014-01-01T01:00:00.html
9+
/2014-01-01T02:00:00.html
10+
.. etc..
11+
*/
12+
13+
var RESOURCE_URL = 'https://news.ycombinator.com/news',
14+
BACKUP_FOLDER_ID = '<THE_ID_OF_THE_FOLDER_WHERE_YOUR_BACKUPS_WILL_LIVE>',
15+
FOLDER_NAME_FORMAT = 'yyyy-MM-dd',
16+
FILE_NAME_FORMAT = "yyyy-MM-dd'T'HH:00:00",
17+
18+
// Customize your file extension.
19+
FILE_EXT = '.html',
20+
21+
// Folder names are all going to look like this.
22+
now = new Date(),
23+
FOLDER_NAME = Utilities.formatDate(now, 'GMT', FOLDER_NAME_FORMAT),
24+
FILE_NAME = Utilities.formatDate(now, 'GMT', FILE_NAME_FORMAT) + FILE_EXT;
25+
26+
27+
function createBackup() {
28+
var folder = getFolder(FOLDER_NAME);
29+
createBackupFile(folder, FILE_NAME, fetchData());
30+
}
31+
32+
// Ensures we're always working within the backup directory.
33+
function getFolder(name) {
34+
var backupFolder = getBackupFolder(),
35+
folders = backupFolder.getFoldersByName(name);
36+
37+
if (folders.hasNext()) {
38+
// Just get the first one.
39+
folder = folders.next();
40+
} else {
41+
folder = backupFolder.createFolder(name);
42+
}
43+
return folder;
44+
}
45+
46+
// Returns the root folder where our backups exist.
47+
function getBackupFolder() {
48+
return DriveApp.getFolderById(BACKUP_FOLDER_ID);
49+
}
50+
51+
function createBackupFile(folder, filename, data, overwrite) {
52+
if (overwrite) {
53+
// Techincally we're not overwriting here. We're just deleting
54+
// the duplicates.
55+
var existingFiles = folder.getFilesByName(filename);
56+
while (existingFiles.hasNext()) {
57+
var file = existingFiles.next();
58+
folder.removeFile(file);
59+
}
60+
}
61+
folder.createFile(filename, data);
62+
}
63+
64+
// NOTE: You could change this URL to be anything you want.
65+
function fetchData() {
66+
var exportUrl = RESOURCE_URL;
67+
return UrlFetchApp.fetch(exportUrl);
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.