aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/batchaddmedia.py
blob: 07c0b3fca63d81c4f48bd562abd3310bfee42ed0 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import copy, tempfile, tarfile, zipfile, subprocess, re, requests
from csv import reader as csv_reader
from urlparse import urlparse
from pyld import jsonld

from mediagoblin.gmg_commands import util as commands_util
from mediagoblin.submit.lib import (
    submit_media, get_upload_file_limits,
    FileUploadLimit, UserUploadLimit, UserPastUploadLimit)
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _

from jsonschema import validate, FormatChecker, draft4_format_checker
from jsonschema.exceptions import ValidationError
from jsonschema.compat import str_types


def parser_setup(subparser):
    subparser.description = """\
This command allows the administrator to upload many media files at once."""
    subparser.add_argument(
        'username',
        help="Name of user these media entries belong to")
    subparser.add_argument(
        'target_path',
        help=("""\
Path to a local archive or directory containing a "location.csv" and a 
"metadata.csv" file. These are csv (comma seperated value) files with the
locations and metadata of the files to be uploaded. The location must be listed
with either the URL of the remote media file or the filesystem path of a local
file. The metadata should be provided with one column for each of the 15 Dublin
Core properties (http://dublincore.org/documents/dces/). Both "location.csv" and
"metadata.csv" must begin with a row demonstrating the order of the columns. We
have provided an example of these files at <url to be added>
"""))
    subparser.add_argument(
        '--celery',
        action='store_true',
        help="Don't process eagerly, pass off to celery")


def batchaddmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    files_uploaded, files_attempted = 0, 0

    # get the user
    user = app.db.User.query.filter_by(username=args.username.lower()).first()
    if user is None:
        print "Sorry, no user by username '%s' exists" % args.username
        return

    upload_limit, max_file_size = get_upload_file_limits(user)
    temp_files = []

    if os.path.isdir(args.target_path):
        dir_path = args.target_path

    elif tarfile.is_tarfile(args.target_path):
        dir_path = tempfile.mkdtemp()
        temp_files.append(dir_path)
        tar = tarfile.open(args.target_path)
        tar.extractall(path=dir_path)

    elif zipfile.is_zipfile(args.target_path):
        dir_path = tempfile.mkdtemp()
        temp_files.append(dir_path)
        zipped_file = zipfile.ZipFile(args.target_path)
        zipped_file.extractall(path=dir_path)

    else:
        print "Couldn't recognize the file. This script only accepts tar files,\
zip files and directories"
    if dir_path.endswith('/'):
        dir_path = dir_path[:-1]

    location_file_path = os.path.join(dir_path,"location.csv")
    metadata_file_path = os.path.join(dir_path, "metadata.csv")

    # check for the location file, if it exists...
    abs_location_filename = os.path.abspath(location_file_path)
    if not os.path.exists(abs_location_filename):
        print "Can't find a file with filename '%s'" % location_file_path
        return

    # check for the metadata file, if it exists...
    abs_metadata_filename = os.path.abspath(metadata_file_path)
    if not os.path.exists(abs_metadata_filename):
        print "Can't find a file with filename '%s'" % metadata_file_path
        return

    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        else:
            return unicode(some_string)

    with file(abs_location_filename, 'r') as all_locations:
        contents = all_locations.read()
        media_locations = parse_csv_file(contents)

    with file(abs_metadata_filename, 'r') as all_metadata:
        contents = all_metadata.read()
        media_metadata = parse_csv_file(contents)

    metadata_context = { 'dcterms':'http://purl.org/dc/terms/',
                         'xsd': 'http://www.w3.org/2001/XMLSchema#'}

    for media_id in media_locations.keys():
        files_attempted += 1

        file_metadata     = media_metadata[media_id]
        sanitized_metadata = check_metadata_format(file_metadata)
        if sanitized_metadata == {}: continue

        json_ld_metadata = jsonld.compact(file_metadata, metadata_context)
        original_location = media_locations[media_id]['media:original']
        url = urlparse(original_location)

        title = sanitized_metadata.get('dcterms:title')
        description = sanitized_metadata.get('dcterms:description')

        # TODO: this isn't the same thing
        license = sanitized_metadata.get('dcterms:rights')
        filename = url.path.split()[-1]

        if url.scheme == 'http':
            res = requests.get(url.geturl(), stream=True)
            media_file = res.raw

        elif url.scheme == '':
            path = url.path
            if os.path.isabs(path):
                file_abs_path = os.path.abspath(path)
            else:
                file_path = os.path.join(dir_path, path)
                file_abs_path = os.path.abspath(file_path)
            try:
                media_file = file(file_abs_path, 'r')
            except IOError:
                print "\
FAIL: Local file {filename} could not be accessed.".format(filename=filename)
                print "Skipping it."
                continue
        try:
            submit_media(
                mg_app=app,
                user=user,
                submitted_file=media_file,
                filename=filename,
                title=maybe_unicodeify(title),
                description=maybe_unicodeify(description),
                license=maybe_unicodeify(license),
                metadata=json_ld_metadata,
                tags_string=u"",
                upload_limit=upload_limit, max_file_size=max_file_size)
            print "Successfully uploading {filename}!".format(filename=filename)
            print ""
            files_uploaded += 1
        except FileUploadLimit:
            print "FAIL: This file is larger than the upload limits for this site."
        except UserUploadLimit:
            print "FAIL: This file will put this user past their upload limits."
        except UserPastUploadLimit:
            print "FAIL: This user is already past their upload limits."
    print "\
{files_uploaded} out of {files_attempted} files successfully uploaded".format(
        files_uploaded=files_uploaded,
        files_attempted=files_attempted)
    teardown(temp_files)


def parse_csv_file(file_contents):
    list_of_contents = file_contents.split('\n')
    key, lines = (list_of_contents[0].split(','),
                  list_of_contents[1:])
    objects_dict = {}

    # Build a dictionaryfrom mediagoblin.tools.translate import lazy_pass_to_ugettext as _
    for line in lines:
        if line.isspace() or line == '': continue
        values = csv_reader([line]).next()
        line_dict = dict([(key[i], val)
            for i, val in enumerate(values)])
        media_id = line_dict['media:id']
        objects_dict[media_id] = (line_dict)

    return objects_dict


def teardown(temp_files):
    for temp_file in temp_files:
        subprocess.call(['rm','-r',temp_file])


## Set up the MediaGoblin checker
# 

URL_REGEX = re.compile(
    r'^[a-z]+://([^/:]+|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(\/.*)?$',
    re.IGNORECASE)

def is_uri(instance):
    if not isinstance(instance, str_types):
        return True

    return URL_REGEX.match(instance)


class DefaultChecker(FormatChecker):
    checkers = copy.deepcopy(draft4_format_checker.checkers)

DefaultChecker.checkers[u"uri"] = (is_uri, ())

DEFAULT_CHECKER = DefaultChecker()

def check_metadata_format(metadata_dict):
    schema = {
        "$schema": "http://json-schema.org/schema#",

        "type": "object",
        "properties": {
            "dcterms:rights": {
                "format": "uri",
                "type": "string",
            },
            "dcterms:created": {
                
            }
        },
        # "required": ["dcterms:title", "media:id"],
    }

    try:
        validate(metadata_dict, schema,
                 format_checker=DEFAULT_CHECKER)
        output_dict = metadata_dict
        # "media:id" is only for internal use, so we delete it for the output
        del output_dict['media:id']

    except ValidationError, exc:
        title = (metadata_dict.get('dcterms:title') or 
            metadata_dict.get('media:id') or _(u'UNKNOWN FILE'))

        if exc.validator == "additionalProperties":
            message = _(u'Invalid metadata provided for file "{title}". This \
script only accepts the Dublin Core metadata terms.'.format(title=title))

        elif exc.validator == "required":
            message = _(
u'All necessary metadata was not provided for file "{title}", you must include \
a "dcterms:title" column for each media file'.format(title=title))

        else:
            message = _(u'Could not find appropriate metadata for file \
"{title}".'.format(title=title))

        print _(u"""WARN: {message} \nSkipping File...\n""".format(
            message=message))

        output_dict = {}
    except:
        raise

    return output_dict