rgdsnatch: script to get post data from RedditGetsDrawn and post to artcontroldrawsyou


In [1]:
import os
import random
import requests
import re
import json
import praw

In [2]:
os.chdir('/home/wcmckee/rgd')

In [3]:
r = praw.Reddit(user_agent='rgdsnatch')


Version 2.1.16 of praw is outdated. Version 2.1.17 was released 10 hours ago.

In [10]:
r.login('artcontrol', 'zipit123')

In [11]:
usrd = r.get_my_subreddits()

In [12]:
suls = []

In [13]:
for subs in usrd:
    print subs
    suls.append(subs)


todayilearned
blog
science
IAmA
videos
technology
bestof
askscience
books
television
food
photoshopbattles
GetMotivated
trees
programming
listentothis
Documentaries
Art
Android
gameofthrones
Minecraft
comics
skyrim
breakingbad
buildapc

In [14]:
suls


Out[14]:
[Subreddit(display_name='todayilearned'),
 Subreddit(display_name='blog'),
 Subreddit(display_name='science'),
 Subreddit(display_name='IAmA'),
 Subreddit(display_name='videos'),
 Subreddit(display_name='technology'),
 Subreddit(display_name='bestof'),
 Subreddit(display_name='askscience'),
 Subreddit(display_name='books'),
 Subreddit(display_name='television'),
 Subreddit(display_name='food'),
 Subreddit(display_name='photoshopbattles'),
 Subreddit(display_name='GetMotivated'),
 Subreddit(display_name='trees'),
 Subreddit(display_name='programming'),
 Subreddit(display_name='listentothis'),
 Subreddit(display_name='Documentaries'),
 Subreddit(display_name='Art'),
 Subreddit(display_name='Android'),
 Subreddit(display_name='gameofthrones'),
 Subreddit(display_name='Minecraft'),
 Subreddit(display_name='comics'),
 Subreddit(display_name='skyrim'),
 Subreddit(display_name='breakingbad'),
 Subreddit(display_name='buildapc')]

In [15]:
lesuls = len(suls)

ransuz = random.randint(0, lesuls)

thesubraz = suls[ransuz]

In [16]:
suls


Out[16]:
[Subreddit(display_name='todayilearned'),
 Subreddit(display_name='blog'),
 Subreddit(display_name='science'),
 Subreddit(display_name='IAmA'),
 Subreddit(display_name='videos'),
 Subreddit(display_name='technology'),
 Subreddit(display_name='bestof'),
 Subreddit(display_name='askscience'),
 Subreddit(display_name='books'),
 Subreddit(display_name='television'),
 Subreddit(display_name='food'),
 Subreddit(display_name='photoshopbattles'),
 Subreddit(display_name='GetMotivated'),
 Subreddit(display_name='trees'),
 Subreddit(display_name='programming'),
 Subreddit(display_name='listentothis'),
 Subreddit(display_name='Documentaries'),
 Subreddit(display_name='Art'),
 Subreddit(display_name='Android'),
 Subreddit(display_name='gameofthrones'),
 Subreddit(display_name='Minecraft'),
 Subreddit(display_name='comics'),
 Subreddit(display_name='skyrim'),
 Subreddit(display_name='breakingbad'),
 Subreddit(display_name='buildapc')]

In [17]:
print thesubraz


Android

In [18]:
rd = r.get_subreddit('gonemild')

In [19]:
rdnewz = rd.get_new()

In [20]:
rdnew = []

In [21]:
rdnew


Out[21]:
[]

In [22]:
for uz in rdnewz:
    #print uz
    rdnew.append(uz)

In [23]:
rdnew


Out[23]:
[<praw.objects.Submission at 0x2abea90>,
 <praw.objects.Submission at 0x2abec90>,
 <praw.objects.Submission at 0x2abed90>,
 <praw.objects.Submission at 0x2abef10>,
 <praw.objects.Submission at 0x2abec10>,
 <praw.objects.Submission at 0x2b7a110>,
 <praw.objects.Submission at 0x2b7a210>,
 <praw.objects.Submission at 0x2b7a310>,
 <praw.objects.Submission at 0x2b7a450>,
 <praw.objects.Submission at 0x2b7a550>,
 <praw.objects.Submission at 0x2b7a650>,
 <praw.objects.Submission at 0x2b7a750>,
 <praw.objects.Submission at 0x2b7a850>,
 <praw.objects.Submission at 0x2b7a990>,
 <praw.objects.Submission at 0x2b7aa90>,
 <praw.objects.Submission at 0x2b7ab90>,
 <praw.objects.Submission at 0x2b7ac90>,
 <praw.objects.Submission at 0x2b7add0>,
 <praw.objects.Submission at 0x2b7aed0>,
 <praw.objects.Submission at 0x2b7afd0>,
 <praw.objects.Submission at 0x2b87190>,
 <praw.objects.Submission at 0x2b87290>,
 <praw.objects.Submission at 0x2b87390>,
 <praw.objects.Submission at 0x2b87490>,
 <praw.objects.Submission at 0x2b875d0>]

In [24]:
ransubz = random.randint(0,24)

In [25]:
print ransubz


5

In [26]:
ransev = rdnew[ransubz]

In [27]:
rgdautoz = str(ransev.author)

In [28]:
rgdsubred = str(ransev.subreddit)

In [29]:
sutit = ('[' + rgdsubred + ']')

In [30]:
sutit


Out[30]:
'[redditgetsdrawn]'

In [31]:
rgdaqwew = ('[RGD]' + rgdautoz)

In [32]:
rgdaturo = str(ransev.url)

In [33]:
rgdatit = str(ransev.title)

In [34]:
rgdatit


Out[34]:
'Could you draw me as Maleficent?'

In [35]:
#rd.get_top

In [36]:
linkdict = {}

In [37]:
ophtml = open('index.html', 'w')

In [37]:


In [38]:
ady = r.get_subreddit('artcontroldrawsyou')

In [39]:
comrgd =  rgdatit + ' ' + rgdaturo

In [40]:
comrgd


Out[40]:
'Could you draw me as Maleficent? http://i.imgur.com/qOo5M9V.jpg?1'

In [41]:
import time

In [42]:
loctime = time.localtime()

In [43]:
loctime.tm_mday


Out[43]:
19

In [44]:
loctime.tm_mon


Out[44]:
6

In [45]:
thedat = (str(loctime.tm_mday) + '/' + str(loctime.tm_mon))

In [46]:
thedat


Out[46]:
'19/6'

In [47]:
mixtut = (thedat + sutit + rgdautoz)

In [48]:
loctime


Out[48]:
time.struct_time(tm_year=2014, tm_mon=6, tm_mday=19, tm_hour=8, tm_min=17, tm_sec=21, tm_wday=3, tm_yday=170, tm_isdst=0)

In [49]:
ady.submit(mixtut , (rgdaqwew, comrgd))


Out[49]:
<praw.objects.Submission at 0x29e0cd0>
for newa in rdnew: #rint newa.url print len(newa) htmstr = (str(newa.title) + '') #ophtml.write(htmstr) #ady.submit(('[RGD]' + newa.author), newa.url) print newa.author #print newa.media ophtml.write(htmstr) print newa.selftext print newa.title print newa.url print newa.num_comments linkdict.update({str(newa.author): str(newa.url)})

In [50]:
#print str(newa.title)

In [51]:
import json

In [52]:
newzjson = json.dumps(linkdict)

In [53]:
#newzjson

In [53]:


In [54]:
rmine = r.get_redditor('itwillbemine')

In [55]:
#opest = open('userurl.json', 'r')
#opest.read()
#opest.close()

In [56]:
mincom = rmine.get_comments()

In [57]:
#print mincom

In [58]:
minels = []

In [59]:
dausr = {}

In [60]:
for newa in rdnew:
    #rint newa.url
    #print newa.author
    linkdict.update({str(newa.author): str(newa.url)})

In [61]:
for con in mincom:
    #print con.body
    minels.append(con)
    dausr.update({str(con.id): str(con.body)})


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-61-1b2775fea6e3> in <module>()
----> 1 for con in mincom:
      2     #print con.body
      3     minels.append(con)
      4     dausr.update({str(con.id): str(con.body)})

/usr/local/lib/python2.7/dist-packages/praw/__init__.pyc in get_content(self, url, params, limit, place_holder, root_field, thing_field, after_field, _use_oauth, object_filter)
    469                 self._use_oauth = _use_oauth  # pylint: disable-msg=W0201
    470             try:
--> 471                 page_data = self.request_json(url, params=params)
    472                 if object_filter:
    473                     page_data = page_data[object_filter]

/usr/local/lib/python2.7/dist-packages/praw/decorators.pyc in wrapped(reddit_session, *args, **kwargs)
    159     @wraps(function)
    160     def wrapped(reddit_session, *args, **kwargs):
--> 161         return_value = function(reddit_session, *args, **kwargs)
    162         if isinstance(return_value, dict):
    163             if return_value.get('error') == 304:  # Not modified exception

/usr/local/lib/python2.7/dist-packages/praw/__init__.pyc in request_json(self, url, params, data, as_objects, retry_on_error)
    514         # Request url just needs to be available for the objecter to use
    515         self._request_url = url  # pylint: disable-msg=W0201
--> 516         data = json.loads(response, object_hook=hook)
    517         delattr(self, '_request_url')
    518         # Update the modhash

/usr/lib/python2.7/json/__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    337     if parse_constant is not None:
    338         kw['parse_constant'] = parse_constant
--> 339     return cls(encoding=encoding, **kw).decode(s)

/usr/lib/python2.7/json/decoder.pyc in decode(self, s, _w)
    363 
    364         """
--> 365         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    366         end = _w(s, end).end()
    367         if end != len(s):

/usr/lib/python2.7/json/decoder.pyc in raw_decode(self, s, idx)
    379         """
    380         try:
--> 381             obj, end = self.scan_once(s, idx)
    382         except StopIteration:
    383             raise ValueError("No JSON object could be decoded")

/usr/local/lib/python2.7/dist-packages/praw/__init__.pyc in _json_reddit_objecter(self, json_data)
    401                 return json_data['json']
    402         else:
--> 403             return object_class.from_api_response(self, json_data['data'])
    404         return json_data
    405 

/usr/local/lib/python2.7/dist-packages/praw/objects.pyc in from_api_response(cls, reddit_session, json_dict)
     56             page = parts[6].split('.', 1)[0]
     57             return cls(reddit_session, subreddit, page, json_dict=json_dict)
---> 58         return cls(reddit_session, json_dict=json_dict)
     59 
     60     def __init__(self, reddit_session, json_dict=None, fetch=True,

/usr/local/lib/python2.7/dist-packages/praw/objects.pyc in __init__(self, reddit_session, json_dict)
    512     def __init__(self, reddit_session, json_dict):
    513         super(Comment, self).__init__(reddit_session, json_dict,
--> 514                                       underscore_names=['replies'])
    515         if self._replies:
    516             self._replies = self._replies['data']['children']

/usr/local/lib/python2.7/dist-packages/praw/objects.pyc in __init__(self, reddit_session, json_dict, fetch, info_url, underscore_names)
     70         self.reddit_session = reddit_session
     71         self._underscore_names = underscore_names
---> 72         self.has_fetched = self._populate(json_dict, fetch)
     73         self.json_dict = (json_dict if reddit_session.config.store_json_result
     74                           is True else None)

/usr/local/lib/python2.7/dist-packages/praw/objects.pyc in _populate(self, json_dict, fetch)
    139             if self._underscore_names and name in self._underscore_names:
    140                 name = '_' + name
--> 141             setattr(self, name, value)
    142         return bool(json_dict) or fetch
    143 

/usr/local/lib/python2.7/dist-packages/praw/objects.pyc in __setattr__(self, name, value)
    100             else:
    101                 value = Redditor(self.reddit_session, value, fetch=False)
--> 102         object.__setattr__(self, name, value)
    103 
    104     def __str__(self):

AttributeError: can't set attribute

In [ ]:
minels

In [62]:
noizjson = json.dumps(dausr)

In [63]:
newposts = open('userurl.json', 'a')
newposts.write(newzjson)
print ('file userurl.json updated')
newcomments = open('idcomt.json', 'a')
newcomments.write(noizjson)
print ('user comments updated')
newposts.close()
newcomments.close()


file userurl.json updated
user comments updated

In [64]:
rdusr = str(con.author)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-64-80f24cbaa657> in <module>()
----> 1 rdusr = str(con.author)

NameError: name 'con' is not defined

In [65]:
minelsz = []

In [65]:


In [66]:
#for mina in minels:
    #print mina.body
   # minelsz.append(mina.body)

In [67]:
#minelsz

In [68]:
#mina.body

In [68]:


In [68]:


In [ ]: