Skip to content

Commit

Permalink
Support for old json metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
UltimaHoarder committed Jun 14, 2021
1 parent d375721 commit 38b96b1
Showing 1 changed file with 58 additions and 47 deletions.
105 changes: 58 additions & 47 deletions classes/prepare_metadata.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
from apis.onlyfans.classes.extras import media_types
from apis.onlyfans.classes.create_auth import create_auth
import copy
from enum import unique
import os
from enum import unique
from itertools import chain, groupby
from math import exp
from typing import Dict, MutableMapping, Union

from requests.api import get
from helpers import main_helper
from itertools import groupby, chain
from math import exp
import jsonpickle
from apis.onlyfans.classes.create_auth import create_auth
from apis.onlyfans.classes.extras import media_types
from helpers import main_helper
from requests.api import get

global_version = 2


class create_metadata(object):
def __init__(self, authed: create_auth = None, metadata: Union[list,dict,MutableMapping] = {}, standard_format=False, api_type: str = "") -> None:
def __init__(
self,
authed: create_auth = None,
metadata: Union[list, dict, MutableMapping] = {},
standard_format=False,
api_type: str = "",
) -> None:
self.version = global_version
fixed_metadata = self.fix_metadata(metadata, standard_format, api_type)
self.content = format_content(
authed, fixed_metadata["version"], fixed_metadata["content"]).content
authed, fixed_metadata["version"], fixed_metadata["content"]
).content

def fix_metadata(self, metadata, standard_format=False, api_type: str = "") -> dict:
new_format = {}
Expand Down Expand Up @@ -57,6 +64,8 @@ def fix_metadata(self, metadata, standard_format=False, api_type: str = "") -> d
if any(x for x in metadata if x in media_types().__dict__.keys()):
metadata.pop("directories", None)
for key, status in metadata.items():
if isinstance(status, int):
continue
for key2, posts in status.items():
if all(x and isinstance(x, list) for x in posts):
posts = list(chain(*posts))
Expand Down Expand Up @@ -94,8 +103,7 @@ def export(self, convert_type="json", keep_empty_items=False) -> dict:
print
print
print
value = jsonpickle.encode(
new_format_copied, unpicklable=False)
value = jsonpickle.encode(new_format_copied, unpicklable=False)
value = jsonpickle.decode(value)
if not isinstance(value, dict):
return {}
Expand All @@ -107,8 +115,7 @@ def convert(self, convert_type="json", keep_empty_items=False) -> dict:
value = {}
if convert_type == "json":
new_format_copied = copy.deepcopy(self)
value = jsonpickle.encode(
new_format_copied, unpicklable=False)
value = jsonpickle.encode(new_format_copied, unpicklable=False)
value = jsonpickle.decode(value)
if not isinstance(value, dict):
return {}
Expand All @@ -128,8 +135,15 @@ def __iter__(self):


class format_content(object):
def __init__(self, authed=None, version=None, old_content: dict = {}, export=False, reformat=False, args={}):

def __init__(
self,
authed=None,
version=None,
old_content: dict = {},
export=False,
reformat=False,
args={},
):
class assign_state(object):
def __init__(self) -> None:
self.valid = []
Expand All @@ -138,6 +152,7 @@ def __init__(self) -> None:
def __iter__(self):
for attr, value in self.__dict__.items():
yield attr, value

old_content.pop("directories", None)
new_content = media_types(assign_states=assign_state)
for key, new_item in new_content:
Expand All @@ -161,8 +176,10 @@ def __iter__(self):

elif version == 1:
old_item2.sort(key=lambda x: x["post_id"])
media_list = [list(g) for k, g in groupby(
old_item2, key=lambda x: x["post_id"])]
media_list = [
list(g)
for k, g in groupby(old_item2, key=lambda x: x["post_id"])
]
for media_list2 in media_list:
old_post = media_list2[0]
post = self.post_item(old_post)
Expand Down Expand Up @@ -194,8 +211,7 @@ def convert(self, convert_type="json", keep_empty_items=False) -> dict:
new_format_copied = copy.deepcopy(self)
for media in new_format_copied.medias:
media.convert()
value = jsonpickle.encode(
new_format_copied, unpicklable=False)
value = jsonpickle.encode(new_format_copied, unpicklable=False)
value = jsonpickle.decode(value)
if not isinstance(value, dict):
return {}
Expand All @@ -222,8 +238,7 @@ def convert(self, convert_type="json", keep_empty_items=False) -> dict:
if convert_type == "json":
value.pop("session", None)
new_format_copied = copy.deepcopy(self)
value = jsonpickle.encode(
new_format_copied, unpicklable=False)
value = jsonpickle.encode(new_format_copied, unpicklable=False)
value = jsonpickle.decode(value)
if not isinstance(value, dict):
return {}
Expand Down Expand Up @@ -262,12 +277,11 @@ def __iter__(self):
yield attr, value


class format_types():
class format_types:
def __init__(self, options) -> None:
self.file_directory_format = options.get("file_directory_format")
self.filename_format = options.get("filename_format")
self.metadata_directory_format = options.get(
"metadata_directory_format")
self.metadata_directory_format = options.get("metadata_directory_format")

def check_rules(self):
bool_status = True
Expand All @@ -292,8 +306,7 @@ def check_rules(self):
if b in self.filename_format:
invalid_list.append(b)
if key == "metadata_directory_format":
wl = ["{site_name}", "{first_letter}",
"{model_id}", "{username}"]
wl = ["{site_name}", "{first_letter}", "{model_id}", "{username}"]
bl = format_variables().whitelist(wl)
invalid_list = []
for b in bl:
Expand Down Expand Up @@ -343,7 +356,8 @@ def check_unique(self, return_unique=True):
setattr(option["unique"], key, e)
else:
option[
"string"] += f"{key} is a invalid format since it has no unique identifiers. Use any from this list {','.join(unique)}\n"
"string"
] += f"{key} is a invalid format since it has no unique identifiers. Use any from this list {','.join(unique)}\n"
option["bool_status"] = False
return option

Expand All @@ -355,25 +369,23 @@ def __iter__(self):
class prepare_reformat(object):
def __init__(self, option, keep_vars=False):
format_variables2 = format_variables()
self.site_name = option.get('site_name', format_variables2.site_name)
self.post_id = option.get('post_id', format_variables2.post_id)
self.media_id = option.get('media_id', format_variables2.media_id)
self.username = option.get('username', format_variables2.username)
self.api_type = option.get('api_type', format_variables2.api_type)
self.media_type = option.get(
'media_type', format_variables2.media_type)
self.filename = option.get('filename', format_variables2.filename)
self.ext = option.get('ext', format_variables2.ext)
self.text = option.get('text', format_variables2.text)
self.date = option.get('postedAt', format_variables2.date)
self.price = option.get('price', 0)
self.archived = option.get('archived', False)
self.date_format = option.get('date_format')
self.site_name = option.get("site_name", format_variables2.site_name)
self.post_id = option.get("post_id", format_variables2.post_id)
self.media_id = option.get("media_id", format_variables2.media_id)
self.username = option.get("username", format_variables2.username)
self.api_type = option.get("api_type", format_variables2.api_type)
self.media_type = option.get("media_type", format_variables2.media_type)
self.filename = option.get("filename", format_variables2.filename)
self.ext = option.get("ext", format_variables2.ext)
self.text = option.get("text", format_variables2.text)
self.date = option.get("postedAt", format_variables2.date)
self.price = option.get("price", 0)
self.archived = option.get("archived", False)
self.date_format = option.get("date_format")
self.maximum_length = 255
self.text_length = option.get('text_length', self.maximum_length)
self.directory = option.get(
'directory')
self.preview = option.get('preview')
self.text_length = option.get("text_length", self.maximum_length)
self.directory = option.get("directory")
self.preview = option.get("preview")
if not keep_vars:
for key, value in self:
print
Expand Down Expand Up @@ -417,8 +429,7 @@ def convert(self, convert_type="json", keep_empty_items=False) -> dict:
if convert_type == "json":
new_format_copied = copy.deepcopy(self)
delattr(new_format_copied, "session")
value = jsonpickle.encode(
new_format_copied, unpicklable=False)
value = jsonpickle.encode(new_format_copied, unpicklable=False)
value = jsonpickle.decode(value)
if not isinstance(value, dict):
return {}
Expand Down

0 comments on commit 38b96b1

Please sign in to comment.