Advertisement
Guest User

amageddontracker.py

a guest
Jul 3rd, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. import time
  2. import sqlite3
  3. import praw
  4. import bot
  5. import datetime
  6.  
  7. sql = sqlite3.connect('sql.db')
  8. cur = sql.cursor()
  9. cur2 = sql.cursor()
  10.  
  11. cur.execute('CREATE INDEX IF NOT EXISTS amaindex ON amageddon(idint)')
  12. r = bot.rG()
  13.  
  14. DEFAULTS = [
  15. 'announcements','Art','AskReddit','askscience','aww',
  16. 'blog','books','creepy','dataisbeautiful','DIY','Documentaries',
  17. 'EarthPorn','explainlikeimfive','Fitness','food','funny',
  18. 'Futurology','gadgets','gaming','GetMotivated','gifs','history',
  19. 'IAmA','InternetIsBeautiful','Jokes','LifeProTips','listentothis',
  20. 'mildlyinteresting','movies','Music','news','nosleep','nottheonion',
  21. 'OldSchoolCool','personalfinance','philosophy','photoshopbattles',
  22. 'pics','science','Showerthoughts','space','sports','television',
  23. 'tifu','todayilearned','TwoXChromosomes','UpliftingNews','videos',
  24. 'worldnews','WritingPrompts']
  25.  
  26. SQL_COLUMNCOUNT = 10
  27. SQL_IDINT = 0
  28. SQL_IDSTR = 1
  29. SQL_CREATED = 2
  30. SQL_HUMAN = 3
  31. SQL_NAME = 4
  32. SQL_NSFW = 5
  33. SQL_SUBSCRIBERS = 6
  34. SQL_JUMBLE = 7
  35. SQL_SUBREDDIT_TYPE = 8
  36. SQL_SUBMISSION_TYPE = 9
  37.  
  38. def now():
  39.     return datetime.datetime.now(datetime.timezone.utc).timestamp()
  40.  
  41.  
  42. def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
  43.     """Converts an integer to a base36 string."""
  44.     if not isinstance(number, (int)):
  45.         raise TypeError('number must be an integer')
  46.     base36 = ''
  47.     sign = ''
  48.     if number < 0:
  49.         sign = '-'
  50.         number = -number
  51.     if 0 <= number < len(alphabet):
  52.         return sign + alphabet[number]
  53.     while number != 0:
  54.         number, i = divmod(number, len(alphabet))
  55.         base36 = alphabet[i] + base36
  56.     return sign + base36
  57.  
  58. def base36decode(number):
  59.     return int(number, 36)
  60.  
  61. def b36(i):
  62.     if type(i) == int:
  63.         return base36encode(i)
  64.     if type(i) == str:
  65.         return base36decode(i)
  66.  
  67. def get_hundred():
  68.     cur.execute('SELECT * FROM subreddits WHERE subscribers > 100 AND subreddit_type != 2 ORDER BY subscribers DESC')
  69.     while True:
  70.         hundred = [cur.fetchone() for x in range(100)]
  71.         hundred = list(filter(None, hundred))
  72.         if len(hundred) == 0:
  73.             print('Run finished.')
  74.             break
  75.         yield hundred
  76.  
  77. def human(timestamp):
  78.     day = datetime.datetime.utcfromtimestamp(timestamp)
  79.     human = datetime.datetime.strftime(day, "%d%b %H:%M")
  80.     return human
  81.  
  82. def manage():
  83.     hundredg = get_hundred()
  84.     for hundred in hundredg:
  85.         print('checking %s - %s %d' % (hundred[0][SQL_NAME], hundred[-1][SQL_NAME], hundred[-1][SQL_SUBSCRIBERS]))
  86.         subreddits = r.get_info(thing_id=['t5_' + x[SQL_IDSTR] for x in hundred])
  87.         hundred.sort(key=lambda x: x[SQL_CREATED])
  88.         subreddits.sort(key=lambda x: x.created_utc)
  89.  
  90.         for sub in subreddits:
  91.             for item in hundred:
  92.                 if item[SQL_IDSTR] == sub.id:
  93.                     sub.previous = item[SQL_SUBREDDIT_TYPE]
  94.                     sub.psubs = item[SQL_SUBSCRIBERS]
  95.  
  96.         for sub in subreddits:
  97.             if sub.previous == 2:
  98.                 continue
  99.  
  100.             idint = b36(sub.id)
  101.             cur2.execute('SELECT * FROM amageddon WHERE idint == ?', [idint])
  102.             f = cur2.fetchone()
  103.             if f:
  104.                 if sub.subreddit_type == 'private':
  105.                     cur2.execute('UPDATE amageddon SET returntime=? WHERE idint=?', [None, idint])
  106.                     continue
  107.                 if f[-1] is not None:
  108.                     continue
  109.                 returntime = int(now())
  110.                 print('     [OPEN] %s' % sub._fast_name)
  111.                 cur2.execute('UPDATE amageddon SET returntime=? WHERE idint=?', [returntime, idint])
  112.             else:
  113.                 if sub.subreddit_type != 'private':
  114.                     continue
  115.                 data = [idint, sub.id, sub._fast_name, sub.psubs, None]
  116.                 print('  [PRIVATE] %s' % sub._fast_name)
  117.                 cur2.execute('INSERT INTO amageddon VALUES(?, ?, ?, ?, ?)', data)
  118.         sql.commit()
  119.  
  120. def show():
  121.     cur2.execute('SELECT * FROM amageddon WHERE subscribers >= 75 ORDER BY subscribers DESC')
  122.     outtotal = ''
  123.     #outtotal += 'I am currently AFK, letting the bot do its work. I hope it doesn\'t crash!\n\n'
  124.     outtotal += '**https://reddit.com/r/GoldTesting/wiki/amageddon**\n\n'
  125.     outtotal += 'This is a list of subreddits which became private at some point during the AMAgeddon.\n\n'
  126.     outtotal += 'The subscriber counts are based on the last time I scanned them, which was, like, 2 days ago maybe.\n\n'
  127.     outtotal += '{down} down  \n{up} up  \n{total} total\n\n'
  128.     outtotal += 'IDSTR|NAME|SUBSCRIBERS|REINSTATED (UTC)\n'
  129.     outtotal += '-:|:-|-:|-:\n'
  130.     down = 0
  131.     up = 0
  132.     total = 0
  133.     while True:
  134.         item = cur2.fetchone()
  135.         if item is None:
  136.             break
  137.         item = list(item)
  138.         if item[-1] is None:
  139.             item[-1] = ''
  140.             down += 1
  141.         else:
  142.             item[-1] = human(int(item[-1]))
  143.             up += 1
  144.         item = item[1:]
  145.         if item[1] in DEFAULTS:
  146.             item[1] = '%s ^D' % item[1]
  147.         item[1] = 'r/' + item[1]
  148.         item[2] = '{:,}'.format(item[2])
  149.         outtotal += '|'.join(item) + '\n'
  150.         total += 1
  151.     outtotal = outtotal.format(up=up, down=down, total=total)
  152.     print(len(outtotal))
  153.     outfile = open('amageddon/outfile.txt', 'w')
  154.     outfile.write(outtotal)
  155.     outfile.close()
  156.     if len(outtotal) < 40000:
  157.         submission = r.get_info(thing_id='t3_3bypzz')
  158.         submission.edit(outtotal)
  159.     s = r.get_subreddit('goldtesting')
  160.     s.edit_wiki_page('amageddon', outtotal)
  161.     print(total)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement