Esempio n. 1
0
    def import_camps(self, fname, year, maxrows):
        reader = csv.reader(open(fname, 'r'))
        keys = {
            'bm_fm_id' : 0,
            'year' : 1,
            'name' : 3,
            'location' : 4,
            'url' : 5,
            'description' : 8,
            'contact_email' : 14,
            'hometown' : (11,12,13),
            }

        common_misspellings = {
            'Engadgment' : 'Engagement',
            'Engagment' : 'Engagement',
            'Coming out' : 'Coming Out',
            'Inititation' : 'Initiation'
            }

        campct = 0
        errct = 0
        timestamp = datetime.datetime.now()
        timestamp = time.mktime(timestamp.timetuple())
        seed = str(int(timestamp))
        ix = 0
        section = settings.PROXY_DOMAINS['playaevents']
        remote = 'http://%s:%s' % (section['server'], section.get('port',80))

        for row in reader:
            if row:
                data = {}

                year = row[keys['year']]

                name = smart_unicode(row[keys['name']], errors='ignore')
                name = name.replace('"', '')
                data['slug'] = slugify(name)

                for key, val in keys.items():
                    if not key == 'hometown':
                        data[key] = row[val]

                city, state, country = keys['hometown']
                city, state, country = row[city], row[state], row[country]
                home = ""
                for e in (city, state, country):
                    e = e.strip()
                    if e:
                        if home:
                            home = "%s, " % e
                        else:
                            home = e

                if home.endswith(', '):
                    home = home[:-2]
                data['hometown'] = home

                if 'description' in data:
                    data['description'] = data['description'].replace('', '\n')

                # camp locations are "Street" and "Time"
                loc = row[keys['location']]
                if loc:
                    loc = loc.replace(" o'clock", '')
                    loc = loc.replace(" o’clock", '')
                    loc = loc.replace('14','15')

                    data['location_string'] = loc
                    if '&' in loc:
                        street, timestr = loc.split('&',1)

                        street = street.strip()
                        timestr = timestr.strip()
                        if street[0] in '0123456789':
                            # probably reversed
                            t = timestr
                            timestr = street
                            street = t
                        print ('Street = %s Time = %s' % (street, timestr))
                        if str(timestr[0]) in '0123456789':
                            if not ':' in timestr:
                                timestr = '%s:00' % timestr
                            data['time_street_name'] = timestr.split(' ')[0]
                        else:
                            print 'bad time: %s [%s]' % (timestr, timestr[0])
                        for bad, good in common_misspellings.items():
                            if street == bad:
                                street = good
                                loc.replace(bad,good)
                                data['location_string'] = loc
                        data['circular_street_name'] = street

                url = '/api/0.2/%s/camp/' % year
                url = sign_url(url, section['authuser'], section['authkey'], seed = "%s%i" % (seed, ix))
                print "Updating ID#%s, Name: %s, Loc: %s" % (data.get('bm_fm_id','---'), data['name'], loc)
                resp, content = remote_json(remote, url, data=data, method='PUT')
                if resp['status'] != '200':
                    print 'error #%s\n%s\ndata: %s' % (resp['status'], content, data)
                    errct += 1
                else:
                    campct += 1
                ix += 1

            if maxrows > 0 and ix >= maxrows:
                break

        print ("done, loaded %i Camps, %i errors" % (campct, errct))
Esempio n. 2
0
    def import_art(self, fname, default_year):
        reader = csv.reader(open(fname, 'r'))
        keys = {
            'year' : 0,
            'bm_fm_id' : 2,
            'artist' : 3,
            'name' : 6,
            'location' : 7,
            'description' : 9,
            'contact_email' : 10
            }

        artct = 0
        errct = 0
        timestamp = datetime.datetime.now()
        timestamp = time.mktime(timestamp.timetuple())
        seed = str(int(timestamp))
        ix = 0
        section = settings.PROXY_DOMAINS['playaevents']
        remote = 'http://%s:%s' % (section['server'], section.get('port',80))

        for row in reader:
            if row:
                data = {}

                year = row[keys['year']]

                name = smart_unicode(row[1], errors='ignore')
                name = name.replace('"', '')
                data['slug'] = slugify(name)

                for key, val in keys.items():
                    data[key] = row[val]

                if 'description' in data:
                    data['description'] = data['description'].replace('', '\n')

                # art locations are "time" and "distance"
                loc = row[keys['location']]
                if loc:
                    loc = loc.replace(" o'clock", '')
                    loc = loc.replace(" o’clock", '')

                    data['location_string'] = loc
                    if ' ' in loc:
                        timestr, distance = row[keys['location']].split(' ',1)
                        if timestr[0] in '0123456789':
                            if not ':' in timestr:
                                timestr = '%s:00' % timestr
                            data['time_address'] = timestr
                            distance = distance.split(',')[0]
                        try:
                            data['distance'] = int(distance)
                        except ValueError:
                            pass
                artist = data.get('artist','')
                if len(artist) > 255:
                    suffix = " [and more]"
                    artist = artist[:(255-len(suffix))]
                    pos = artist.rfind(' ')
                    if pos > -1:
                        artist = artist[:pos]
                    artist = "%s%s" % (artist, suffix)
                    data['artist'] = artist




                url = '/api/0.2/%s/art/' % year
                url = sign_url(url, section['authuser'], section['authkey'], seed = "%s%i" % (seed, ix))
                print "Updating ID#%s, Name: %s, Loc: %s" % (data.get('bm_fm_id','---'), data['name'], loc)
                resp, content = remote_json(remote, url, data=data, method='PUT')
                if resp['status'] != '200':
                    print 'error #%s\n%s\ndata: %s' % (resp['status'], content, data)
                    errct += 1
                else:
                    artct += 1
                ix += 1

        print ("done, loaded %i art installations, %i errors" % (artct, errct))