<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 RedTube json Python
Step1:  Requests and json are the two main modules used for this. Random can also be handy
Step2:  Convert it into readable text that you can work with
<ASSISTANT_TASK:>
Python Code:
import requests
import json
import random
import getpass
#import couchdb
import pickle
import getpass
#!flask/bin/python
#from flask import Flask, jsonify
myusr = getpass.getuser()
print(myusr)
#couch = couchdb.Server()
with open('/home/{}/prn.pickle'.format(myusr), 'rb') as handle:
    prnlis = pickle.load(handle)
#db = couch.create('redtube')    
#db = couch['redtube']
Explanation: RedTube json Python
End of explanation
payload = {'output' : 'json', 'data' : 'redtube.Videos.searchVideos', 'page' : 1}
getprn = requests.get('http://api.redtube.com/', params = payload)
daprn = getprn.json()
levid = len(daprn['videos'])
porndick = dict()
#for lev in range(0, levid):
#    print(daprn['videos'][lev]['video'])
#    prntit = (daprn['videos'][lev]['video']['title'])
#    prnnow = prntit.replace(' ', '-')
#    prnlow = prnnow.lower()
#    print(prnlow)
#    try:
#        somelis = list()
#        for dapr in daprn['videos'][lev]['video']['tags']:
#            print(dapr['tag_name'])
#            somelis.append(dapr['tag_name'])
#            porndick.update({daprn['videos'][lev]['video']['video_id'] : {'tags' : ", ".join(str(x) for x in somelis)}})
            #db.save(porndick)
            #try:
            #    db = couch.create(prnlow)
            #except PreconditionFailed:
            #    db = couch[prnlow]
            #db.save({daprn['videos'][lev]['video']['video_id'] : {'tags' : ", ".join(str(x) for x in somelis)}})
            
#    except KeyError:
#        continue
#for i in db:
#    print(i)
#db.save(porndick)
#for i in db:
#    print(db[i])
#print(pornd['tags'])
#loaPrn = json.loads(getPrn.text)
#print loaUrl
Explanation: Requests and json are the two main modules used for this. Random can also be handy
End of explanation
lenvid = len(daprn[u'videos'])
lenvid
#aldic = dict()
with open('/home/{}/prn3.pickle'.format(myusr), 'rb') as handles:
    aldic = pickle.load(handles)
import shutil
for napn in range(0, lenvid):
    print(daprn[u'videos'][napn]['video']['url'])
    print(daprn[u'videos'][napn]['video']['title'])
    try:
        letae = len(daprn[u'videos'][napn]['video']['tags'])
        tagna = (daprn[u'videos'][napn]['video']['tags'])
        reqbru = requests.get('http://api.giphy.com/v1/gifs/translate?s={}&api_key=dc6zaTOxFJmzC'.format(tagna))
        brujsn = reqbru.json()
        print(brujsn['data']['images']['fixed_width']['url'])
        gurl = (brujsn['data']['images']['fixed_width']['url'])
        gslug = (brujsn['data']['slug'])
        #fislg = gslug.repl
        
        try:
            somelis = list()
            for dapr in daprn['videos'][lev]['video']['tags']:
                print(dapr['tag_name'])
                somelis.append(dapr['tag_name'])
                porndick.update({daprn['videos'][lev]['video']['video_id'] : {'tags' : ", ".join(str(x) for x in somelis)}})
 
        except KeyError:
            continue
        
        aldic.update({gslug : gurl})
        #print(gurl)
        '''
        with open('/home/pi/redtube/posts/{}.meta'.format(gslug), 'w') as blmet:
            blmet.write('.. title: ' + glug + ' \n' + '.. slug: ' + nameofblogpost + ' \n' + '.. date: ' + str(nowtime) + ' \n' +  '.. tags: ' + tagblog + '\n' + '.. link:\n.. description:\n.. type: text')
     
        response = requests.get(gurl, stream=True)#
        response
        with open('/home/pi/redtube/galleries/{}.gif'.format(gslug), 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
            del response
            
            tan = tagna.replace(' ', '-')
            tanq = tan.lower()
            print(tanq)
            
        '''
    except KeyError:
         continue                                                                                                                                                                                                                                                                                                                  
with open('/home/{}/prn.pickle'.format(myusr), 'wb') as handle:
    pickle.dump(porndick, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('/home/{}/prn3.pickle'.format(myusr), 'wb') as handle:
    pickle.dump(aldic, handle, protocol=pickle.HIGHEST_PROTOCOL)
#db.save(aldic)
Explanation: Convert it into readable text that you can work with
End of explanation
<END_TASK>