def main(): """Does the job""" keep = True photos = request_photos() page = 1 t = False while keep is True: p = next(photos, False) if p is False: photos = request_photos(page + 1) continue trial = Photo.objects(flickr=p["id"]).first() print("Trying to find %s in collection" % (p["id"])) if trial is not None: print("Photo already published, looping again") print("Found ID: %s\n" % (p["id"])) continue print("Found a valid photo, posting") keep = False url = make_photo_url(p["farm"], p["server"], p["id"], p["secret"]) t = tweet(url) Photo(flickr=p["id"], tweet=t["id_str"]).save() return t
def mongo_get_photos(name, offset, limit): list_ph = Photo.objects( author=name).order_by('id').skip(offset).limit(limit) if list_ph.count(with_limit_and_skip=False) > (offset + limit): has_more = True else: has_more = False return (has_more, list_ph)
def mongo_set_attributes_by_id(name, photo_id, body): attributes = body ph = Photo.objects(id=ObjectId(photo_id), author=name).update(title=attributes['title'], location=attributes['location'], comment=attributes['comment'], tags=attributes['tags']) return ph
def mongo_set_attributes_by_id(name, photo_id, body): attributes = body ph = Photo.objects(id=ObjectId(photo_id), author=name).update(title=attributes.title, location=attributes.location, comment=attributes.comment, tags=attributes.tags) return ph
def mongo_get_photo_by_name_and_id(display_name, photo_id): try: ph = Photo.objects(photo_id=photo_id, display_name=display_name).get() return ph except (pymongo.errors.AutoReconnect, pymongo.errors.ServerSelectionTimeoutError, pymongo.errors.NetworkTimeout) as e: raise
def mongo_get_photos_by_name(display_name, offset, limit): try: photos_by_display_name = Photo.objects( display_name=display_name).order_by('photo_id').skip(offset).limit( limit) return photos_by_display_name except (pymongo.errors.AutoReconnect, pymongo.errors.ServerSelectionTimeoutError, pymongo.errors.NetworkTimeout) as e: raise
def mongo_get_attributes_by_id(name, photo_id): ph = Photo.objects(id=ObjectId(photo_id), author=name).get() attributes = json.loads( json.dumps({ 'title': ph.title, 'location': ph.location, 'author': ph.author, 'comment': ph.comment, 'tags': ph.tags })) return attributes
def mongo_delete_photo_by_id(name, photo_id): try: ph = Photo.objects(id=ObjectId(photo_id), author=name).get() except (Photo.DoesNotExist, Photo.MultipleObjectsReturned): return False except (pymongo.errors.AutoReconnect, pymongo.errors.ServerSelectionTimeoutError, pymongo.errors.NetworkTimeout) as e: raise ph.delete() return True
def mongo_delete_photo_by_name_and_id(display_name, photo_id): try: ph = Photo.objects(photo_id=photo_id, display_name=display_name).get() except (pymongo.errors.AutoReconnect, pymongo.errors.ServerSelectionTimeoutError, pymongo.errors.NetworkTimeout) as e: raise except (Photo.DoesNotExist) as e: return False ph.delete() return True
def mongo_set_photo_attributes(display_name, photo_id, attributes, photo_all_attributes): try: qs = Photo.objects(photo_id=photo_id, display_name=display_name) for key, value in attributes.items(): set_attr = "set__" + key qs.update(**{set_attr: value}) for element in photo_all_attributes: if not element in attributes: unset_attr = "unset__" + element qs.update(**{unset_attr: True}) except (pymongo.errors.AutoReconnect, pymongo.errors.ServerSelectionTimeoutError, pymongo.errors.NetworkTimeout) as e: raise
def mongo_get_photo_by_id(name, photo_id): ph = Photo.objects(id=ObjectId(photo_id), author=name).get() return ph
def mongo_get_photo(photo_id): ph = Photo.objects(id=ObjectId(photo_id)).get() return ph
def mongo_check(author): count = Photo.objects(filename=filename).count() return count
def delete_photo(photo_id): ph = Photo.objects(id=ObjectId(photo_id)).get().delete() return 'NoContent', 204