sudoguy / tiktokpy

Tool for automated TikTok interactions
MIT License
711 stars 146 forks source link

New: add new ImagePost FeedItem model #330

Open jbelinchonm opened 10 months ago

jbelinchonm commented 10 months ago

I was testing this code and looked promising. The model did not implement ImagePost as a FeedItem, I included the basic classes so that both videos and imagePosts can be successfully handled by the model.

I have not dived in at all, the changes are basically:

Added a new property to the existing FeedItem class, when the item is a regular video, you do not receive the new property, so it is initialized to None.

class FeedItem(BaseModel):
    id: str
    ...
    video: VideoInfo
+    image_post: ImagePostlInfo = None
    challenges: List[ChallengeInfo] = []

When the item is ImagePost, it still has video property, but two required properties will be missing, so I made them Optional in the VideoInfo definition:

class VideoInfo(BaseModel):
    id: str
    ...
    cover: HttpUrl
-  play_addr: HttpUrl
-  download_addr: HttpUrl
+  play_addr: Optional[HttpUrl]
+  download_addr: Optional[HttpUrl]

Lastly I realized that in the new imagePost data, properties with 'image type' values had a consistent structure (imagePost cover, shareCover and images). So I added imagePostInfo model and a new class for these properties:

+ class ImageUrls(BaseModel):
+     url_list: List[HttpUrl]
+ 
+     class Config:
+         fields: ClassVar[dict] = {"url_list": "urlList"}
+ 
+ 
+ class ImageInfo(BaseModel):
+     image_height: int
+     image_width: int
+     image_url: ImageUrls
+ 
+     class Config:
+         fields: ClassVar[dict] = {
+             "image_url": "imageURL",
+             "image_width": "imageWidth",
+             "image_height": "imageHeight",
+         }
+ 
+ 
+ class ImagePostlInfo(BaseModel):
+     cover: ImageInfo
+     images: List[ImageInfo]