Exemple #1
0
def swapCodeForTokens(response):
    # takes dict of response parameters as input, like {'error':'blah blah'} or {'code':'blah blah','state':'blah blah'}
    db = app.connect_db()
    c = db.cursor()
    state = json.loads(response['state'])
    user_identifier = state['id']
    redir = state['redir']
    if 'error' in response:
        c.execute(
            'UPDATE users SET imgur_json=NULL WHERE imgur_id=' +
            app.app.sqlesc, (user_identifier, ))
        db.commit()
        return {'success': False}
    # called at the server redirect when imgur returns the code
    client = ImgurClient(app.app.config['IMGUR_CLIENTID'],
                         app.app.config['IMGUR_SECRET'])
    credentials = client.authorize(response['code'], 'authorization_code')
    # print credentials
    if 'access_token' in credentials.keys(
    ) and 'refresh_token' in credentials.keys():
        db = app.connect_db()
        c = db.cursor()
        c.execute(
            'UPDATE users SET imgur_json=' + app.app.sqlesc +
            ' WHERE imgur_id=' + app.app.sqlesc,
            (json.dumps(credentials), user_identifier))
        db.commit()
        db.close()
        return {'success': True, 'redir': redir}
    else:
        c.execute(
            'UPDATE users SET imgur_json=NULL WHERE imgur_id=' +
            app.app.sqlesc, (user_identifier, ))
        db.commit()
        return {'success': False}
Exemple #2
0
def swapCodeForTokens(response):
	# takes dict of response parameters as input, like {'error':'blah blah'} or {'code':'blah blah','state':'blah blah'}
	db = app.connect_db()
	c=db.cursor()
	state = json.loads(response['state'])
	user_identifier = state['id']
	redir = state['redir']
	if 'error' in response:
		c.execute('UPDATE users SET imgur_json=NULL WHERE imgur_id='+app.app.sqlesc,(user_identifier,))
		db.commit()
		return {'success':False}
	# called at the server redirect when imgur returns the code
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	credentials = client.authorize(response['code'],'authorization_code')
	# print credentials
	if 'access_token' in credentials.keys() and 'refresh_token' in credentials.keys():
		db = app.connect_db()
		c = db.cursor()
		c.execute('UPDATE users SET imgur_json='+app.app.sqlesc+' WHERE imgur_id='+app.app.sqlesc,(json.dumps(credentials),user_identifier))
		db.commit()
		db.close()
		return {'success':True,'redir':redir}
	else:
		c.execute('UPDATE users SET imgur_json=NULL WHERE imgur_id='+app.app.sqlesc,(user_identifier,))
		db.commit()
		return {'success':False}
Exemple #3
0
def uploadToImgur(userid,url):
	db = app.connect_db()
	c = db.cursor()
	c.execute('SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url='+app.app.sqlesc,(url,))
	result = c.fetchone()
	if result[4] != None:
		previous_upload_properties = json.loads(result[4])
		if time.time() < previous_upload_properties['upload_time']+(2*3600):
			return {'error':'too_soon','link':previous_upload_properties['imgur_url']}
	map_url = result[0]
	titlestring = u"{} Farm, {} by {}".format(result[2],result[3],result[1])
	descriptionstring = u"Stardew Valley game progress, full summary at http://upload.farm/{}".format(url)
	# try:
	c.execute('SELECT imgur_json FROM users WHERE id='+app.app.sqlesc,(userid,))
	r = json.loads(c.fetchone()[0])
	access_token = r['access_token']
	refresh_token = r['refresh_token']
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	client.set_user_auth(access_token,refresh_token)
	# file = url_for('home',filename=map_url,_external=True)
	# print 'uploaded to',file
	# client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
	if app.app.config['IMGUR_DIRECT_UPLOAD'] == True:
		result = client.upload_from_path(map_url,config={'title':titlestring,'description':descriptionstring},anon=False)
	else:
		map_url = u"http://upload.farm/{}".format(map_url)
		result = client.upload_from_url(map_url,config={'title':titlestring,'description':descriptionstring},anon=False)
	print(result)
	imgur_json = json.dumps({'imgur_url':'http://imgur.com/'+result['id'],'upload_time':time.time()})
	c.execute('UPDATE playerinfo SET imgur_json='+app.app.sqlesc+' WHERE url='+app.app.sqlesc,(imgur_json,url))
	db.commit()
	try:
		return {'success':None,'link':result['link']}
	except:
		return {'error':'upload_issue','link':None}
Exemple #4
0
def process_email():
	start_time = time.time()
	records_handled = 0
	db = connect_db()
	cur = db.cursor()
	while True:
		cur.execute('UPDATE todo SET currently_processing='+sqlesc+' WHERE id=(SELECT id FROM todo WHERE task=ANY('+sqlesc+') AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *',(True,['email_confirmation','old_email_confirmation','email_passwordreset'],))
		tasks = cur.fetchall()
		db.commit()
		# print tasks
		if len(tasks) != 0:
			for task in tasks:
				if task[1] in ['email_confirmation','old_email_confirmation']:
					cur.execute('UPDATE users SET email_conf_token=(CASE WHEN email_conf_token IS NULL THEN '+sqlesc+' WHEN email_conf_token IS NOT NULL THEN email_conf_token END) WHERE id='+sqlesc+' RETURNING email, id, email_conf_token',(str(uuid.uuid4()),task[2]))
					email_data = cur.fetchall()[0]
					if task[1] == 'email_confirmation':
						email_confirmation(*email_data)
					elif task[1] == 'old_email_confirmation':
						old_email_confirmation(*email_data)
				elif task[1] in ['email_passwordreset']:
					cur.execute('UPDATE users SET pw_reset_token='+sqlesc+' WHERE id='+sqlesc+' RETURNING email, id, pw_reset_token',(str(uuid.uuid4()),task[2]))
					email_data = cur.fetchall()[0]
					email_passwordreset(*email_data)
				cur.execute('DELETE FROM todo WHERE id=('+sqlesc+')',(task[0],))
				db.commit()
				records_handled += 1
		else:
			db.close()
			return time.time()-start_time, records_handled
Exemple #5
0
def checkApiAccess(userid):
	# something that checks whether we have api keys and whether they work;
	# if not, return False
	db = app.connect_db()
	c = db.cursor()
	c.execute('SELECT imgur_json FROM users WHERE id='+app.app.sqlesc,(userid,))
	r = c.fetchone()
	if len(r) > 0:
		try:
			r = json.loads(r[0])
			access_token = r['access_token']
			refresh_token = r['refresh_token']
		except TypeError:
			return False
	else:
		return False
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	client.set_user_auth(access_token,refresh_token)
	try:
		client.get_account('me').url
		credits = client.credits
		# print(credits)
		if credits['ClientRemaining'] > 10 and credits['UserRemaining'] > 10:
			return True
		else:
			return None
	except ImgurClientError:
		return False
Exemple #6
0
def checkApiAccess(userid):
    # something that checks whether we have api keys and whether they work;
    # if not, return False
    db = app.connect_db()
    c = db.cursor()
    c.execute('SELECT imgur_json FROM users WHERE id=' + app.app.sqlesc,
              (userid, ))
    r = c.fetchone()
    if len(r) > 0:
        try:
            r = json.loads(r[0])
            access_token = r['access_token']
            refresh_token = r['refresh_token']
        except TypeError:
            return False
    else:
        return False
    client = ImgurClient(app.app.config['IMGUR_CLIENTID'],
                         app.app.config['IMGUR_SECRET'])
    client.set_user_auth(access_token, refresh_token)
    try:
        client.get_account('me').url
        credits = client.credits
        # print(credits)
        if credits['ClientRemaining'] > 10 and credits['UserRemaining'] > 10:
            return True
        else:
            return None
    except ImgurClientError:
        return False
Exemple #7
0
def process_plans():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        # cur.execute('SELECT * FROM todo WHERE task='+sqlesc+' AND currently_processing NOT TRUE',('process_image',))
        cur.execute(
                'UPDATE todo SET currently_processing=' + sqlesc + ' WHERE id=(SELECT id FROM todo WHERE task=' + sqlesc + ' AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *',
                (True, 'process_plan_image',))
        tasks = cur.fetchall()
        db.commit()
        if len(tasks) != 0:
            for task in tasks:
                cur.execute('SELECT source_json, url, season FROM plans WHERE id=(' + sqlesc + ')',
                            (task[2],))
                result = cur.fetchone()
                farm_json = json.loads(result[0])
                url = result[1]
                season = 'spring' if result[2] == None else result[2]

                base_path = os.path.join(app.config.get('RENDER_FOLDER'), url)
                try:
                    os.mkdir(legacy_location(base_path))
                except OSError:
                    pass
                try:
                    farm_data = parse_json(farm_json)

                    if farm_data['type'] == 'unsupported_map':
                        continue

                    farm_path = os.path.join(base_path, url + '-plan.png')
                    # generateMinimap(farm_data).save(legacy_location(farm_path), compress_level=9)

                    # map_path = os.path.join(base_path, data['url']+'-m.png')
                    # thumb_path = os.path.join(base_path, data['url']+'-t.png')
                    farm = generateFarm(season, farm_data)
                    farm = watermark(farm, filename='stardew_info.png')
                    # th = farm.resize((int(farm.width/4), int(farm.height/4)), Image.ANTIALIAS)
                    # th.save(legacy_location(thumb_path))
                    farm.save(legacy_location(farm_path), compress_level=9)

                    cur.execute(
                            'UPDATE plans SET image_url=' + sqlesc + ', base_path=' + sqlesc + ', render_deleted=FALSE, failed_render=NULL WHERE id=' + sqlesc + '',
                            (farm_path, base_path, task[2]))
                    db.commit()
                    # # except Exception as e:
                    # #     cur.execute('UPDATE playerinfo SET failed_processing='+sqlesc+' WHERE id='+,(True,data['id']))
                    # #     db.commit()
                    cur.execute('DELETE FROM todo WHERE id=(' + sqlesc + ')', (task[0],))
                    db.commit()
                except:
                    cur.execute('UPDATE plans SET failed_render=TRUE WHERE id=' + sqlesc,
                                (task[2],))
                    cur.execute('DELETE fROM todo WHERE id=(' + sqlesc + ')', (task[0],))
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled
Exemple #8
0
def uploadToImgur(userid, url):
    db = app.connect_db()
    c = db.cursor()
    c.execute(
        'SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url='
        + app.app.sqlesc, (url, ))
    result = c.fetchone()
    if result[4] != None:
        previous_upload_properties = json.loads(result[4])
        if time.time() < previous_upload_properties['upload_time'] + (2 *
                                                                      3600):
            return {
                'error': 'too_soon',
                'link': previous_upload_properties['imgur_url']
            }
    map_url = result[0]
    titlestring = u"{} Farm, {} by {}".format(result[2], result[3], result[1])
    descriptionstring = u"Stardew Valley game progress, full summary at http://upload.farm/{}".format(
        url)
    # try:
    c.execute('SELECT imgur_json FROM users WHERE id=' + app.app.sqlesc,
              (userid, ))
    r = json.loads(c.fetchone()[0])
    access_token = r['access_token']
    refresh_token = r['refresh_token']
    client = ImgurClient(app.app.config['IMGUR_CLIENTID'],
                         app.app.config['IMGUR_SECRET'])
    client.set_user_auth(access_token, refresh_token)
    # file = url_for('home',filename=map_url,_external=True)
    # print 'uploaded to',file
    # client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
    if app.app.config['IMGUR_DIRECT_UPLOAD'] == True:
        result = client.upload_from_path(map_url,
                                         config={
                                             'title': titlestring,
                                             'description': descriptionstring
                                         },
                                         anon=False)
    else:
        map_url = u"http://upload.farm/{}".format(map_url)
        result = client.upload_from_url(map_url,
                                        config={
                                            'title': titlestring,
                                            'description': descriptionstring
                                        },
                                        anon=False)
    print(result)
    imgur_json = json.dumps({
        'imgur_url': 'http://imgur.com/' + result['id'],
        'upload_time': time.time()
    })
    c.execute(
        'UPDATE playerinfo SET imgur_json=' + app.app.sqlesc + ' WHERE url=' +
        app.app.sqlesc, (imgur_json, url))
    db.commit()
    try:
        return {'success': None, 'link': result['link']}
    except:
        return {'error': 'upload_issue', 'link': None}
def process_queue():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        # cur.execute('SELECT * FROM todo WHERE task='+sqlesc+' AND currently_processing NOT TRUE',('process_image',))
        cur.execute('UPDATE todo SET currently_processing='+sqlesc+' WHERE id=(SELECT id FROM todo WHERE task='+sqlesc+' AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *',(True,'process_image',))
        tasks = cur.fetchall()
        db.commit()
        # print tasks
        if len(tasks) != 0:
            for task in tasks:
                cur.execute('SELECT '+database_fields+' FROM playerinfo WHERE id=('+sqlesc+')',(task[2],))
                result = cur.fetchone()
                data = {}
                for i, item in enumerate(result):
                    data[sorted(database_structure_dict.keys())[i]] = item
                data['pantsColor'] = [data['pantsColor0'],data['pantsColor1'],data['pantsColor2'],data['pantsColor3']]
                data['newEyeColor'] = [data['newEyeColor0'],data['newEyeColor1'],data['newEyeColor2'],data['newEyeColor3']]
                data['hairstyleColor'] = [data['hairstyleColor0'],data['hairstyleColor1'],data['hairstyleColor2'],data['hairstyleColor3']]

                base_path = os.path.join(config.get('IMAGE_FOLDER'), data['url'])
                try:
                    os.mkdir(base_path)
                except OSError:
                    pass

                avatar_path = os.path.join(base_path, data['url']+'-a.png')
                avatar = generateAvatar(data)

                pi = json.loads(data['portrait_info'])
                portrait_path = os.path.join(base_path, data['url']+'-p.png')
                generateFamilyPortrait(avatar, pi).save(portrait_path, compress_level=9)

                avatar.resize((avatar.width*4, avatar.height*4)).save(avatar_path, compress_level=9)

                farm_data = regenerateFarmInfo(json.loads(data['farm_info']))
                farm_path = os.path.join(base_path, data['url']+'-f.png')
                generateMinimap(farm_data).save(farm_path, compress_level=9)

                map_path = os.path.join(base_path, data['url']+'-m.png')
                thumb_path = os.path.join(base_path, data['url']+'-t.png')
                farm = generateFarm(data['currentSeason'], farm_data)
                th = farm.resize((int(farm.width/4), int(farm.height/4)), Image.ANTIALIAS)
                th.save(thumb_path)
                farm.save(map_path, compress_level=9)

                cur.execute('UPDATE playerinfo SET farm_url='+sqlesc+', avatar_url='+sqlesc+', portrait_url='+sqlesc+', map_url='+sqlesc+', thumb_url='+sqlesc+', base_path='+sqlesc+' WHERE id='+sqlesc+'',(farm_path,avatar_path,portrait_path,map_path,thumb_path,base_path,data['id']))
                db.commit()
                # except:
                    # cur.execute('UPDATE playerinfo SET failed_processing='+sqlesc+' WHERE id='+,(True,data['id']))
                    # db.commit()
                cur.execute('DELETE FROM todo WHERE id=('+sqlesc+')',(task[0],))
                db.commit()
                records_handled += 1
        else:
            db.close()
            return time.time()-start_time, records_handled
def getEntries(where=None):
	connection = connect_db()
	c = connection.cursor()
	if where==None:
		where=''
	c.execute('SELECT id, base_path, avatar_url, portrait_url, farm_url, map_url, thumb_url, url FROM playerinfo '+where)
	entries = c.fetchall()
	connection.close()
	return entries
Exemple #11
0
def getAuthUrl(userid,target=None):
	db = app.connect_db()
	c = db.cursor()
	iuid = unicode(uuid.uuid4())
	imgur_id = json.dumps({'id':iuid,'redir':target})
	c.execute('UPDATE users SET imgur_id='+app.app.sqlesc+' WHERE id='+app.app.sqlesc,(iuid,userid))
	db.commit()
	db.close()
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	authorization_url = client.get_auth_url('code')+'&state='+unicode(imgur_id)
	return authorization_url
Exemple #12
0
def getEntries(where=None):
    connection = connect_db()
    c = connection.cursor()
    if where == None:
        where = ""
    c.execute(
        "SELECT id, base_path, avatar_url, portrait_url, farm_url, map_url, thumb_url, url FROM playerinfo "
        + where)
    entries = c.fetchall()
    connection.close()
    return entries
Exemple #13
0
def process_email():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        cur.execute(
            "UPDATE todo SET currently_processing="
            + sqlesc
            + " WHERE id=(SELECT id FROM todo WHERE task=ANY("
            + sqlesc
            + ") AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *",
            (
                True,
                ["email_confirmation", "old_email_confirmation", "email_passwordreset"],
            ),
        )
        tasks = cur.fetchall()
        db.commit()
        # print tasks
        if len(tasks) != 0:
            for task in tasks:
                if task[1] in ["email_confirmation", "old_email_confirmation"]:
                    cur.execute(
                        "UPDATE users SET email_conf_token=(CASE WHEN email_conf_token IS NULL THEN "
                        + sqlesc
                        + " WHEN email_conf_token IS NOT NULL THEN email_conf_token END) WHERE id="
                        + sqlesc
                        + " RETURNING email, id, email_conf_token",
                        (str(uuid.uuid4()), task[2]),
                    )
                    email_data = cur.fetchall()[0]
                    if task[1] == "email_confirmation":
                        email_confirmation(*email_data)
                    elif task[1] == "old_email_confirmation":
                        old_email_confirmation(*email_data)
                elif task[1] in ["email_passwordreset"]:
                    cur.execute(
                        "UPDATE users SET pw_reset_token="
                        + sqlesc
                        + " WHERE id="
                        + sqlesc
                        + " RETURNING email, id, pw_reset_token",
                        (str(uuid.uuid4()), task[2]),
                    )
                    email_data = cur.fetchall()[0]
                    email_passwordreset(*email_data)
                cur.execute("DELETE FROM todo WHERE id=(" + sqlesc + ")", (task[0],))
                db.commit()
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled
Exemple #14
0
def swapCodeForTokens(response):
    # takes dict of response parameters as input, like {'error':'blah blah'} or {'code':'blah blah','state':'blah blah'}
    db = app.connect_db()
    c = db.cursor()
    state = json.loads(response["state"])
    user_identifier = state["id"]
    redir = state["redir"]
    if "error" in response:
        c.execute(
            "UPDATE users SET imgur_json=NULL WHERE imgur_id=" +
            app.app.sqlesc,
            (user_identifier, ),
        )
        db.commit()
        return {"success": False}
    # called at the server redirect when imgur returns the code
    client = ImgurClient(app.app.config["IMGUR_CLIENTID"],
                         app.app.config["IMGUR_SECRET"])
    credentials = client.authorize(response["code"], "authorization_code")
    # print credentials
    if "access_token" in credentials.keys(
    ) and "refresh_token" in credentials.keys():
        db = app.connect_db()
        c = db.cursor()
        c.execute(
            "UPDATE users SET imgur_json=" + app.app.sqlesc +
            " WHERE imgur_id=" + app.app.sqlesc,
            (json.dumps(credentials), user_identifier),
        )
        db.commit()
        db.close()
        return {"success": True, "redir": redir}
    else:
        c.execute(
            "UPDATE users SET imgur_json=NULL WHERE imgur_id=" +
            app.app.sqlesc,
            (user_identifier, ),
        )
        db.commit()
        return {"success": False}
Exemple #15
0
def getAuthUrl(userid, target=None):
    db = app.connect_db()
    c = db.cursor()
    iuid = unicode(uuid.uuid4())
    imgur_id = json.dumps({'id': iuid, 'redir': target})
    c.execute(
        'UPDATE users SET imgur_id=' + app.app.sqlesc + ' WHERE id=' +
        app.app.sqlesc, (iuid, userid))
    db.commit()
    db.close()
    client = ImgurClient(app.app.config['IMGUR_CLIENTID'],
                         app.app.config['IMGUR_SECRET'])
    authorization_url = client.get_auth_url('code') + '&state=' + unicode(
        imgur_id)
    return authorization_url
Exemple #16
0
def getAuthUrl(userid, target=None):
    db = app.connect_db()
    c = db.cursor()
    iuid = unicode(uuid.uuid4())
    imgur_id = json.dumps({"id": iuid, "redir": target})
    c.execute(
        "UPDATE users SET imgur_id=" + app.app.sqlesc + " WHERE id=" +
        app.app.sqlesc,
        (iuid, userid),
    )
    db.commit()
    db.close()
    client = ImgurClient(app.app.config["IMGUR_CLIENTID"],
                         app.app.config["IMGUR_SECRET"])
    authorization_url = client.get_auth_url("code") + "&state=" + unicode(
        imgur_id)
    return authorization_url
def processFile(rowid, original_base_path, original_avatar_path, original_portrait_path, original_farm_path, original_map_path, original_thumb_path, url):
	if url == None:
		return None

	base_subfolder = str(int(math.floor(int(rowid)/app.config.get('IMAGE_MAX_PER_FOLDER'))))
	base_path = os.path.join(app.config.get('IMAGE_FOLDER'), base_subfolder, url)

	if base_path == original_base_path:
		return None

	try:
		os.makedirs(legacy_location(base_path))
	except OSError:
		pass

	try:
		connection = connect_db()
		cur = connection.cursor()

		avatar_paths = [original_avatar_path, os.path.join(base_path, url+'-a.png')]
		portrait_paths = [original_portrait_path, os.path.join(base_path, url+'-p.png')]
		farm_paths = [original_farm_path, os.path.join(base_path, url+'-f.png')]
		map_paths = [original_map_path, os.path.join(base_path, url+'-m.png')]
		thumb_paths = [original_thumb_path, os.path.join(base_path, url+'-t.png')]

		for original, new in [avatar_paths,portrait_paths,farm_paths,map_paths,thumb_paths]:
			try:
				shutil.move(legacy_location(original),legacy_location(new))
			except IOError:
				pass
		try:
			os.rmdir(legacy_location(original_base_path))
		except WindowsError:
			pass

		cur.execute('UPDATE playerinfo SET base_path = '+sqlesc+', avatar_url = '+sqlesc+', portrait_url = '+sqlesc+', farm_url = '+sqlesc+', map_url = '+sqlesc+', thumb_url = '+sqlesc+' WHERE id='+sqlesc,(base_path,avatar_paths[1],portrait_paths[1],farm_paths[1],map_paths[1],thumb_paths[1],rowid))
		connection.commit()
		return True
	except:
		return False
Exemple #18
0
def processFile(
    rowid,
    original_base_path,
    original_avatar_path,
    original_portrait_path,
    original_farm_path,
    original_map_path,
    original_thumb_path,
    url,
):
    if url == None:
        return None

    base_subfolder = str(
        int(math.floor(int(rowid) / app.config.get("IMAGE_MAX_PER_FOLDER"))))
    base_path = os.path.join(app.config.get("IMAGE_FOLDER"), base_subfolder,
                             url)

    if base_path == original_base_path:
        return None

    try:
        os.makedirs(legacy_location(base_path))
    except OSError:
        pass

    try:
        connection = connect_db()
        cur = connection.cursor()

        avatar_paths = [
            original_avatar_path,
            os.path.join(base_path, url + "-a.png")
        ]
        portrait_paths = [
            original_portrait_path,
            os.path.join(base_path, url + "-p.png"),
        ]
        farm_paths = [
            original_farm_path,
            os.path.join(base_path, url + "-f.png")
        ]
        map_paths = [
            original_map_path,
            os.path.join(base_path, url + "-m.png")
        ]
        thumb_paths = [
            original_thumb_path,
            os.path.join(base_path, url + "-t.png")
        ]

        for original, new in [
                avatar_paths,
                portrait_paths,
                farm_paths,
                map_paths,
                thumb_paths,
        ]:
            try:
                shutil.move(legacy_location(original), legacy_location(new))
            except IOError:
                pass
        try:
            os.rmdir(legacy_location(original_base_path))
        except WindowsError:
            pass

        cur.execute(
            "UPDATE playerinfo SET base_path = " + sqlesc + ", avatar_url = " +
            sqlesc + ", portrait_url = " + sqlesc + ", farm_url = " + sqlesc +
            ", map_url = " + sqlesc + ", thumb_url = " + sqlesc +
            " WHERE id=" + sqlesc,
            (
                base_path,
                avatar_paths[1],
                portrait_paths[1],
                farm_paths[1],
                map_paths[1],
                thumb_paths[1],
                rowid,
            ),
        )
        connection.commit()
        return True
    except:
        return False
Exemple #19
0
def uploadToImgur(userid, url):
    db = app.connect_db()
    c = db.cursor()
    c.execute(
        "SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url="
        + app.app.sqlesc,
        (url, ),
    )
    result = c.fetchone()
    if result[4] != None:
        previous_upload_properties = json.loads(result[4])
        if time.time() < previous_upload_properties["upload_time"] + (2 *
                                                                      3600):
            return {
                "error": "too_soon",
                "link": previous_upload_properties["imgur_url"],
            }
    map_url = result[0]
    titlestring = u"{} Farm, {} by {}".format(result[2], result[3], result[1])
    descriptionstring = (
        u"Stardew Valley game progress, full summary at http://upload.farm/{}".
        format(url))
    # try:
    c.execute("SELECT imgur_json FROM users WHERE id=" + app.app.sqlesc,
              (userid, ))
    r = json.loads(c.fetchone()[0])
    access_token = r["access_token"]
    refresh_token = r["refresh_token"]
    client = ImgurClient(app.app.config["IMGUR_CLIENTID"],
                         app.app.config["IMGUR_SECRET"])
    client.set_user_auth(access_token, refresh_token)
    # file = url_for('home',filename=map_url,_external=True)
    # print 'uploaded to',file
    # client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
    if app.app.config["IMGUR_DIRECT_UPLOAD"] == True:
        result = client.upload_from_path(
            map_url,
            config={
                "title": titlestring,
                "description": descriptionstring
            },
            anon=False,
        )
    else:
        map_url = u"http://upload.farm/{}".format(map_url)
        result = client.upload_from_url(
            map_url,
            config={
                "title": titlestring,
                "description": descriptionstring
            },
            anon=False,
        )
    print(result)
    imgur_json = json.dumps({
        "imgur_url": "http://imgur.com/" + result["id"],
        "upload_time": time.time()
    })
    c.execute(
        "UPDATE playerinfo SET imgur_json=" + app.app.sqlesc + " WHERE url=" +
        app.app.sqlesc,
        (imgur_json, url),
    )
    db.commit()
    try:
        return {"success": None, "link": result["link"]}
    except:
        return {"error": "upload_issue", "link": None}
Exemple #20
0
def process_queue():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        # cur.execute('SELECT * FROM todo WHERE task='+sqlesc+' AND currently_processing NOT TRUE',('process_image',))
        cur.execute(
            'UPDATE todo SET currently_processing=' + sqlesc +
            ' WHERE id=(SELECT id FROM todo WHERE task=' + sqlesc +
            ' AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *', (
                True,
                'process_image',
            ))
        tasks = cur.fetchall()
        db.commit()
        if len(tasks) != 0:
            for task in tasks:
                cur.execute(
                    'SELECT ' + database_fields +
                    ' FROM playerinfo WHERE id=(' + sqlesc + ')', (task[2], ))
                result = cur.fetchone()
                data = {}
                for i, item in enumerate(result):
                    data[sorted(database_structure_dict.keys())[i]] = item
                data['pantsColor'] = [
                    data['pantsColor0'], data['pantsColor1'],
                    data['pantsColor2'], data['pantsColor3']
                ]
                data['newEyeColor'] = [
                    data['newEyeColor0'], data['newEyeColor1'],
                    data['newEyeColor2'], data['newEyeColor3']
                ]
                data['hairstyleColor'] = [
                    data['hairstyleColor0'], data['hairstyleColor1'],
                    data['hairstyleColor2'], data['hairstyleColor3']
                ]

                base_subfolder = str(
                    int(
                        math.floor(
                            int(task[2]) /
                            app.config.get('IMAGE_MAX_PER_FOLDER'))))
                base_path = os.path.join(app.config.get('IMAGE_FOLDER'),
                                         base_subfolder, data['url'])
                try:
                    os.makedirs(legacy_location(base_path))
                except OSError:
                    pass

                avatar_path = os.path.join(base_path, data['url'] + '-a.png')
                avatar = generateAvatar(data)

                pi = json.loads(data['portrait_info'])
                portrait_path = os.path.join(base_path, data['url'] + '-p.png')
                generateFamilyPortrait(avatar,
                                       pi).save(legacy_location(portrait_path),
                                                compress_level=9)

                avatar.resize(
                    (avatar.width * 4,
                     avatar.height * 4)).save(legacy_location(avatar_path),
                                              compress_level=9)

                farm_data = regenerateFarmInfo(json.loads(data['farm_info']))
                farm_path = os.path.join(base_path, data['url'] + '-f.png')
                generateMinimap(farm_data).save(legacy_location(farm_path),
                                                compress_level=9)

                map_path = os.path.join(base_path, data['url'] + '-m.png')
                thumb_path = os.path.join(base_path, data['url'] + '-t.png')
                farm = generateFarm(data['currentSeason'], farm_data)
                th = farm.resize((int(farm.width / 4), int(farm.height / 4)),
                                 Image.ANTIALIAS)
                th.save(legacy_location(thumb_path))
                farm = watermark(farm, filename='u.f.png')
                farm.save(legacy_location(map_path), compress_level=9)

                cur.execute(
                    'UPDATE playerinfo SET farm_url=' + sqlesc +
                    ', avatar_url=' + sqlesc + ', portrait_url=' + sqlesc +
                    ', map_url=' + sqlesc + ', thumb_url=' + sqlesc +
                    ', base_path=' + sqlesc + ' WHERE id=' + sqlesc + '',
                    (farm_path, avatar_path, portrait_path, map_path,
                     thumb_path, base_path, data['id']))
                db.commit()
                # except Exception as e:
                #     cur.execute('UPDATE playerinfo SET failed_processing='+sqlesc+' WHERE id='+,(True,data['id']))
                #     db.commit()
                cur.execute('DELETE FROM todo WHERE id=(' + sqlesc + ')',
                            (task[0], ))
                db.commit()
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled
Exemple #21
0
def process_plans():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()
    while True:
        # cur.execute('SELECT * FROM todo WHERE task='+sqlesc+' AND currently_processing NOT TRUE',('process_image',))
        cur.execute(
            "UPDATE todo SET currently_processing=" + sqlesc +
            " WHERE id=(SELECT id FROM todo WHERE task=" + sqlesc +
            " AND currently_processing IS NOT TRUE LIMIT 1) RETURNING *",
            (
                True,
                "process_plan_image",
            ),
        )
        tasks = cur.fetchall()
        db.commit()
        if len(tasks) != 0:
            for task in tasks:
                cur.execute(
                    "SELECT source_json, url, season FROM plans WHERE id=(" +
                    sqlesc + ")",
                    (task[2], ),
                )
                result = cur.fetchone()
                farm_json = json.loads(result[0])
                url = result[1]
                season = "spring" if result[2] == None else result[2]

                base_path = os.path.join(app.config.get("RENDER_FOLDER"), url)
                try:
                    os.mkdir(legacy_location(base_path))
                except OSError:
                    pass
                try:
                    farm_data = parse_json(farm_json)

                    if farm_data["type"] == "unsupported_map":
                        continue

                    farm_path = os.path.join(base_path, url + "-plan.png")
                    # generateMinimap(farm_data).save(legacy_location(farm_path), compress_level=9)

                    # map_path = os.path.join(base_path, data['url']+'-m.png')
                    # thumb_path = os.path.join(base_path, data['url']+'-t.png')
                    farm = generateFarm(season, farm_data)
                    farm = watermark(farm, filename="stardew_info.png")
                    # th = farm.resize((int(farm.width/4), int(farm.height/4)), Image.ANTIALIAS)
                    # th.save(legacy_location(thumb_path))
                    farm.save(legacy_location(farm_path), compress_level=9)

                    cur.execute(
                        "UPDATE plans SET image_url=" + sqlesc +
                        ", base_path=" + sqlesc +
                        ", render_deleted=FALSE, failed_render=NULL WHERE id="
                        + sqlesc + "",
                        (farm_path, base_path, task[2]),
                    )
                    db.commit()
                    # # except Exception as e:
                    # #     cur.execute('UPDATE playerinfo SET failed_processing='+sqlesc+' WHERE id='+,(True,data['id']))
                    # #     db.commit()
                    cur.execute("DELETE FROM todo WHERE id=(" + sqlesc + ")",
                                (task[0], ))
                    db.commit()
                except:
                    cur.execute(
                        "UPDATE plans SET failed_render=TRUE WHERE id=" +
                        sqlesc,
                        (task[2], ),
                    )
                    cur.execute("DELETE fROM todo WHERE id=(" + sqlesc + ")",
                                (task[0], ))
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled
Exemple #22
0
def process_queue():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()

    while True:
        cur.execute(
            sql.GET_TODO_TASKS,
            (
                True,
                "process_image",
            ),
        )
        tasks = cur.fetchall()
        db.commit()

        if tasks:
            for task in tasks:
                task_id = task[0]
                farm_id = task[2]

                data = save_from_id(farm_id, cur)
                base_subfolder = str(
                    int(
                        math.floor(
                            int(farm_id) /
                            app.config.get("IMAGE_MAX_PER_FOLDER"))))
                base_path = os.path.join(app.config.get("IMAGE_FOLDER"),
                                         base_subfolder, data["url"])
                try:
                    os.makedirs(legacy_location(base_path))
                except OSError:
                    pass

                base_path_fmt = os.path.join(base_path,
                                             data["url"] + "-{image_type}.png")

                avatar_path = base_path_fmt.format(image_type="a")
                portrait_path = base_path_fmt.format(image_type="p")
                farm_path = base_path_fmt.format(image_type="f")
                map_path = base_path_fmt.format(image_type="m")
                thumb_path = base_path_fmt.format(image_type="t")

                # Main Player Avatar and Portrait
                avatar = generateAvatar(data)
                avatar.resize((avatar.width * 4, avatar.height * 4))
                avatar.save(legacy_location(avatar_path), compress_level=9)

                # Farmhands
                farmhands = data.get("farmhands", [])
                if farmhands:
                    for i, farmhand in enumerate(farmhands):
                        farmhand_path = base_path_fmt.format(
                            image_type=f'fh-{farmhand["UniqueMultiplayerID"]}')
                        farmhand_avatar = generateAvatar(farmhand)

                        farmhand_avatar.resize(
                            (avatar.width * 4, avatar.height * 4))
                        farmhand_avatar.save(legacy_location(farmhand_path),
                                             compress_level=9)
                        farmhand["avatar_url"] = farmhand_path

                cur.execute(sql.UPDATE_FARMHANDS,
                            (json.dumps(farmhands), farm_id))

                portrait_info = json.loads(data["portrait_info"])

                partner_image = None
                partner_id = portrait_info.get("partner_id")
                if partner_id:
                    partner = next(
                        filter(
                            lambda f: f["UniqueMultiplayerID"] == partner_id,
                            farmhands))
                    partner_image = Image.open(
                        legacy_location(partner["avatar_url"]))

                generateFamilyPortrait(avatar,
                                       portrait_info,
                                       partner_image=partner_image).save(
                                           legacy_location(portrait_path),
                                           compress_level=9)

                # Minimap, Thumbnail and Main Map
                farm_data = regenerateFarmInfo(json.loads(data["farm_info"]))
                generateMinimap(farm_data).save(legacy_location(farm_path),
                                                compress_level=9)

                farm = generateFarm(data["currentSeason"], farm_data)

                th = farm.resize((int(farm.width / 4), int(farm.height / 4)),
                                 Image.ANTIALIAS)
                th.save(legacy_location(thumb_path))
                farm = watermark(farm, filename="u.f.png")
                farm.save(legacy_location(map_path), compress_level=9)

                cur.execute(
                    sql.UPDATE_PLAYER_IMAGE_URLS,
                    (
                        farm_path,
                        avatar_path,
                        portrait_path,
                        map_path,
                        thumb_path,
                        base_path,
                        data["id"],
                    ),
                )
                db.commit()

                cur.execute(sql.DELETE_TASK, (task_id, ))
                db.commit()
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled
Exemple #23
0
def process_queue():
    start_time = time.time()
    records_handled = 0
    db = connect_db()
    cur = db.cursor()

    while True:
        cur.execute(
                sql.GET_TODO_TASKS,
                (True, 'process_image',)
        )
        tasks = cur.fetchall()
        db.commit()

        if tasks:
            for task in tasks:
                task_id = task[0]
                farm_id = task[2]

                data = save_from_id(farm_id, cur)
                base_subfolder = str(
                        int(math.floor(int(farm_id) / app.config.get('IMAGE_MAX_PER_FOLDER')))
                )
                base_path = os.path.join(app.config.get('IMAGE_FOLDER'), base_subfolder,
                                         data['url'])
                try:
                    os.makedirs(legacy_location(base_path))
                except OSError:
                    pass

                base_path_fmt = os.path.join(base_path, data['url'] + '-{image_type}.png')

                avatar_path = base_path_fmt.format(image_type='a')
                portrait_path = base_path_fmt.format(image_type='p')
                farm_path = base_path_fmt.format(image_type='f')
                map_path = base_path_fmt.format(image_type='m')
                thumb_path = base_path_fmt.format(image_type='t')

                # Main Player Avatar and Portrait
                avatar = generateAvatar(data)
                avatar.resize((avatar.width * 4, avatar.height * 4))
                avatar.save(legacy_location(avatar_path), compress_level=9)

                # Farmhands
                farmhands = data.get('farmhands', [])
                if farmhands:
                    for i, farmhand in enumerate(farmhands):
                        farmhand_path = base_path_fmt.format(
                                image_type=f'fh-{farmhand["UniqueMultiplayerID"]}'
                        )
                        farmhand_avatar = generateAvatar(farmhand)

                        farmhand_avatar.resize((avatar.width * 4, avatar.height * 4))
                        farmhand_avatar.save(legacy_location(farmhand_path), compress_level=9)
                        farmhand['avatar_url'] = farmhand_path

                cur.execute(
                        sql.UPDATE_FARMHANDS,
                        (json.dumps(farmhands), farm_id)
                )

                portrait_info = json.loads(data['portrait_info'])

                partner_image = None
                partner_id = portrait_info.get('partner_id')
                if partner_id:
                    partner = next(filter(lambda f: f['UniqueMultiplayerID'] == partner_id, farmhands))
                    partner_image = Image.open(legacy_location(partner['avatar_url']))

                generateFamilyPortrait(avatar, portrait_info, partner_image=partner_image) \
                    .save(legacy_location(portrait_path), compress_level=9)

                # Minimap, Thumbnail and Main Map
                farm_data = regenerateFarmInfo(json.loads(data['farm_info']))
                generateMinimap(farm_data).save(legacy_location(farm_path), compress_level=9)

                farm = generateFarm(data['currentSeason'], farm_data)

                th = farm.resize((int(farm.width / 4), int(farm.height / 4)), Image.ANTIALIAS)
                th.save(legacy_location(thumb_path))
                farm = watermark(farm, filename='u.f.png')
                farm.save(legacy_location(map_path), compress_level=9)

                cur.execute(
                        sql.UPDATE_PLAYER_IMAGE_URLS,
                        (farm_path, avatar_path, portrait_path, map_path, thumb_path, base_path,
                         data['id'])
                )
                db.commit()

                cur.execute(sql.DELETE_TASK, (task_id,))
                db.commit()
                records_handled += 1
        else:
            db.close()
            return time.time() - start_time, records_handled