aboutsummaryrefslogtreecommitdiffstats
path: root/reupload.py
blob: 5d2c0a0e30bf880d4629a307a960a6122eef95d9 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3

import os
import sys
import json
from typing import Optional
import requests
import argparse

BASE_URL = "https://wiki.mako.cc/api.php"


def login(session: requests.Session) -> (str, str):

    bot_username = os.environ['WIKI_USERNAME']
    bot_password = os.environ['WIKI_PASSWORD']

    # Step 1: Retrieve a login token
    resp = session.get(
        BASE_URL,
        params={
            "action": "query",
            "meta": "tokens",
            "type": "login",
            "format": "json",
        },
    )

    resp.raise_for_status()
    login_token = resp.json()['query']['tokens']['logintoken']
    assert login_token

    # Step 2: Send a post request to login. Use of main account for login is not
    # supported. Obtain credentials via Special:BotPasswords
    # (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
    resp = session.post(
        BASE_URL,
        data={
            "action": "login",
            "lgname": bot_username,
            "lgpassword": bot_password,
            "format": "json",
            "lgtoken": login_token, 
        },
    )

    resp.raise_for_status()

    # Step 3: While logged in, retrieve a CSRF token
    resp = session.get(
        BASE_URL,
        params={
            "action": "query",
            "meta":"tokens",
            "format":"json"
        },
    )

    resp.raise_for_status()
    csrf_token = resp.json()['query']['tokens']['csrftoken']
    assert csrf_token

    return (login_token, csrf_token)

def get_imageinfo(title: str, session: requests.Session) -> Optional[dict]:
    """Returns a dict, or None if image not found"""

    assert not title.startswith("File:")
    # Does not require authentication
    resp = session.get(
        BASE_URL,
        params={
            "action": "query",
            "format": "json",
            "prop": "imageinfo",
            "titles": f"File:{title}"
        },
    )
    resp.raise_for_status()
    pages = resp.json()['query']['pages']
    if "-1" in pages:
        return None
    else:
        return list(pages.values())[0]

def reupload_post(args):

    # parse metadata
    base_path = args.json_file.replace('.json', '')
    meta = json.loads(open(args.json_file, 'r').read())['node']
    #print(meta)
    shortcode = meta['shortcode']
    caption = meta['edge_media_to_caption']['edges'][0]['node']['text']

    if meta.get('edge_sidecar_to_children'):
        image_count = len(meta['edge_sidecar_to_children']['edges'])
    else:
        image_count = 1

    session = requests.Session()
    (login_token, csrf_token) = login(session)

    for image_num in range(1,image_count+1):

        if image_count == 1:
            jpeg_path = base_path + ".jpg"
        else:
            jpeg_path = base_path + f"_{image_num}.jpg"
        date_part = base_path.split('/')[1].split('_')[0].replace('-', '')
        time_part = base_path.split('/')[1].split('_')[1].replace('-', '')[:4]
        remote_name = f"CEQD-{date_part}-{time_part}-{image_num}.jpg"
        page_text = f"""[[Category:Center for Extraordinary Quarantine Dining]]

== Summary ==
{caption}

Imported from ELS instagram: https://www.instagram.com/p/{shortcode}/
        """

        # First, check if file already exists
        existing = get_imageinfo(remote_name, session)
        if existing:
            print(f"Already exists: {remote_name}")
            continue

        # If it doesn't, upload it!
        print(f"Uploading: {remote_name}")
        resp = session.post(
            BASE_URL,
            data={
                "action": "upload",
                "filename": remote_name,
                "text": page_text,
                "format": "json",
                "token": csrf_token,
                "ignorewarnings": 1
            },
            files={
                'file': ('filename.jpg', open(jpeg_path, 'rb'), 'multipart/form-data'),
            },
        )
        resp.raise_for_status()
        #print(json.dumps(resp.json(), sort_keys=True, indent=2)

def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        'json_file',
        help="JSON metadata file",
        type=str,
        #type=argparse.FileType('r'),
    )
    args = parser.parse_args()

    reupload_post(args)

if __name__ == "__main__":
    main()