Skip to content

Instantly share code, notes, and snippets.

@monajalal
Created October 18, 2016 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monajalal/e02792e9a5cbced301a8691b7a62836f to your computer and use it in GitHub Desktop.
Save monajalal/e02792e9a5cbced301a8691b7a62836f to your computer and use it in GitHub Desktop.
from imgurpython import ImgurClient
import inspect
import random
import urllib2
import requests
from imgurpython.helpers.error import ImgurClientError
import time
import socket
client_id = ""
client_secret = ""
access_token = ""
refresh_token = ""
image_type = ['jpg', 'jpeg', 'png', 'gif']
imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
timeTillUserReset = imgur_client.credits['UserReset']#Grabs the time the user quota will reset in Unix Epoch
timeString = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(timeTillUserReset)))#Formats the Unix Epoch to something readable
print ('User Limit: ' + str(imgur_client.credits['UserLimit']))#Print the Total credits that can be allocated.
print ('User Remaining: ' + str(imgur_client.credits['UserRemaining']))#Print Total credits available.
print ('User Reset: ' + str(timeString)) #Timestamp for when the credits will be reset.
print ('User Client Limit: ' + str(imgur_client.credits['ClientLimit'])) #Total credits that can be allocated for the application in a day.
print ('User Client Remaining: ' + str(imgur_client.credits['ClientRemaining'])) #Total credits remaining for the application in a day.
item_count = 0
#p=0
#for p in range(1, 1000):
#for p in range(100, -1, -1):
p=0
while True:
p += 1
time.sleep(8)
try:
search = imgur_client.gallery_search('horse', window='all', sort='viral', page=p)
for i in range(0, len(search)):
item_count +=1
print(search[i].comment_count)
if not search[i].is_album:
print(search[i].type)
print('{0}: {1}'.format("page number is", p))
if search[i].comment_count > 10 and not search[i].is_album:
if search[i].type[6:] in image_type:
count = 0
try:
image_file = urllib2.urlopen(search[i].link, timeout = 5)
image_file_name = 'images/'+ search[i].id+'.'+search[i].type[6:]
output_image = open(image_file_name, 'wb')
output_image.write(image_file.read())
for post in imgur_client.gallery_item_comments(search[i].id, sort='best'):
if count <= 10:
count += 1
output_image.close()
except urllib2.URLError as e:
print(e)
continue
except socket.timeout as e:
print(e)
continue
except socket.error as e:
print(e.status_code)
print(e.error_code)
continue
except ImgurClientError as e:
print(e.status_code)
print(e.error_message)
print item_count
'''
for item in imgur_client.gallery_item_comments("NROSx", sort='best'):
if count <= 30:
print item.comment
count += 1
'''
#gallery_image = imgur_client.gallery_item_comments('S1jmapR')
#author = gallery_image.author
#print(dir(gallery_method))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment