kirisame commited on
Commit
5e142d8
·
1 Parent(s): da46b41

Implement Discord downloading

Browse files
Files changed (2) hide show
  1. origins/discord.json +116 -0
  2. utils/discord_parse.py +40 -9
origins/discord.json ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "guilds": [
3
+ {
4
+ "name": "The Land of Illusions and Fantasy",
5
+ "invite": "https://discord.gg/yQexURAAU4",
6
+ "channels": [
7
+ 848720500515405825, 857375832301502504, 872177434223595530, 844105873848008736, 844105841719115776, 844106037237252117, 844101345605844992, 844102456655740938, 873683098192183316, 873686417824423946, 845005740116803624, 865722150937296936, 861433031260373012, 871833472401031238, 888123892365951066, 898331237322743848, 904840362332397578, 918984713555820594, 863317608416935936, 863317665958199326, 886822020296376341, 844118397167599646, 869409251888627732, 869410865047273532, 869411167293038593, 869412982302593104, 869411963824922684, 869410273344258068, 869413006029770812, 869411366522478652, 853459241179807774, 876218794740174868, 878502051208888370, 882358565162065970, 887911739507769396, 871505287129997332, 871506078217674802, 845724712025981050, 871505188052172831, 930864374623637525, 846212628035928076, 871504446050414642, 871504478594019348, 882712467468320798, 880153160147161178, 883408908696309772, 883460413948297217, 875080018030657597, 872628684991889418, 875080387934715935, 875844934039642132, 875844969850630144, 878519410996944946, 872629059211907072, 884908250519187496, 888957964738789386, 885666749494747218, 885977293787381811, 887411100851781682, 888532018177138728, 905896666564026418, 932438089769558076, 872628905369034772, 883402336893022218, 886416030098944030, 886420462266122240, 844993011510935612, 889290745855500319, 845079569295474728, 896822703791702067 ]
8
+ },
9
+ {
10
+ "name": "東方Project AI",
11
+ "invite": "https://discord.gg/Sx6Spmsgx7",
12
+ "channels": [
13
+ 930499731451428926, 950914873800396841, 930561058144411649, 953132470528798811, 950914842708050021
14
+ ]
15
+ },
16
+ {
17
+ "name": "Touhou Wiki",
18
+ "invite": "https://discord.gg/jfSNFZG",
19
+ "channels": [
20
+ 296119300195090435, 296253672558690304, 436928453464555521
21
+ ]
22
+ },
23
+ {
24
+ "name": "「 Official THP Server ~ No Time to Slack, but We Do So Anyway 」",
25
+ "invite": "https://discord.com/invite/UPHZMWy",
26
+ "channels": [
27
+ 414466323570098176, 414466298375045120
28
+ ]
29
+ },
30
+ {
31
+ "name": "Era Games General /egg/",
32
+ "invite": "https://discord.gg/PxrAKAuKAU",
33
+ "channels": [
34
+ 484076945223581698
35
+ ]
36
+ },
37
+ {
38
+ "name": "DoujinStyle",
39
+ "invite": "https://discord.gg/doujinstyle",
40
+ "channels": [
41
+ 398274632408629252, 398276036611014676
42
+ ]
43
+ },
44
+ {
45
+ "name": "Optimalverse",
46
+ "invite": "https://discord.gg/evQV7mr3ge",
47
+ "channels": [
48
+ 332189065954983936, 699006506368106696, 415315246992064512
49
+ ]
50
+ },
51
+ {
52
+ "name": "Touhou Project",
53
+ "invite": "https://discord.gg/touhou",
54
+ "channels": [
55
+ 716151735692820501, 165372974415740928, 391375957229043712, 203320155697184768, 318808251330199552
56
+ ]
57
+ },
58
+ {
59
+ "name": "Touhou LostWord Official",
60
+ "invite": "https://discord.gg/touhoulostword",
61
+ "channels": [
62
+ 612698272385073154, 612698029237207088
63
+ ]
64
+ },
65
+ {
66
+ "name": "EleutherAI",
67
+ "invite": "https://discord.com/invite/zBGx3azzUn",
68
+ "channels": [
69
+ 729741769738158194, 730095596861521970, 730451873613611079, 747850033994662000
70
+ ]
71
+ },
72
+ {
73
+ "name": "KoboldAI",
74
+ "invite": "https://discord.com/invite/XuQWadgU9k",
75
+ "channels": [
76
+ 849938666612457502, 849938730617405440, 888235052054491146
77
+ ]
78
+ },
79
+ {
80
+ "name": "Danbooru",
81
+ "invite": "https://discord.gg/g7ptmcemFh",
82
+ "channels": [
83
+ 707034424478531595
84
+ ]
85
+ },
86
+ {
87
+ "name": "The Programmer's Hangout",
88
+ "invite": "https://discord.gg/programming",
89
+ "channels": [
90
+ 749496350047207474, 327869408389365760
91
+ ]
92
+ },
93
+ {
94
+ "name": "Touhou Paradise",
95
+ "invite": "https://discord.gg/Rb7mxz4DcT",
96
+ "channels": [
97
+ 545369569011826688, 595416872715812883
98
+ ]
99
+ },
100
+ {
101
+ "name": "Logic World",
102
+ "invite": "https://discord.gg/HBggVw5nCS",
103
+ "channels": [
104
+ 416276124977332226, 631004929762525204
105
+ ]
106
+ },
107
+ {
108
+ "name": "Touhou Town",
109
+ "invite": "https://discord.gg/4z9rRCn2qY",
110
+ "channels": [
111
+ 833435613654810684, 801642678420766721, 880122670094631002,
112
+ 806974398603067434, 907047495794823200, 905154526452006992, 907686502933340201, 806974926087127040, 806974844607397918, 907661903810166924, 909544005140897822, 907415206429343865, 914217367242948660, 911071208836063242, 896434286356463658, 893627983431094292, 891815821511368755, 891815882152624199, 907685455573708861, 911948393943810078, 914254932415221780, 894050719966064680, 891815988310442005, 891815905066123274, 900120994507808798, 891815965568929802, 907691167037157456, 909804563043844147, 891815952570789898, 907659927022108773, 907660252655288330
113
+ ]
114
+ }
115
+ ]
116
+ }
utils/discord_parse.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import json
2
  import argparse
3
  import tqdm
@@ -6,12 +7,13 @@ import glob
6
  from clean import clean
7
 
8
  def format_channel(metadata):
9
- return f'[Name: #{metadata["name"]}; Description: {metadata["topic"]};]'
10
 
11
  def worker_parse(filename, out_filename=None, **kwargs):
12
  data = json.load(open(filename, 'r', encoding='utf-8'))
13
  messages = data['messages']
14
  metadata = data['channel']
 
15
 
16
  msgs = []
17
 
@@ -43,19 +45,48 @@ def worker_parse(filename, out_filename=None, **kwargs):
43
  with open(out_filename, 'w', encoding='utf-8') as f:
44
  f.write('⁂\n'+format_channel(metadata)+'\n⁂\n')
45
  f.write('\n'.join(msgs))
46
-
47
- parser = argparse.ArgumentParser(description='Process Discord JSONs')
48
- parser.add_argument('-f', '--file', type=str, help='file to process', required=False)
49
- parser.add_argument('-a', '--anonymous', action='store_true', help='anonymous author')
50
- parser.add_argument('-i', '--in_dir', type=str, help='directory to process', required=False)
51
- parser.add_argument('-o', '--out_dir', type=str, help='directory to output', required=False)
52
- args = parser.parse_args()
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  if args.file:
56
  worker_parse(args.file, anonymous=args.anonymous)
57
-
58
  if args.in_dir and args.out_dir:
 
 
59
  files = glob.glob(args.in_dir+'/*.json')
60
  for file in files:
61
  try:
 
1
+ import os
2
  import json
3
  import argparse
4
  import tqdm
 
7
  from clean import clean
8
 
9
  def format_channel(metadata):
10
+ return f'[Name: #{metadata["name"]}; Description: {metadata["topic"]}; Guild: {metadata["guild"]}]'
11
 
12
  def worker_parse(filename, out_filename=None, **kwargs):
13
  data = json.load(open(filename, 'r', encoding='utf-8'))
14
  messages = data['messages']
15
  metadata = data['channel']
16
+ metadata['guild'] = data['guild']['name']
17
 
18
  msgs = []
19
 
 
45
  with open(out_filename, 'w', encoding='utf-8') as f:
46
  f.write('⁂\n'+format_channel(metadata)+'\n⁂\n')
47
  f.write('\n'.join(msgs))
48
+
49
+ def worker_dl(channel_id_path, auth_token):
50
+ # channel_id_path is a json
51
+ channel_id_data = json.load(open(channel_id_path, 'r', encoding='utf-8'))
52
+ channel_ids = []
53
+ print('Loading channel ids\nYou need to be in the following guilds to construct the dataset.\n')
54
+ for guild in channel_id_data['guilds']:
55
+ print(f'[Guild: {guild["name"]}; Invite: {guild["invite"]};]')
56
+ for channel in guild['channels']:
57
+ channel_ids.append(str(channel))
58
+ print(f'\nTotal channels: {len(channel_ids)}\nTotal guilds: {len(channel_id_data["guilds"])}\n')
59
+ print('Downloading messages...')
60
+ channel_ids = ' '.join(channel_ids)
61
+ os.system(f'discord-chat-exporter-cli export -t {auth_token} -f Json -c {channel_ids}')
62
+ print('Moving files...')
63
+ if not os.path.exists('raw/discord/'):
64
+ os.mkdir('raw/discord/')
65
+ for filename in glob.glob('*.json'):
66
+ os.rename(filename, f'raw/discord/{filename}')
67
 
68
  if __name__ == '__main__':
69
+ parser = argparse.ArgumentParser(description='Process Discord JSONs')
70
+ parser.add_argument('-f', '--file', type=str, help='file to process', required=False)
71
+ parser.add_argument('-a', '--anonymous', action='store_true', help='anonymous author')
72
+ parser.add_argument('-i', '--in_dir', type=str, help='directory to process', required=False, default='./raw/discord')
73
+ parser.add_argument('-o', '--out_dir', type=str, help='directory to output', required=False, default='./data/discord')
74
+ parser.add_argument('-d', '--dl', type=str, help='json file containing channel IDs to download', required=False)
75
+ parser.add_argument('-t', '--token', type=str, help='discord auth token', required=False)
76
+ args = parser.parse_args()
77
+
78
+ if args.dl:
79
+ if not args.token:
80
+ print('Please provide a Discord auth token')
81
+ exit(1)
82
+ worker_dl(args.dl, args.token)
83
+ exit()
84
  if args.file:
85
  worker_parse(args.file, anonymous=args.anonymous)
86
+ exit()
87
  if args.in_dir and args.out_dir:
88
+ if not os.path.exists(args.out_dir):
89
+ os.mkdir(args.out_dir)
90
  files = glob.glob(args.in_dir+'/*.json')
91
  for file in files:
92
  try: