1.3.0 • Published 4 months ago

plex-api-oauth v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months 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

4 months ago

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.147

2 years ago

1.1.146

2 years ago

1.1.145

2 years ago

1.1.144

2 years ago

1.1.143

2 years ago

1.1.142

2 years ago

1.1.141

2 years ago

1.1.140

2 years ago

1.1.139

2 years ago

1.1.137

2 years ago

1.1.136

2 years ago

1.1.135

2 years ago

1.1.134

2 years ago

1.1.133

2 years ago

1.1.132

2 years ago

1.1.131

2 years ago

1.1.130

2 years ago

1.1.129

2 years ago

1.1.128

2 years ago

1.1.127

2 years ago

1.1.126

2 years ago

1.1.125

2 years ago

1.1.124

2 years ago

1.1.123

2 years ago

1.1.122

2 years ago

1.1.121

2 years ago

1.1.120

2 years ago

1.1.119

2 years ago

1.1.118

2 years ago

1.1.117

2 years ago

1.1.116

2 years ago

1.1.115

2 years ago

1.1.114

2 years ago

1.1.113

2 years ago

1.1.112

2 years ago

1.1.111

2 years ago

1.1.110

2 years ago

1.1.109

2 years ago

1.1.108

2 years ago

1.1.106

2 years ago

1.1.105

2 years ago

1.1.104

2 years ago

1.1.103

2 years ago

1.1.102

2 years ago

1.1.101

2 years ago

1.1.100

2 years ago

1.1.99

2 years ago

1.1.98

2 years ago

1.1.97

2 years ago

1.1.96

2 years ago

1.1.95

2 years ago

1.1.94

2 years ago

1.1.93

2 years ago

1.1.92

2 years ago

1.1.91

2 years ago

1.1.90

2 years ago

1.1.89

2 years ago

1.1.88

2 years ago

1.1.87

2 years ago

1.1.86

2 years ago

1.1.85

2 years ago

1.1.84

2 years ago

1.1.83

2 years ago

1.1.82

2 years ago

1.1.81

2 years ago

1.1.80

2 years ago

1.1.79

2 years ago

1.1.78

2 years ago

1.1.76

2 years ago

1.1.75

2 years ago

1.1.74

2 years ago

1.1.73

2 years ago

1.1.72

2 years ago

1.1.71

2 years ago

1.1.70

2 years ago

1.1.69

2 years ago

1.1.68

2 years ago

1.1.67

2 years ago

1.1.66

2 years ago

1.1.65

2 years ago

1.1.64

2 years ago

1.1.63

2 years ago

1.1.62

2 years ago

1.1.61

2 years ago

1.1.60

2 years ago

1.1.59

2 years ago

1.1.58

2 years ago

1.1.57

2 years ago

1.1.56

2 years ago

1.1.55

2 years ago

1.1.54

2 years ago

1.1.53

2 years ago

1.1.52

2 years ago

1.1.51

2 years ago

1.1.50

2 years ago

1.1.49

2 years ago

1.1.48

2 years ago

1.1.47

2 years ago

1.1.46

2 years ago

1.1.45

2 years ago

1.1.44

2 years ago

1.1.43

2 years ago

1.1.42

2 years ago

1.1.41

2 years ago

1.1.40

2 years ago

1.1.39

2 years ago

1.1.38

2 years ago

1.1.37

2 years ago

1.1.36

2 years ago

1.1.35

2 years ago

1.1.34

2 years ago

1.1.33

2 years ago

1.1.32

2 years ago

1.1.31

2 years ago

1.1.30

2 years ago

1.1.29

2 years ago

1.1.28

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.24

2 years ago

1.1.23

2 years ago

1.1.21

2 years ago

1.1.20

2 years ago

1.1.19

2 years ago

1.1.18

2 years ago

1.1.17

2 years ago

1.1.16

2 years ago

1.1.15

2 years ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.140

2 years ago

1.0.139

2 years ago

1.0.138

2 years ago

1.0.137

2 years ago

1.0.136

2 years ago

1.0.135

2 years ago

1.0.134

2 years ago

1.0.133

2 years ago

1.0.132

2 years ago

1.0.131

2 years ago

1.0.130

2 years ago

1.0.129

2 years ago

1.0.128

2 years ago

1.0.127

2 years ago

1.0.126

2 years ago

1.0.125

2 years ago

1.0.124

2 years ago

1.0.123

2 years ago

1.0.122

2 years ago

1.0.121

2 years ago

1.0.120

2 years ago

1.0.119

2 years ago

1.0.118

2 years ago

1.0.117

2 years ago

1.0.116

2 years ago

1.0.115

2 years ago

1.0.114

2 years ago

1.0.113

2 years ago

1.0.112

2 years ago

1.0.111

2 years ago

1.0.110

2 years ago

1.0.109

2 years ago

1.0.108

2 years ago

1.0.107

2 years ago

1.0.106

2 years ago

1.0.105

2 years ago

1.0.104

2 years ago

1.0.103

2 years ago

1.0.102

2 years ago

1.0.101

2 years ago

1.0.100

2 years ago

1.0.99

2 years ago

1.0.98

2 years ago

1.0.97

2 years ago

1.0.96

2 years ago

1.0.95

2 years ago

1.0.94

2 years ago

1.0.93

2 years ago

1.0.92

2 years ago

1.0.91

2 years ago

1.0.90

2 years ago

1.0.89

2 years ago

1.0.88

2 years ago

1.0.87

2 years ago

1.0.86

2 years ago

1.0.85

2 years ago

1.0.84

2 years ago

1.0.83

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.0.80

2 years ago

1.0.79

2 years ago

1.0.78

2 years ago

1.0.77

2 years ago

1.0.76

2 years ago

1.0.75

2 years ago

1.0.74

2 years ago

1.0.73

2 years ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.69

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.60

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago