-
Notifications
You must be signed in to change notification settings - Fork 0
/
2stacks-get-user-metadata.py
97 lines (86 loc) · 5.54 KB
/
2stacks-get-user-metadata.py
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
import json
import boto3
from botocore.exceptions import ClientError
# import logging
import helpers
import requests
import re
import config
import os
# logger = logging.getLogger()
# logger.setLevel(logging.INFO)
def lambda_handler(event, context):
for record in event['Records']:
callback_url = record['messageAttributes']['callback_url']['stringValue']
user_id = record['messageAttributes']['user_id']['stringValue']
wikidot_site = record['messageAttributes']['wikidot_site']['stringValue']
# Get the basic info from wikidot.
data = {"user_id": user_id, 'moduleName': 'users/UserInfoWinModule'}
response = helpers.fetch(data, wikidot_site)
# Believe it or not, the next two patterns look for two different things. Thanks Wikidot.
# logger.info(response)
if wikidot_site == 'scp-ru': #SCP-RU
wd_registration_timestamp = re.search('(?:Wikidot.com с:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'lafundacionscp' or wikidot_site == 'scp-pt-br': # SCP-ES & -PT
wd_registration_timestamp = re.search('(?:desde:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'fondationscp': # SCP-FR
wd_registration_timestamp = re.search('(?:depuis:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-wiki-de': # SCP-DE
wd_registration_timestamp = re.search('(?:seit:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-pl': # SCP-PL
wd_registration_timestamp = re.search('(?:Wikidot.com od:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'fondazionescp': # SCP-IT
wd_registration_timestamp = re.search('(?:Wikidot dal:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-wiki-cn': # SCP-CN
wd_registration_timestamp = re.search('(?:使用者始于:)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scpko': # SCP-KO
wd_registration_timestamp = re.search('(?:Wikidot.com 사용자 시작:)(?:\D*)(\d*)', response).group(1)
else: # SCP-EN and English-speaking wikis, and a few translated sites (-UA, -CS, )
wd_registration_timestamp = re.search('(?:since:)(?:\D*)(\d*)', response).group(1)
try:
if wikidot_site == 'scp-ru': # SCP-RU
wiki_membership_timestamp = re.search('(?:сайта: с)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'lafundacionscp' or wikidot_site == 'scp-pt-br': # SCP-ES & -PT
wiki_membership_timestamp = re.search('(?:: desde)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'fondationscp': # SCP-FR
wiki_membership_timestamp = re.search('(?:: depuis :)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-wiki-de': # SCP-DE
wiki_membership_timestamp = re.search('(?:Site: seit)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-ukrainian': # SCP-UA
wiki_membership_timestamp = re.search('(?:сайту: з)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-cs': # SCP-CS
wiki_membership_timestamp = re.search('(?:Stránky: od)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-th': # SCP-TH
wiki_membership_timestamp = re.search('(?:เป็นสมาชิกตั้งแต่)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-pl': # SCP-PL
wiki_membership_timestamp = re.search('(?:projektu: od)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'fondazionescp': # SCP-IT
wiki_membership_timestamp = re.search('(?:sito: dal)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scp-wiki-cn': # SCP-CN
wiki_membership_timestamp = re.search('(?:本站成员:始于)(?:\D*)(\d*)', response).group(1)
elif wikidot_site == 'scpko': # SCP-KO
wiki_membership_timestamp = re.search('(?:이 사이트의 회원 시작 시간:)(?:\D*)(\d*)', response).group(1)
else: # SCP-EN and English-speaking wikis.
wiki_membership_timestamp = re.search('(?:: since)(?:\D*)(\d*)', response).group(1)
except AttributeError:
# Altogether possible this user is no longer a member. We'll send a boolean false.
wiki_membership_timestamp = False
username = re.search('(?:<h1>)(.*)(?:<\/h1>)', response).group(1)
# Download the user's avatar as a file object.
r_avatar = requests.get('http://www.wikidot.com/avatar.php?userid=' + user_id)
avatar = r_avatar.content # Bytes-like object here.
# Upload the avatar to s3
s3 = boto3.client('s3')
upload = s3.put_object(Bucket="scuttle-s3", Body=avatar, Key="avatars/wikidot/" + str(user_id))
# Give SCUTTLE back the data requested and a link to the file.
payload = {"wd_user_id": user_id, "username": username,
"wd_user_since": wd_registration_timestamp,
"avatar_path": "https://cdn.scpfoundation.wiki/avatars/wikidot/" + user_id,
"wiki_member_since": wiki_membership_timestamp}
# Send everything to SCUTTLE
headers = {"Authorization": "Bearer " + config.scuttle_token, "Content-Type": "application/json"}
j = json.dumps(payload)
r = requests.put(callback_url + '/2stacks/user/metadata', data=j, headers=headers)
return {
'job': 'complete'
}