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
# 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(
        cid = post['link_id'],
        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']
    )
    print(created, new_post)

for post in feed.feed:
    post = post.post
    if hasattr(post.record.embed, 'external'):
        p = {}
        p['link'] = post.record.embed.external.uri.split("?")[0]
        p['text'] = " ".join(post.record.text.split("\n")[:2])
        p['handle'] = post.author.handle
        p['link_id'] = post.uri.split("feed.post/")[-1]
        p['uri'] = post.uri
        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
        
        try:
            createSocialLink(p)
        except Exception as e:
            print(e)