Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from quintindunn/development
Browse files Browse the repository at this point in the history
Added Searching functions
  • Loading branch information
quintindunn authored Nov 11, 2023
2 parents 976c180 + b45dfa0 commit d712564
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 5 deletions.
15 changes: 15 additions & 0 deletions examples/get_profile_by_username/get_profile_by_username_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

from lapsepy.lapse import Lapse

if __name__ == '__main__':
lapse = Lapse(os.getenv("REFRESH_TOKEN"))

username = input("Username: ")

profile = lapse.get_profile_by_username(username, friends_limit=1)

print(profile.username, profile.user_display_name)

im = profile.load_profile_picture()
im.show()
10 changes: 10 additions & 0 deletions examples/search_for_user/search_for_user_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os

from lapsepy import Lapse

if __name__ == '__main__':
lapse = Lapse(os.getenv("REFRESH_TOKEN"))

user = input("User: ")
result = lapse.search_for_user(user, first=1)
print(result)
2 changes: 1 addition & 1 deletion lapsepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Date: 10/22/23
"""

__version__ = '1.0.2'
__version__ = '1.0.3'

from .journal import Journal
from .auth.refresher import refresh
Expand Down
10 changes: 10 additions & 0 deletions lapsepy/journal/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def __str__(self):
return self.message


class UserNotFoundException(SyncJournalException):
def __init__(self, message: str = ""):
self.message = message

def __str__(self):
return self.message


class AuthTokenError(SyncJournalException):
def __init__(self, message: str = ""):
self.message = message
Expand All @@ -28,6 +36,8 @@ def __str__(self):
return self.message




def sync_journal_exception_router(error: dict) -> SyncJournalException:
"""
Takes in errors from https://sync-service.production.journal-api.lapse.app/graphql API and parses them, raises
Expand Down
23 changes: 23 additions & 0 deletions lapsepy/journal/factory/friends_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,26 @@ def _render_variables(self):
self.variables['input'] = {
"id": self.user_id
}


class SearchUsersGQL(BaseGQL):
def __init__(self, term: str, first: int = 10):
super().__init__("SearchUsersGraphQLQuery",
"query SearchUsersGraphQLQuery($searchTerm: String!, $first: Int, $after: String, "
"$last: Int, $before: String) { searchUsers( searchTerm: $searchTerm first: $first after: "
"$after last: $last before: $before ) { __typename edges { __typename cursor node { "
"__typename id displayName profilePhotoName username friendStatus blockedMe isBlocked } } "
"pageInfo { __typename startCursor endCursor hasNextPage hasPreviousPage } } }")

self.first = first
self.term = term

self.variables = {}

self._render_variables()

def _render_variables(self):
self.variables = {
"first": self.first,
"searchTerm": self.term
}
31 changes: 29 additions & 2 deletions lapsepy/journal/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests

from .common.utils import format_iso_time
from .factory.friends_factory import FriendsFeedItemsGQL, ProfileDetailsGQL, SendKudosGQL
from .factory.friends_factory import FriendsFeedItemsGQL, ProfileDetailsGQL, SendKudosGQL, SearchUsersGQL

from .factory.media_factory import ImageUploadURLGQL, CreateMediaGQL, SendInstantsGQL, StatusUpdateGQL, \
RemoveFriendsFeedItemGQL, AddReactionGQL, RemoveReactionGQL, SendCommentGQL, DeleteCommentGQL, ReviewMediaGQL, \
Expand All @@ -24,7 +24,8 @@
from lapsepy.journal.factory.profile_factory import SaveBioGQL, SaveDisplayNameGQL, SaveUsernameGQL, SaveEmojisGQL, \
SaveDOBGQL, CurrentUserGQL

from .structures import Snap, Profile, ProfileMusic, FriendsFeed, FriendNode, DarkRoomMedia, ReviewMediaPartition
from .structures import Snap, Profile, ProfileMusic, FriendsFeed, FriendNode, DarkRoomMedia, ReviewMediaPartition, \
SearchUser

import logging

Expand Down Expand Up @@ -511,3 +512,29 @@ def delete_comment(self, msg_id: str, comment_id: str):

if not response.get('data', {}).get("deleteMediaComment", {}).get("success"):
raise SyncJournalException("Error deleting comment.")

def search_for_user(self, term: str, first: int = 10) -> list[SearchUser]:
"""
Searches for a User using Lapse API
:param term: Term to search for
:param first: How many results to get at maximum (Not used)
:return:
"""
query = SearchUsersGQL(term=term, first=first).to_dict()
response = self._sync_journal_call(query)

users = []

for edge in response.get("data", {}).get("searchUsers", {}).get("edges"):
node = edge['node']
search_user = SearchUser(user_id=node.get("id"),
display_name=node.get("displayName"),
profile_photo_name=node.get("profilePhotoName"),
username=node.get("username"),
friend_status=node.get("friendStatus"),
blocked_me=node.get("blockedMe"),
is_blocked=node.get("isBlocked")
)
users.append(search_user)

return users
1 change: 1 addition & 0 deletions lapsepy/journal/structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .profile import Profile, ProfileMusic
from .snap import Snap, DarkRoomMedia, ReviewMediaPartition
from .friendsfeed import FriendsFeed, FriendNode
from .searches import SearchUser
25 changes: 25 additions & 0 deletions lapsepy/journal/structures/searches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from lapsepy.lapse import Lapse


class SearchUser:
def __init__(self, user_id, display_name: str, profile_photo_name: str, username: str, friend_status: str,
blocked_me: bool, is_blocked: bool):

self.user_id = user_id
self.display_name = display_name,
self.profile_photo_name = profile_photo_name
self.username = username
self.friend_status = friend_status
self.blocked_me = blocked_me
self.is_blocked = is_blocked

def to_profile(self, ctx: "Lapse", album_limit: int = 6, friends_limit: int = 10):
return ctx.get_profile_by_id(user_id=self.user_id, album_limit=album_limit, friends_limit=friends_limit)

def __str__(self):
return f"<SearchUser username=\"{self.username}\" user_id=\"{self.user_id}\">"

__repr__ = __str__
26 changes: 25 additions & 1 deletion lapsepy/lapse/lapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from lapsepy.auth.refresher import refresh
from lapsepy.journal.journal import Journal
from lapsepy.journal.common.exceptions import AuthTokenExpired
from lapsepy.journal.common.exceptions import AuthTokenExpired, UserNotFoundException
from lapsepy.journal.structures import Profile, DarkRoomMedia, ReviewMediaPartition

import logging
Expand Down Expand Up @@ -239,3 +239,27 @@ def delete_comment(self, msg_id: str, comment_id: str):
:return:
"""
return self.journal.delete_comment(msg_id=msg_id, comment_id=comment_id)

def search_for_user(self, term: str, first: int = 10):
"""
Searches for a User using Lapse API
:param term: Term to search for
:param first: How many results to get at maximum (Not used)
:return:
"""
return self.journal.search_for_user(term=term, first=first)

def get_profile_by_username(self, username: str, album_limit: int = 6, friends_limit: int = 10) -> Profile:
"""
Wrapper for Lapse.search_for_user, returns a Profile object from a username.
:param username: Username to search for.
:param album_limit: Max amount of albums to get.
:param friends_limit: Max amount of friends to get.
:return:
"""
search_results = self.journal.search_for_user(term=username, first=10)

if search_results[0].username == username:
return search_results[0].to_profile(self, album_limit=album_limit, friends_limit=friends_limit)

raise UserNotFoundException(f"Could not find user with username: \"{username}\"")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = "1.0.2"
VERSION = "1.0.3"
DESCRIPTION = "A Python API wrapper for the social media app Lapse."

with open("README.md", 'r') as f:
Expand Down

0 comments on commit d712564

Please sign in to comment.