1.3.0 • Published 1 year ago

plex-api-oauth v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

plex-api-oauth

THIS PACKAGE IS DEPRECATED IN FAVOR OF https://github.com/LukeHagar/plexjs

An NPM Module designed to make Plex Media Server and plex.tv API calls easier to implement in JavaScript and React projects

https://www.npmjs.com/package/plex-api-oauth

This is a JavaScript Module written to take the OAuth Module written by @Dmbob https://github.com/Dmbob/plex-oauth and incorporate it into a front end friend frame work to make development of Plex API Based applications better

How to Use

Examples Assume React Syntax

Loading a Saved Session

LoadPlexSession:
Function returns an object that contains the plexClientInformation and plexTVAuthToken keys/values according to how they were created

const loadedSession = LoadPlexSession();
if (loadedSession.plexClientInformation == null){
    loadedSession.plexClientInformation = CreatePlexClientInformation();
}

const [plexClientInformation, setPlexClientInformation] = useState(
  loadedSession.plexClientInformation
);
const [plexTVAuthToken, setPlexTVAuthToken] = useState(
  loadedSession.plexTVAuthToken
);

Login Button + Save Session

 async function PlexLoginButton() {
    const tempPlexTVAuthToken = await PlexLogin(plexClientInformation);
    const tempPlexTVUserData = await GetPlexUserData(
      plexClientInformation,
      tempPlexTVAuthToken
    );
    const tempPlexServers = await GetPlexServers(
      plexClientInformation,
      tempPlexTVAuthToken
    );
    const tempPlexLibraries = await GetPlexLibraries(tempPlexServers);
    setPlexTVAuthToken(tempPlexTVAuthToken);
    setPlexServers(tempPlexServers);
    setPlexTVUserData(tempPlexTVUserData);
    setPlexLibraries(tempPlexLibraries);
    SavePlexSession(plexClientInformation, tempPlexTVAuthToken);
  }

Refresh Data with Saved Session

 async function Refresh() {
    const tempPlexTVUserData = await GetPlexUserData(
      plexClientInformation,
      plexTVAuthToken
    );
    const tempPlexServers = await GetPlexServers(
      plexClientInformation,
      plexTVAuthToken
    );
    const tempPlexDevices = await GetPlexDevices(
      plexClientInformation,
      plexTVAuthToken
    );
    const tempPlexLibraries = await GetPlexLibraries(tempPlexServers);
    UpdateHubs(tempPlexServers, tempPlexLibraries);
    setPlexServers(tempPlexServers);
    setPlexDevices(tempPlexDevices);
    setPlexTVUserData(tempPlexTVUserData);
    setPlexLibraries(tempPlexLibraries);
    setIsRefreshing(false);
  }

Update Library list when the topic is changed (artists. albums, songs)

 async function UpdateLibrary() {
    setIsLoading(true);
    const returnObject = await GetLibraryPages(
      plexServers,
      plexLibraries,
      topic,
      pageNumber,
      250
    );
    console.log(returnObject);
    const tempItemArray = Array.from([
      ...new Set([...libraryItems, ...returnObject.items]),
    ]);

    setLibraryItems(tempItemArray);
    setLibraryHasMore(returnObject.hasMore);
    setIsLoading(false);
  }

Intersection observer to load more library items when the callbackref object comes into view

 const observer = useRef();
  const lastLibraryItem = useCallback(
    (node) => {
      if (isLoading) return;
      if (observer.current) observer.current.disconnect();
      observer.current = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && libraryHasMore) {
          setPageNumber(pageNumber + 1);
        }
      });
      if (node) observer.current.observe(node);
      console.log(node);
    },
    [isLoading, libraryHasMore]
  );

Intersection observer Callback usage

  if (libraryItems?.length === index + 100) {
    return (
          <Grid item xs="12" key={Obj.guid + index}>
            <ListItem ref={lastLibraryItem}>
              <ListItemAvatar>
                <Avatar alt={NoArt} src={Obj.thumb} />
              </ListItemAvatar>
              <Typography variant="h6" noWrap>
                {Obj.title} - {Obj.grandparentTitle} -{' '}
                {Obj.parentTitle}
              </Typography>
            </ListItem>
        </Grid>
      );
    }

Intersection observer to load more library items when the callbackref object comes into view

 const observer = useRef();
  const lastLibraryItem = useCallback(
    (node) => {
      if (isLoading) return;
      if (observer.current) observer.current.disconnect();
      observer.current = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && libraryHasMore) {
          setPageNumber(pageNumber + 1);
        }
      });
      if (node) observer.current.observe(node);
      console.log(node);
    },
    [isLoading, libraryHasMore]
  );

Get Plex Music Hub data

 async function UpdateHubs(plexClientInformation, plexServers, plexLibraries) {
    const tempMusicHubs = await GetMusicHub(
      plexClientInformation,
      plexServers, // No Auth Token is needed since the server objects have their own access tokens
      plexLibraries
    );
    setMusicHubs(tempMusicHubs);
  }

Commands:

CreatePlexClientInformation

Accepts: input values

Generates the Client Information to uniquely identify the Authenticated client Save between sessions for consistency

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

PlexLogin

Accepts: plexClientInformation

If command is run in a browser window it will open the new login window automatically

plexTVAuthToken = PlexLogin(plexClientInformation);

GetPlexUserData

Accepts: plexClientInformation, plexTVAuthToken

Queries the plex.tv api to get information on the authenticated user

plexUserData = GetPlexUserData(plexClientInformation, plexTVAuthToken);

GetPlexServers

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

plexServers = GetPlexServers(plexClientInformation, plexTVAuthToken);

GetPlexMovies

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexShows

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexSeasons

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexEpisodes

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexMovieLibraries

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexMusicLibraries

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexTVShowLibraries

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexArtists

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexAlbums

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexSongs

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexLibraries

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetPlexDevices

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

### GetLibraryPages

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

LoadPlexSession

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

SavePlexSession

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetMusicHub

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);

GetArtistPage

Accepts an Object with partial client values, will populate the remaining ones left blank if needed

input = {product:"LukeHagar.com"}
plexClientInformation = CreatePlexClientInformation(input);
1.3.0

1 year ago

1.2.9

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.147

3 years ago

1.1.146

3 years ago

1.1.145

3 years ago

1.1.144

3 years ago

1.1.143

3 years ago

1.1.142

3 years ago

1.1.141

3 years ago

1.1.140

3 years ago

1.1.139

3 years ago

1.1.137

3 years ago

1.1.136

3 years ago

1.1.135

3 years ago

1.1.134

3 years ago

1.1.133

3 years ago

1.1.132

3 years ago

1.1.131

3 years ago

1.1.130

3 years ago

1.1.129

3 years ago

1.1.128

3 years ago

1.1.127

3 years ago

1.1.126

3 years ago

1.1.125

3 years ago

1.1.124

3 years ago

1.1.123

3 years ago

1.1.122

3 years ago

1.1.121

3 years ago

1.1.120

3 years ago

1.1.119

3 years ago

1.1.118

3 years ago

1.1.117

3 years ago

1.1.116

3 years ago

1.1.115

3 years ago

1.1.114

3 years ago

1.1.113

3 years ago

1.1.112

3 years ago

1.1.111

3 years ago

1.1.110

3 years ago

1.1.109

3 years ago

1.1.108

3 years ago

1.1.106

3 years ago

1.1.105

3 years ago

1.1.104

3 years ago

1.1.103

3 years ago

1.1.102

3 years ago

1.1.101

3 years ago

1.1.100

3 years ago

1.1.99

3 years ago

1.1.98

3 years ago

1.1.97

3 years ago

1.1.96

3 years ago

1.1.95

3 years ago

1.1.94

3 years ago

1.1.93

3 years ago

1.1.92

3 years ago

1.1.91

3 years ago

1.1.90

3 years ago

1.1.89

3 years ago

1.1.88

3 years ago

1.1.87

3 years ago

1.1.86

3 years ago

1.1.85

3 years ago

1.1.84

3 years ago

1.1.83

3 years ago

1.1.82

3 years ago

1.1.81

3 years ago

1.1.80

3 years ago

1.1.79

3 years ago

1.1.78

3 years ago

1.1.76

3 years ago

1.1.75

3 years ago

1.1.74

3 years ago

1.1.73

3 years ago

1.1.72

3 years ago

1.1.71

3 years ago

1.1.70

3 years ago

1.1.69

3 years ago

1.1.68

3 years ago

1.1.67

3 years ago

1.1.66

3 years ago

1.1.65

3 years ago

1.1.64

3 years ago

1.1.63

3 years ago

1.1.62

3 years ago

1.1.61

3 years ago

1.1.60

3 years ago

1.1.59

3 years ago

1.1.58

3 years ago

1.1.57

3 years ago

1.1.56

3 years ago

1.1.55

3 years ago

1.1.54

3 years ago

1.1.53

3 years ago

1.1.52

3 years ago

1.1.51

3 years ago

1.1.50

3 years ago

1.1.49

3 years ago

1.1.48

3 years ago

1.1.47

3 years ago

1.1.46

3 years ago

1.1.45

3 years ago

1.1.44

3 years ago

1.1.43

3 years ago

1.1.42

3 years ago

1.1.41

3 years ago

1.1.40

3 years ago

1.1.39

3 years ago

1.1.38

3 years ago

1.1.37

3 years ago

1.1.36

3 years ago

1.1.35

3 years ago

1.1.34

3 years ago

1.1.33

3 years ago

1.1.32

3 years ago

1.1.31

3 years ago

1.1.30

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.23

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.140

3 years ago

1.0.139

3 years ago

1.0.138

3 years ago

1.0.137

3 years ago

1.0.136

3 years ago

1.0.135

3 years ago

1.0.134

3 years ago

1.0.133

3 years ago

1.0.132

3 years ago

1.0.131

3 years ago

1.0.130

3 years ago

1.0.129

3 years ago

1.0.128

3 years ago

1.0.127

3 years ago

1.0.126

3 years ago

1.0.125

3 years ago

1.0.124

3 years ago

1.0.123

3 years ago

1.0.122

3 years ago

1.0.121

3 years ago

1.0.120

3 years ago

1.0.119

3 years ago

1.0.118

3 years ago

1.0.117

3 years ago

1.0.116

3 years ago

1.0.115

3 years ago

1.0.114

3 years ago

1.0.113

3 years ago

1.0.112

3 years ago

1.0.111

3 years ago

1.0.110

3 years ago

1.0.109

3 years ago

1.0.108

3 years ago

1.0.107

3 years ago

1.0.106

3 years ago

1.0.105

3 years ago

1.0.104

3 years ago

1.0.103

3 years ago

1.0.102

3 years ago

1.0.101

3 years ago

1.0.100

3 years ago

1.0.99

3 years ago

1.0.98

3 years ago

1.0.97

3 years ago

1.0.96

3 years ago

1.0.95

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.92

3 years ago

1.0.91

3 years ago

1.0.90

3 years ago

1.0.89

3 years ago

1.0.88

3 years ago

1.0.87

3 years ago

1.0.86

3 years ago

1.0.85

3 years ago

1.0.84

3 years ago

1.0.83

3 years ago

1.0.82

3 years ago

1.0.81

3 years ago

1.0.80

3 years ago

1.0.79

3 years ago

1.0.78

3 years ago

1.0.77

3 years ago

1.0.76

3 years ago

1.0.75

3 years ago

1.0.74

3 years ago

1.0.73

3 years ago

1.0.72

3 years ago

1.0.71

3 years ago

1.0.70

3 years ago

1.0.69

3 years ago

1.0.68

3 years ago

1.0.67

3 years ago

1.0.66

3 years ago

1.0.65

3 years ago

1.0.64

3 years ago

1.0.63

3 years ago

1.0.62

3 years ago

1.0.61

3 years ago

1.0.60

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.56

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.49

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.40

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago