import os, sys
from datetime import datetime
from dateutil import relativedelta

import django
sys.path.append('../../../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ds_events.settings'
django.setup()

from time import sleep
from pprint import pprint as ppr
import pytz

import praw

from socials.models import SocialLink, SocialPost
# from digitools import getBrowser, createURL, createBasicEvent, getSource

tz = pytz.timezone("US/Central")


# timestamp = 1729322547223
# dt_object = datetime.datetime.fromtimestamp(timestamp)
# print(dt_object)

reddit = praw.Reddit(
    client_id="rxW3Ywqke6FZDP7pIhYYuw",
    client_secret="cg1VNl0I-RTuYUwgz16ryKh2wWKEcA",
    password="7CTu4sGFi9E0",
    user_agent="CultureClap",
    username="cultureclap",
)


def createSocialLink(post):
    new_post, created = SocialLink.objects.update_or_create(
        text = post['text'],
        link = post['link'],
        handle = post['handle'],
        likes = post['likes'],
        replies = post['replies'],
        platform = post['platform'],
        created_at = post['created_at'],
        rt_uri = 'blank',
        rt_text = 'blank',
        rt_link = 'blank',
        rt_handle = 'blank',
    )
    print(created, new_post)

count = 0

for item in reddit.user.me().upvoted():
    rdt = {}
    rdt['text'] = item.title + " | " + item.selftext
    rdt['handle'] = item.author.name
    rdt['link'] = item.url
    rdt['likes'] = item.ups
    rdt['replies'] = len(item.comments.list())
    rdt['created_at'] = datetime.fromtimestamp(item.created_utc)
    rdt['platform'] = 'reddit'
    
    try:
        print('writing file')
        createSocialLink(rdt)
        count +=1
        if count > 50:
            quit()
    except Exception as e:
        ppr(item)
        print(e, "\nthis\n\n")
    # ppr(item)