2022-04-18 16:25:36 +02:00
"""
Auth setup
1. Head to https : / / www . reddit . com / prefs / apps
2. Click " create another app "
3. Give it a name and description
4. Select " script "
5. Redirect to http : / / localhost : 8080
6. Create file " credentials " with the format below
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
│ [ ID ] < - Under " personal use script " │
│ [ Secret ] │
│ [ Username ] < - Must be a mod , don ' t do this if you │
│ [ Password ] < - don ' t know what you are doing. │
└ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
7. Run Script
Running Script
1. Input the next ID to use
2. Manually resolve errors in manual_atlas . json
3 a . Use merge_out . py , or . . .
b . a . Copy temp_atlas . json entries into web / _js / atlas . js ( mind the edits ! )
b . Copy read - ids - temp . txt IDs into data / read - ids . txt
5. Create a pull request
"""
2017-04-04 20:12:45 +02:00
import praw
2022-04-04 18:49:57 +02:00
import json
2022-04-05 12:55:45 +02:00
import time
2022-04-08 06:19:46 +02:00
import re
2022-04-08 01:52:00 +02:00
import traceback
2022-05-21 22:57:19 +02:00
from formatter import format_all , validate
2022-04-25 10:50:26 +02:00
from migrate_atlas_format import migrate_atlas_format
2017-04-04 20:12:45 +02:00
2022-04-18 16:21:02 +02:00
OUT_FILE = open ( ' temp_atlas.json ' , ' w ' , encoding = ' utf-8 ' )
READ_IDS_FILE = open ( ' read-ids-temp.txt ' , ' w ' )
2022-04-19 04:00:26 +02:00
FAIL_FILE = open ( ' manual_atlas.txt ' , ' w ' , encoding = ' utf-8 ' )
2022-04-18 16:21:02 +02:00
OUT_FILE_LINES = [ ' [ \n ' , ' ] \n ' ]
2017-04-04 20:12:45 +02:00
2022-04-08 05:59:06 +02:00
with open ( ' credentials ' , ' r ' ) as file :
credentials = file . readlines ( )
client_id = credentials [ 0 ] . strip ( )
client_secret = credentials [ 1 ] . strip ( )
2022-04-08 17:57:33 +02:00
username = credentials [ 2 ] . strip ( ) if len ( credentials ) > 3 else " "
password = credentials [ 3 ] . strip ( ) if len ( credentials ) > 3 else " "
2022-04-08 05:59:06 +02:00
reddit = praw . Reddit (
2022-05-21 22:57:19 +02:00
client_id = client_id ,
2022-04-08 05:59:06 +02:00
client_secret = client_secret ,
username = username ,
password = password ,
user_agent = ' atlas_bot '
)
has_write_access = not reddit . read_only
if not has_write_access :
print ( " Warning: No write access. Post flairs will not be updated. " )
time . sleep ( 5 )
2022-04-07 07:01:09 +02:00
2022-04-05 12:25:12 +02:00
existing_ids = [ ]
2022-04-18 16:21:02 +02:00
with open ( ' ../data/read-ids.txt ' , ' r ' ) as edit_ids_file :
2022-04-09 16:42:05 +02:00
for id in [ x . strip ( ) for x in edit_ids_file . readlines ( ) ] :
existing_ids . append ( id )
2022-04-08 05:59:06 +02:00
def set_flair ( submission , flair ) :
if has_write_access and submission . link_flair_text != flair :
flair_choices = submission . flair . choices ( )
flair = next ( x for x in flair_choices if x [ " flair_text_editable " ] and flair == x [ " flair_text " ] )
submission . flair . select ( flair [ " flair_template_id " ] )
2022-04-06 21:47:46 +02:00
total_all_flairs = 0
duplicate_count = 0
2022-04-08 06:19:46 +02:00
failcount = 0
successcount = 0
totalcount = 0
2022-04-05 21:57:08 +02:00
for submission in reddit . subreddit ( ' placeAtlas2 ' ) . new ( limit = 2000 ) :
2022-04-06 21:47:46 +02:00
total_all_flairs + = 1
2022-04-07 07:01:09 +02:00
2022-04-05 12:55:45 +02:00
if ( submission . id in existing_ids ) :
2022-04-08 06:19:46 +02:00
set_flair ( submission , " Processed Entry " )
2022-04-05 14:47:36 +02:00
print ( " Found first duplicate! " )
2022-04-06 21:47:46 +02:00
duplicate_count + = 1
2022-04-08 06:19:46 +02:00
if ( duplicate_count > 0 ) :
2022-04-06 21:47:46 +02:00
break
else :
continue
2022-05-21 22:57:19 +02:00
2022-04-10 12:53:26 +02:00
if submission . link_flair_text == " New Entry " or submission . link_flair_text == " Edit Entry " :
2022-04-07 07:01:09 +02:00
2022-04-05 09:39:51 +02:00
try :
2022-04-07 16:22:41 +02:00
text = submission . selftext
2022-04-08 01:52:00 +02:00
rawtext = text
2022-04-08 06:01:51 +02:00
text = text . replace ( ' \u200c ' , ' ' )
2022-04-19 03:55:37 +02:00
text = re . compile ( r " ( \ { .+ \ }) " , re . DOTALL ) . search ( text ) . group ( 0 )
2022-04-08 02:28:20 +02:00
# Test if it needs to escape the escape character. Usually happens on fancy mode.
try : json . loads ( text )
except json . JSONDecodeError : text = re . sub ( r " \\ (.) " , r " \ 1 " , text )
2022-04-08 01:52:00 +02:00
2022-04-07 07:01:09 +02:00
submission_json = json . loads ( text )
2017-04-05 02:53:39 +02:00
2022-04-07 16:22:41 +02:00
if submission_json :
2017-04-05 02:53:39 +02:00
2022-04-10 12:53:26 +02:00
if submission . link_flair_text == " Edit Entry " :
2022-04-18 16:21:02 +02:00
assert submission_json [ " id " ] != 0 , " Edit invalid because ID is tampered, it must not be 0! "
2022-04-10 12:53:26 +02:00
2022-04-19 03:55:37 +02:00
submission_json_dummy = { " id " : submission_json [ " id " ] , " edit " : submission . id }
2022-04-09 16:07:46 +02:00
else :
2022-04-10 12:53:26 +02:00
2022-04-18 16:21:02 +02:00
assert submission_json [ " id " ] == 0 , " Edit invalid because ID is tampered, it must be 0! "
2022-05-21 22:57:19 +02:00
2022-04-18 16:21:02 +02:00
submission_json_dummy = { " id " : submission . id }
2022-04-09 16:07:46 +02:00
2022-04-07 16:22:41 +02:00
for key in submission_json :
if not key in submission_json_dummy :
submission_json_dummy [ key ] = submission_json [ key ] ;
2022-05-21 22:57:19 +02:00
submission_json = format_all ( submission_json_dummy , True )
2022-05-16 15:01:07 +02:00
validation_status = validate ( submission_json )
2022-05-21 22:57:19 +02:00
2022-04-08 13:04:49 +02:00
assert validation_status < 3 , \
" Submission invalid after validation. This may be caused by not enough points on the path. "
2022-04-25 10:50:26 +02:00
submission_json = migrate_atlas_format ( submission_json )
2022-05-21 22:57:19 +02:00
2022-05-12 18:54:27 +02:00
add_comma_line = len ( OUT_FILE_LINES ) - 2
if len ( OUT_FILE_LINES [ add_comma_line ] ) > 2 :
OUT_FILE_LINES [ add_comma_line ] = OUT_FILE_LINES [ add_comma_line ] . replace ( ' \n ' , ' , \n ' )
2022-04-18 16:21:02 +02:00
OUT_FILE_LINES . insert ( len ( OUT_FILE_LINES ) - 1 , json . dumps ( submission_json , ensure_ascii = False ) + ' \n ' )
READ_IDS_FILE . write ( submission . id + ' \n ' )
2022-04-07 16:22:41 +02:00
successcount + = 1
2022-04-08 05:59:06 +02:00
set_flair ( submission , " Processed Entry " )
2022-04-07 16:22:41 +02:00
except Exception as e :
2022-04-18 16:21:02 +02:00
FAIL_FILE . write (
2022-04-19 05:47:11 +02:00
" \n \n " + " = " * 40 + " \n \n Submission ID: " +
2022-04-08 01:52:00 +02:00
submission . id + " \n \n " +
traceback . format_exc ( ) + " \n \n " +
2022-04-08 05:13:12 +02:00
" ==== RAW ==== " + " \n \n " +
rawtext + " \n \n "
" ==== CLEAN ==== " + " \n \n " +
text + " \n \n "
2022-04-08 01:52:00 +02:00
)
2022-04-07 16:22:41 +02:00
failcount + = 1
2022-04-08 05:59:06 +02:00
set_flair ( submission , " Rejected Entry " )
2022-04-07 07:01:09 +02:00
2022-04-19 03:55:37 +02:00
print ( " Wrote " + submission . id + " , submitted " + str ( round ( time . time ( ) - submission . created_utc ) ) + " seconds ago " )
2022-04-06 03:33:57 +02:00
totalcount + = 1
2022-04-04 18:49:57 +02:00
2022-04-18 16:21:02 +02:00
OUT_FILE . writelines ( OUT_FILE_LINES )
2022-04-06 21:47:46 +02:00
2022-04-19 05:47:11 +02:00
print ( f " \n \n Total all flairs: { total_all_flairs } \n Success: { successcount } / { totalcount } \n Fail: { failcount } / { totalcount } \n Please check manual_atlas.txt for failed entries to manually resolve. " )