1.3.0 • Published 2 years ago

plex-api-oauth v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years 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

2 years 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

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.147

4 years ago

1.1.146

4 years ago

1.1.145

4 years ago

1.1.144

4 years ago

1.1.143

4 years ago

1.1.142

4 years ago

1.1.141

4 years ago

1.1.140

4 years ago

1.1.139

4 years ago

1.1.137

4 years ago

1.1.136

4 years ago

1.1.135

4 years ago

1.1.134

4 years ago

1.1.133

4 years ago

1.1.132

4 years ago

1.1.131

4 years ago

1.1.130

4 years ago

1.1.129

4 years ago

1.1.128

4 years ago

1.1.127

4 years ago

1.1.126

4 years ago

1.1.125

4 years ago

1.1.124

4 years ago

1.1.123

4 years ago

1.1.122

4 years ago

1.1.121

4 years ago

1.1.120

4 years ago

1.1.119

4 years ago

1.1.118

4 years ago

1.1.117

4 years ago

1.1.116

4 years ago

1.1.115

4 years ago

1.1.114

4 years ago

1.1.113

4 years ago

1.1.112

4 years ago

1.1.111

4 years ago

1.1.110

4 years ago

1.1.109

4 years ago

1.1.108

4 years ago

1.1.106

4 years ago

1.1.105

4 years ago

1.1.104

4 years ago

1.1.103

4 years ago

1.1.102

4 years ago

1.1.101

4 years ago

1.1.100

4 years ago

1.1.99

4 years ago

1.1.98

4 years ago

1.1.97

4 years ago

1.1.96

4 years ago

1.1.95

4 years ago

1.1.94

4 years ago

1.1.93

4 years ago

1.1.92

4 years ago

1.1.91

4 years ago

1.1.90

4 years ago

1.1.89

4 years ago

1.1.88

4 years ago

1.1.87

4 years ago

1.1.86

4 years ago

1.1.85

4 years ago

1.1.84

4 years ago

1.1.83

4 years ago

1.1.82

4 years ago

1.1.81

4 years ago

1.1.80

4 years ago

1.1.79

4 years ago

1.1.78

4 years ago

1.1.76

4 years ago

1.1.75

4 years ago

1.1.74

4 years ago

1.1.73

4 years ago

1.1.72

4 years ago

1.1.71

4 years ago

1.1.70

4 years ago

1.1.69

4 years ago

1.1.68

4 years ago

1.1.67

4 years ago

1.1.66

4 years ago

1.1.65

4 years ago

1.1.64

4 years ago

1.1.63

4 years ago

1.1.62

4 years ago

1.1.61

4 years ago

1.1.60

4 years ago

1.1.59

4 years ago

1.1.58

4 years ago

1.1.57

4 years ago

1.1.56

4 years ago

1.1.55

4 years ago

1.1.54

4 years ago

1.1.53

4 years ago

1.1.52

4 years ago

1.1.51

4 years ago

1.1.50

4 years ago

1.1.49

4 years ago

1.1.48

4 years ago

1.1.47

4 years ago

1.1.46

4 years ago

1.1.45

4 years ago

1.1.44

4 years ago

1.1.43

4 years ago

1.1.42

4 years ago

1.1.41

4 years ago

1.1.40

4 years ago

1.1.39

4 years ago

1.1.38

4 years ago

1.1.37

4 years ago

1.1.36

4 years ago

1.1.35

4 years ago

1.1.34

4 years ago

1.1.33

4 years ago

1.1.32

4 years ago

1.1.31

4 years ago

1.1.30

4 years ago

1.1.29

4 years ago

1.1.28

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.24

4 years ago

1.1.23

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.140

4 years ago

1.0.139

4 years ago

1.0.138

4 years ago

1.0.137

4 years ago

1.0.136

4 years ago

1.0.135

4 years ago

1.0.134

4 years ago

1.0.133

4 years ago

1.0.132

4 years ago

1.0.131

4 years ago

1.0.130

4 years ago

1.0.129

4 years ago

1.0.128

4 years ago

1.0.127

4 years ago

1.0.126

4 years ago

1.0.125

4 years ago

1.0.124

4 years ago

1.0.123

4 years ago

1.0.122

4 years ago

1.0.121

4 years ago

1.0.120

4 years ago

1.0.119

4 years ago

1.0.118

4 years ago

1.0.117

4 years ago

1.0.116

4 years ago

1.0.115

4 years ago

1.0.114

4 years ago

1.0.113

4 years ago

1.0.112

4 years ago

1.0.111

4 years ago

1.0.110

4 years ago

1.0.109

4 years ago

1.0.108

4 years ago

1.0.107

4 years ago

1.0.106

4 years ago

1.0.105

4 years ago

1.0.104

4 years ago

1.0.103

4 years ago

1.0.102

4 years ago

1.0.101

4 years ago

1.0.100

4 years ago

1.0.99

4 years ago

1.0.98

4 years ago

1.0.97

4 years ago

1.0.96

4 years ago

1.0.95

4 years ago

1.0.94

4 years ago

1.0.93

4 years ago

1.0.92

4 years ago

1.0.91

4 years ago

1.0.90

4 years ago

1.0.89

4 years ago

1.0.88

4 years ago

1.0.87

4 years ago

1.0.86

4 years ago

1.0.85

4 years ago

1.0.84

4 years ago

1.0.83

4 years ago

1.0.82

4 years ago

1.0.81

4 years ago

1.0.80

4 years ago

1.0.79

4 years ago

1.0.78

4 years ago

1.0.77

4 years ago

1.0.76

4 years ago

1.0.75

4 years ago

1.0.74

4 years ago

1.0.73

4 years ago

1.0.72

4 years ago

1.0.71

4 years ago

1.0.70

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.67

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.64

4 years ago

1.0.63

4 years ago

1.0.62

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.52

4 years ago

1.0.51

4 years ago

1.0.50

4 years ago

1.0.49

4 years ago

1.0.48

4 years ago

1.0.47

4 years ago

1.0.46

4 years ago

1.0.45

4 years ago

1.0.44

4 years ago

1.0.43

4 years ago

1.0.42

4 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.39

4 years ago

1.0.38

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.33

4 years ago

1.0.32

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.29

4 years ago

1.0.28

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago