-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (101 loc) · 4.52 KB
/
Copy pathProgram.cs
File metadata and controls
117 lines (101 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace duplicates
{
class Program
{
static readonly string[] Scopes = { DriveService.Scope.Drive, DriveService.Scope.DriveMetadata, DriveService.Scope.DriveFile };
static readonly string ApplicationName = "Duplo";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 1000;
listRequest.Fields = "*";
listRequest.Q = "mimeType = 'image/jpeg' or mimeType = 'image/png' or mimeType = 'video/mp4' or mimeType = 'video/mpeg'";
FileList fileList = null;
IList<Google.Apis.Drive.v3.Data.File> files = new List<Google.Apis.Drive.v3.Data.File>();
do
{
if (fileList != null) listRequest.PageToken = fileList.NextPageToken;
fileList = listRequest.Execute();
files = files.Concat(fileList.Files).ToList();
} while (fileList.NextPageToken != null);
var fileGroups = files
.GroupBy(g => new { g.Name, g.Size })
.Where(c => c.Count() > 1);
var duplicates = new List<Google.Apis.Drive.v3.Data.File>();
string output = "";
foreach (var group in fileGroups.OrderBy(o => o.Count()))
{
string groupInfo = string.Format("Group: {0} - Group Count: {1}", group.Key, group.Count());
Console.WriteLine(groupInfo);
output += groupInfo + Environment.NewLine;
var origin = group.OrderBy(o => o.CreatedTimeRaw).FirstOrDefault();
if (origin != null)
{
string originInfo = string.Format("Origin:{0} - created:{1}", origin.Id, origin.CreatedTimeRaw);
Console.WriteLine(originInfo);
output += originInfo + Environment.NewLine;
}
foreach (var file in group)
{
string indicatorSign = "+";
if (file.Id != origin.Id)
{
duplicates.Add(file);
indicatorSign = "-";
}
string info = string.Format("{0};{1};{2};{3};{4};{5};{6};",
indicatorSign, file.Id, file.Name, file.Size, file.CreatedTime.Value.ToShortDateString(),
file.Md5Checksum, file.Parents.Aggregate((a, b) => a + ";" + b));
Console.WriteLine(info);
output += info + Environment.NewLine;
}
}
string delInfoMessage = "----------------------DELETE";
Console.WriteLine(delInfoMessage);
output += delInfoMessage + Environment.NewLine;
long? total = 0;
foreach (var duplicate in duplicates)
{
total += duplicate.Size;
service.Files.Delete(duplicate.Id).Execute();
}
string delInfo = string.Format("Total bytes:{0}", total);
Console.WriteLine(delInfo);
output += delInfo + Environment.NewLine;
System.IO.File.WriteAllText("grdrive.txt", output);
Console.Read();
}
}
}