Skip to content

Commit

Permalink
Merge release/v4 into main (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriusnottin authored May 13, 2023
1 parent 7052904 commit 2d68752
Show file tree
Hide file tree
Showing 26 changed files with 584 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: [siriusnottin]
custom: ["https://buymeacoffee.com/siriusnottin"]
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Coda.io
.coda*
_upload_build

# Other
*.paw

# Logs
logs
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ Use this pack to synchronise your photos and albums right inside Coda!
- [x] Filter photos by favorites
- [x] Filter photos by date
- [x] Filter photos by categories (include)
- [x] Filter photos by categories (exclude) (soon)
- [ ] See which photos belong to what album (soon)
- [ ] Get details informations about an album or a photo (soon)
- [ ] Refactor the code for better readability and organization (I’m still learning)
- [x] Filter photos by categories (exclude)
- [x] See which photos belong to what album (soon) [#5](https://github.com/siriusnottin/google-photos-pack/issues/5)
- [ ] Get details informations about an album or a photo (soon) [#6](https://github.com/siriusnottin/google-photos-pack/issues/6)

## Changelog

Expand Down
8 changes: 8 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# google-photos-api

Google photos api client.

This is a fork of [a PR that was never merged](https://github.com/roopakv/google-photos/pull/27).

Link to the original repo:
https://github.com/roopakv/google-photos
32 changes: 32 additions & 0 deletions api/albums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Transport } from "api/transport";

export class Albums {
constructor(public transport: Transport) { }

list(pageSize = 50, pageToken?: string) { // range: 20-50
return this.transport.get("albums", { pageSize, pageToken });
}

get(albumId: string) {
return this.transport.get(`albums/${albumId}`);
}

create(title: string, description?: string) {
}

share(albumId: string) {
}

unshare(albumId: string) {
}

addMediaItems(albumId: string, mediaItemIds: string[]) {
}

removeMediaItems(albumId: string, mediaItemIds: string[]) {
}

update(albumId: string, title: string, description?: string) {
}

}
22 changes: 22 additions & 0 deletions api/common/date-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GDate } from "./gdate";
import { DateRange } from "./date-range";

export class DateFilter {
public dates: ReturnType<typeof GDate.newDate>[] = [];
public ranges: DateRange[] = [];

addDate(date) {
this.dates.push(GDate.newDate(date));
}

addRange(startDate, endDate) {
this.ranges.push(new DateRange(startDate, endDate));
}

toJSON() {
return {
dates: this.dates.map(d => d.toJSON()),
ranges: this.ranges.map(r => r.toJSON()),
}
}
}
16 changes: 16 additions & 0 deletions api/common/date-range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GDate } from "./gdate";
import { MediaItemsFilter } from "types/api-types";

export class DateRange {
constructor(
public startDate: ReturnType<GDate['toJSON']> | GDate | Date,
public endDate: ReturnType<GDate['toJSON']> | GDate | Date
) { }

toJSON(): MediaItemsFilter['dateFilter']['ranges'][0] {
return {
startDate: GDate.newDate(this.startDate).toJSON(),
endDate: GDate.newDate(this.endDate).toJSON()
}
}
}
45 changes: 45 additions & 0 deletions api/common/gdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Moment, isMoment } from "moment";
import { GPhotosDate } from "types/api-types";

export class GDate {
constructor(public year?: number, public month?: number, public day?: number) { }

static fromDate(date: Date) {
if (!(date instanceof Date)) {
throw Error('Not a valid date object');
}
return new GDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
}

static fromMoment(moment: Moment) {
if (!isMoment(moment)) {
throw Error('Not a valid moment object');
}
return new GDate(moment.year(), moment.month() + 1, moment.date());
}

static newDate(date?: GDate | Date | Moment | GPhotosDate) {
if (date instanceof GDate) {
return date;
}
if (date instanceof Date) {
return GDate.fromDate(date);
}
if (isMoment(date)) {
return GDate.fromMoment(date);
}
if (date.year && date.month && date.day) {
return new GDate(date.year, date.month, date.day);
}
return new GDate();
}

toJSON(): GPhotosDate {
return {
year: this.year,
month: this.month,
day: this.day
}
}

}
32 changes: 32 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as coda from "@codahq/packs-sdk";

import { Transport } from "./transport";

import { Albums } from "./albums";
import { MediaItems } from "./media-items";

import { DateFilter } from "./common/date-filter";
import { MediaTypeFilter } from "./media-items/media-type-filter";
import { FeatureFilter } from "./media-items/feature-filter"
import { ContentFilter } from "./media-items/content-filter";
import { Filters } from "./media-items/filters";
export default class GPhotos {

public readonly transport: Transport;

public readonly albums: Albums;
public readonly mediaItems: MediaItems;

public readonly DateFilter = DateFilter;
public readonly MediaTypeFilter = MediaTypeFilter;
public readonly FeatureFilter = FeatureFilter;
public readonly ContentFilter = ContentFilter;
public readonly Filters = Filters;

constructor(public readonly fetcher: coda.Fetcher) {
this.transport = new Transport(fetcher);
this.albums = new Albums(this.transport);
this.mediaItems = new MediaItems(this.transport);
}

}
22 changes: 22 additions & 0 deletions api/media-items/content-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MediasContentCategories } from 'types/api-types';

export class ContentFilter {

public includedContentCategories: MediasContentCategories[] = []
public excludedContentCategories: MediasContentCategories[] = []

addIncludedCategory(category: MediasContentCategories) {
this.includedContentCategories.push(category);
}

addExcludedCategory(category: MediasContentCategories) {
this.excludedContentCategories.push(category);
}

toJSON() {
return {
includedContentCategories: this.includedContentCategories,
excludedContentCategories: this.excludedContentCategories,
}
}
}
21 changes: 21 additions & 0 deletions api/media-items/feature-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { MediaFeature, MediaItemsFilter } from "types/api-types";

export class FeatureFilter {
public includedFeatures: [MediaFeature];

constructor(includedFeature = MediaFeature.None) {
this.includedFeatures = [includedFeature];
}

setFeature(feature: MediaFeature) {
this.includedFeatures = [feature];
}

toJSON(): MediaItemsFilter['featureFilter'] {
return {
includedFeatures: this.includedFeatures
}
}

}
44 changes: 44 additions & 0 deletions api/media-items/filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { DateFilter } from "../common/date-filter";
import { MediaTypeFilter } from "./media-type-filter";
import { ContentFilter } from "./content-filter";
import { MediaItemsFilter } from "types/api-types";
import { FeatureFilter } from "./feature-filter";

export class Filters {
public dateFilter: DateFilter;
public mediaTypeFilter: MediaTypeFilter;
public contentFilter: ContentFilter;
public featureFilter: FeatureFilter;

constructor(public includeArchivedMedia = false) { }

setDateFilter(dateFilter: DateFilter) {
this.dateFilter = dateFilter;
}

setMediaTypeFilter(mediaTypeFilter: MediaTypeFilter) {
this.mediaTypeFilter = mediaTypeFilter;
}

setContentFilter(contentFilter: ContentFilter) {
this.contentFilter = contentFilter;
}

setIncludeArchivedMedia(includeArchivedMedia: boolean) {
this.includeArchivedMedia = includeArchivedMedia;
}

setFeatureFilter(featureFilter: FeatureFilter) {
this.featureFilter = featureFilter;
}

toJSON(): MediaItemsFilter {
return {
dateFilter: this.dateFilter,
mediaTypeFilter: this.mediaTypeFilter,
contentFilter: this.contentFilter,
includeArchivedMedia: this.includeArchivedMedia,
featureFilter: this.featureFilter,
}
}
}
34 changes: 34 additions & 0 deletions api/media-items/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Transport } from "api/transport";
import { Filters } from "./filters";
import { FetchResponse } from "@codahq/packs-sdk";
import { ApiResponse } from "types/api-types";
export class MediaItems {
constructor(public transport: Transport) { }

list(pageToken?: string, pageSize = 100) { // range: 25-100
return this.transport.get("mediaItems", { pageSize, pageToken });
}

get(mediaItemId: string) {
return this.transport.get(`mediaItems/${mediaItemId}`);
}

search(albumId: string, pageToken?: string, pageSize?: number, fields?: string): Promise<FetchResponse<ApiResponse>>
search(filters: Filters, pageToken?: string, pageSize?: number, fields?: string): Promise<FetchResponse<ApiResponse>>;

search(albumIdOrFilters: string | Filters, pageToken?: string, pageSize = 100, fields?: string) {
const body: {
pageSize?: number;
pageToken?: string;
albumId?: string;
filters?: ReturnType<Filters["toJSON"]>;
} = { pageSize, pageToken }
if (typeof albumIdOrFilters === "string") {
body.albumId = albumIdOrFilters;
} else {
body.filters = albumIdOrFilters.toJSON();
}
return this.transport.post("mediaItems:search", { fields }, body);
}

}
19 changes: 19 additions & 0 deletions api/media-items/media-type-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MediaTypes, MediaItemsFilter } from 'types/api-types';

export class MediaTypeFilter {
public mediaTypes: MediaTypes[];

constructor(type = MediaTypes.All) {
this.mediaTypes = [type];
}

setType(type: MediaTypes) {
this.mediaTypes = [type];
}

toJSON(): MediaItemsFilter['mediaTypeFilter'] {
return {
mediaTypes: this.mediaTypes
}
}
}
17 changes: 17 additions & 0 deletions api/shared_albums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Transport } from "api/transport";

export class SharedAlbums {
constructor(public transport: Transport) { }

list(pageSize = 50, pageToken?: string) {
}

get(id: string) {
}

join(id: string) {
}

leave(id: string) {
}
}
Loading

0 comments on commit 2d68752

Please sign in to comment.