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

from atproto import Client

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

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

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

USERNAME = "dreamfreely.org"
PASSWORD = "Futbol21!@"

client = Client()
client.login(USERNAME, PASSWORD)
feed = client.get_author_feed(USERNAME, limit = 100)

def createSocialLink(post):
    new_post, created = SocialLink.objects.update_or_create(
        uri = post['uri'],
        text = post['text'],
        link = post['link'],
        handle = post['handle'],
        likes = post['likes'],
        reposts = post['reposts'],
        quotes = post['quotes'],
        replies = post['replies'],
        created_at = post['created_at'],
        platform = 'bluesky',
        rt_uri = post['rt_uri'],
        rt_text = post['rt_text'],
        rt_link = post['rt_link'],
        rt_handle = post['rt_handle'],
    )
    # print(created, new_post)
    print("completed write")

tweets = []

print(len(feed.feed))

for post in feed.feed:
    post = post.post
    print("\n\nNEW POST\n\n")
    # try:
    #     ppr(post.embed.record.record.author.handle)
    #     ppr(post.embed.record.record.value.text.split("\n")[:2])
    #     ppr(post.embed.record.record.value.embed.external.uri.split("?")[0])
    #     ppr(post.embed.record.record.uri.split("feed.post/")[1])
    # except:
    #     pass

    if hasattr(post.record.embed, 'external'):
        p = {}
        try:
            p['link'] = post.record.embed.external.uri.split("?")[0]
        except:
            pass
        p['text'] = " ".join(post.record.text.split("\n")[:2])
        p['handle'] = post.author.handle
        p['uri'] = post.uri.split("feed.post/")[1]
        p['likes'] = post.like_count
        p['quotes'] = post.quote_count
        p['replies'] = post.reply_count
        p['reposts'] = post.repost_count
        p['created_at'] = post.record.created_at

        p['rt_handle'] = "blank"
        p['rt_text'] = "blank"
        p['rt_uri'] = "blank"
        p['rt_link'] = "blank"
        
    elif hasattr(post.embed, 'record'):
        p = {}
        p['text'] = " ".join(post.record.text.split("\n")[:2])
        p['handle'] = post.author.handle
        p['uri'] = post.uri.split("feed.post/")[1]
        p['likes'] = post.like_count
        p['quotes'] = post.quote_count
        p['replies'] = post.reply_count
        p['reposts'] = post.repost_count
        p['created_at'] = post.record.created_at
        p['link'] = "blank"

        try:
            p['rt_handle'] = post.embed.record.record.author.handle
            p['rt_text'] = " ".join(post.embed.record.record.value.text.split("\n")[:2])
            p['rt_uri'] = post.embed.record.record.uri.split("feed.post/")[1]
            p['rt_link'] = post.embed.record.record.value.embed.external.uri.split("?")[0]
        except:
            p['rt_handle'] = "blank"
            p['rt_text'] = "blank"
            p['rt_uri'] = "blank"
            p['rt_link'] = "blank"
        

    else:
        p = {}
        p['text'] = " ".join(post.record.text.split("\n")[:2])
        p['handle'] = post.author.handle
        p['uri'] = post.uri.split("feed.post/")[1]
        p['likes'] = post.like_count
        p['quotes'] = post.quote_count
        p['replies'] = post.reply_count
        p['reposts'] = post.repost_count
        p['created_at'] = post.record.created_at

        p['rt_handle'] = "blank"
        p['rt_text'] = "blank"
        p['rt_uri'] = "blank"
        p['rt_link'] = "blank"
        p['link'] = "blank"

    # ppr(p)
    # tweets.append(p)

    try:
        print('writing file')
        createSocialLink(p)
    except Exception as e:
        ppr(post.record.embed)
        print(e, "\nthis\n\n")