-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
148 lines (137 loc) · 5.41 KB
/
Program.cs
File metadata and controls
148 lines (137 loc) · 5.41 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
namespace SlackFileDownload
{
// Extensions to string
public static class Extensions
{
public static int IndexOfNth (this string str, string value, int n)
{
return str.IndexOfNth(value, n, 0, str.Length);
}
public static int IndexOfNth (this string str, string value, int n, int startIndex)
{
return str.IndexOfNth(value, n, startIndex, str.Length);
}
public static int IndexOfNth (this string str, string value, int n, int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfLessThan(n, 1);
int position = startIndex-1;
for (int i = 1; i <= n; i++)
{
position = str.IndexOf(value, position + 1, count - (position + 1 - startIndex));
if (position < 0) return position;
}
return position;
}
public static int IndexOfNth (this string str, char value, int n)
{
return str.IndexOfNth(value, n, 0, str.Length);
}
public static int IndexOfNth (this string str, char value, int n, int startIndex)
{
return str.IndexOfNth(value, n, startIndex, str.Length);
}
public static int IndexOfNth (this string str, char value, int n, int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfLessThan(n, 1);
int position = startIndex-1;
for (int i = 1; i <= n; i++)
{
position = str.IndexOf(value, position + 1, count - (position + 1 - startIndex));
if (position < 0) return position;
}
return position;
}
}
// JSON schema
public class Message
{
public string type { get; set; }
public FileMessage[] files { get; set; }
}
public class FileMessage
{
public string name { get; set; }
public string url_private_download { get; set; }
}
public class FileMessageComparer : IComparer<FileMessage>
{
public int Compare(FileMessage x, FileMessage y)
{
return string.Compare(x.url_private_download, y.url_private_download);
}
}
// Main program
class Program
{
static void Main(string[] args)
{
// Initialize FileMessage store
var fms = new SortedSet<FileMessage>(new FileMessageComparer());
// Set current directory
string target = args.Length > 0 ? args[0] : Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(target);
// Find all downloadable files
string[] directories = Directory.GetDirectories(".");
foreach (string dir in directories)
{
string[] jsons = Directory.GetFiles(dir, "*.json");
foreach (string json in jsons)
{
Console.WriteLine("Processing file: {0}", json);
string jsonString = File.ReadAllText(json);
var messages = JsonSerializer.Deserialize<Message[]>(jsonString);
foreach (var m in messages)
{
if (m.type == @"message" && m.files is not null)
{
foreach (var fm in m.files)
{
if (fm.url_private_download is not null)
{
var success = fms.Add(fm);
Console.Write(success ? "Added: " : "Failed: ");
Console.WriteLine(fm.url_private_download);
}
}
}
}
}
}
Console.WriteLine("Total files: {0}", fms.Count);
// Download files
if (fms.Count > 0)
{
target = Directory.GetCurrentDirectory() + "\\files"; // ensure absolute path
Directory.CreateDirectory(target);
var client = new HttpClient();
foreach (var fm in fms)
{
string url = fm.url_private_download;
Console.Write("Processing URL: {0}", url);
int dirStart = url.IndexOfNth('/', 4) + 1;
int dirEnd = url.IndexOf('/', dirStart);
string dirName = url[dirStart..dirEnd];
if (string.IsNullOrEmpty(fm.name))
{
int fileStart = url.IndexOf('/', dirEnd + 1) + 1;
int fileEnd = url.IndexOf('?', fileStart);
fm.name = url[fileStart..fileEnd];
}
Directory.SetCurrentDirectory(target);
Directory.CreateDirectory(dirName);
Directory.SetCurrentDirectory(dirName);
using (var httpStream = client.GetStreamAsync(url).Result)
using (var fileStream = new FileStream(fm.name, FileMode.Create))
httpStream.CopyTo(fileStream);
Console.WriteLine(@" (DONE)");
}
}
Console.WriteLine(@"All done.");
}
}
}