from requests.cookies import RequestsCookieJar
import faapi
import os
from datetime import date
from datetime import datetime
from colorama import init, Fore, Back, Style
import requests
import sys
#SET FURAFFINITY USERNAME
username = ''
#SET FURAFFINITY COOKIES A AND B
cookie_a = ''
cookie_b = ''
#SET DISCORD WEBHOOK URL
webhook_url = ''
#SOME FILES MAY NEED TO BE CREATED MANUALLY, NO CONTENTS NEEDED
cookies = RequestsCookieJar()
cookies.set("a", cookie_a)
cookies.set("b", cookie_b)
api = faapi.FAAPI(cookies)
entries = []
processed_count = 0
printed_max = 0
max_views = 0
max_favs = 0
most_viewed = 'None'
most_views = 0
most_faved = 'None'
most_favs = 0
curr_time = datetime.now()
if int(curr_time.hour) < 12:
morn_aft = 'morning'
else:
morn_aft = 'evening'
fa_user = api.user(username)
fa_gallery = api.gallery(username)
#Check if current record already exists
if os.path.exists(f"records/{date.today()}_{morn_aft}.txt"):
os.remove(f"records/{date.today()}_{morn_aft}.txt")
print('Removed current record.')
#Add stories to entries list
for x in fa_gallery[0]:
entries.append(x.id)
#Print amount of users watching user
print(f'Watched by: {fa_user.stats[6]}')
#Create file and write watch count
with open(f"records/{date.today()}_{morn_aft}.txt", "at") as f:
#Write watched count at top of file
f.write(f'Watched by: {fa_user.stats[6]}\n')
f.close()
#Read contents from previous record and put into dictionary
record_dict = {}
fav_dict = {}
#Open file and read view record
with open("previous_record.txt") as rec_file:
#Read each line
for line in rec_file:
#Split line into id and number
id, views_num = map(int, line.split())
#Add to dict
record_dict[id] = views_num
#Close view record file and delete
rec_file.close()
os.remove("previous_record.txt")
#Open file and read fav record
with open("previous_fav_record.txt") as fav_file:
#Read each line
for line in fav_file:
#Split line into id and number
id, fav_num = map(int, line.split())
#Add to dict
fav_dict[id] = fav_num
#Close view record file and delete
fav_file.close()
os.remove("previous_fav_record.txt")
#For each submission
for i in entries:
#Set sub as the submission itself
sub, sub_file = api.submission(i, get_file=True)
#Set gender tags for participating characters
genders = []
if any('male' in tag.lower() for tag in sub.tags):
genders.append('Male')
if any('female' in tag.lower() for tag in sub.tags):
genders.append('Female')
#CHANGE TAGS AS WANTED
#Set color for terminal display
#If story has certain tags
if any('tag1' in tag.lower() for tag in sub.tags):
CUSTOM_COLOR = "\x1b[38;2;3;236;252m"
elif any('tag2' in tag.lower() for tag in sub.tags):
CUSTOM_COLOR = "\x1b[38;2;212;30;229m" #TRUE COLOR: 38;2 RGB values: R=212, G=30, B=229
elif any('tag3' in tag.lower() for tag in sub.tags):
CUSTOM_COLOR = "\x1b[38;2;13;123;83m"
elif any('tag4' in tag.lower() for tag in sub.tags):
CUSTOM_COLOR = "\x1b[38;2;100;100;255m"
else:
CUSTOM_COLOR = "\x1b[38;2;226;147;21m"
#Remove information past '-'
title_cut = sub.title.split('-')
#Make string of submission title, upload date, rating, and number of favourites
printed = f'{CUSTOM_COLOR}{title_cut[0]:<30}{Style.RESET_ALL} | {sub.date.strftime("%Y-%m-%d")} | {sub.rating:<7} | Views: {sub.stats[0]:04d}, Favourites: {sub.stats[2]:>2} | {genders}'
written = f'{title_cut[0]:<30} | {sub.date.strftime("%Y-%m-%d")} | {sub.rating:<7} | Views: {sub.stats[0]:03d}, Favourites: {sub.stats[2]} | {genders}'
#Add to max views and favs
max_views += sub.stats[0]
max_favs += sub.stats[2]
#Set the maximum length of a line
if printed_max < len(printed):
printed_max = len(printed)
#Make a line of underscores (at top)
if processed_count == 0:
print('_' * (len(printed) - 22))
#Print submission info
print(printed)
#Compare submission ID to find in previous run view record
if i in record_dict:
prev_views = record_dict[i]
#Subtract old views from new views
new_views = sub.stats[0] - prev_views
#Else, new story
else:
new_views = sub.stats[0]
#Compare and replace
if new_views > most_views:
most_views = new_views
most_viewed = sub.title
elif (new_views == most_views) and (most_views != 0):
most_viewed = most_viewed + ", " + sub.title
#Compare submission ID to find in previous run fav record
if i in fav_dict:
prev_favs = fav_dict[i]
#Subtract old views from new views
new_favs = sub.stats[2] - prev_favs
#Else, new story
else:
new_favs = sub.stats[2]
#Compare
if new_favs > most_favs:
most_favs = new_favs
most_faved = sub.title
elif (new_favs == most_favs) and (most_favs != 0):
most_faved = most_faved + ", " + sub.title
####################
### Write Record ###
####################
#Create file and write to it
with open(f"records/{date.today()}_{morn_aft}.txt", "at") as f:
#Write submission info
if processed_count == 0:
f.write('_' * len(written))
f.write('\n')
f.write(f'{written}\n')
#Iterate processed_count
processed_count += 1
if processed_count >= len(entries):
#Make a line of overlines (at bottom)
f.write('_' * len(written))
#Erase previous file
#Write new view file
record = f'{i} {sub.stats[0]}'
with open("previous_record.txt", "at") as g:
g.write(f'{record}\n')
#Write new view file
fav_record = f'{i} {sub.stats[2]}'
with open("previous_fav_record.txt", "at") as h:
h.write(f'{fav_record}\n')
#Make a line of overlines (at bottom)
print('‾' * (printed_max - 22))
print(f'Total Views: {max_views}\nTotal Favs: {max_favs}')
f.close()
g.close()
print(f'File closed to records/{date.today()}.txt with {processed_count}/{len(entries)} entries processed.\n')
#######################
### Discord Webhook ###
#######################
message = f'Total Views: {max_views}\nTotal Favs: {max_favs}\nWatched By: {fa_user.stats[6]}'
payload = {'content': message}
payload = {
'embeds': [
{
'title': f'{date.today()}',
'description': 'Today\'s analytics',
#OPTIONAL LINK TO FURAFFINITY PROFILE
'url': 'https://www.furaffinity.net/user/zanteri',
'color': 0xFFFF00,
'thumbnail': {
#WEBHOOK IMAGE
'url': 'https://zanteri.com/characters/vigal/images/vigal-sticker_panic.png'
},
'fields': [
{
'name': 'Total Views',
'value': max_views,
'inline': False
},
{
'name': 'Most Viewed',
'value': most_viewed,
'inline': True
},
{
'name': 'Views',
'value': most_views,
'inline': True
},
{
'name': 'Total Favourites',
'value': max_favs,
'inline': False
},
{
'name': 'Most Faved',
'value': most_faved,
'inline': True
},
{
'name': 'Favs',
'value': most_favs,
'inline': True
},
{
'name': 'Watches',
'value': fa_user.stats[6],
'inline': False
}
]
}
]
}
#Check amount of arguments
num_args = len(sys.argv)
#Check if argument to run webhook
if num_args > 1:
args = [*sys.argv[1]]
print(args)
#If present
if '-webhook':
requests.post(webhook_url, json=payload)