Exemplo n.º 1
0
class uploader():

  def __init__(self):
    self.client_id = os.environ.get('Client_ID')
    self.client_secret = os.environ.get('Client_Secret')
    self.access_token = os.environ.get('access_token')
    self.refresh_token = os.environ.get('refresh_token')
    self.album_id = os.environ.get('Album_ID')
    self.mashape_key = os.environ.get('Mashape_key')
    # self.client_id = config['imgur_api']['Client_ID']
    # self.client_secret = config['imgur_api']['Client_Secret']
    # self.access_token = config['imgur_api']['access_token']
    # self.refresh_token = config['imgur_api']['refresh_token']
    # self.album_id = config['imgur_api']['Album_ID']
    # self.mashape_key = config['imgur_api']['Mashape_key']
    # self.client = ImgurClient(self.client_id, self.client_secret, self.access_token, self.refresh_token)
    self.client = ImgurClient(self.client_id, self.client_secret, self.access_token, self.refresh_token, self.mashape_key)

    
    self.logger = None

  def exelogging(self, msg):
    if self.logger:
      self.logger.debug(msg)
    else:
      print(msg)

  def upload_photo(self, image_url, album, name):

    config = {
      'album': album,
      'name': name,
    }
    self.exelogging(config)
    self.exelogging("Uploading image... ")
    try:  
      if not self.mashape_key :
        self.client.upload_from_url(image_url, config=config, anon=False)
      else:
        #### imgurpython is no longer support, 
        #### when mashape_key not null, use request directly
        header = {
          'Authorization' : 'Bearer {}'.format(self.access_token),
          'X-RapidAPI-Key' : self.mashape_key,
        }
        config["type"] = "url"
        config["image"] = image_url
        url = "https://imgur-apiv3.p.rapidapi.com/3/image"
        response = requests.post(url, headers=header, data=config)
        print("url={},headers={},data={}".format(url, header, config))
        print("status_code={}".format(response.status_code))
    except Exception as e:
      self.exelogging(e)
      return False

    self.exelogging("Done")

    return True
Exemplo n.º 2
0
def upload_image(imgur_client_id,
                 imgur_client_secret,
                 app_name,
                 image_url=None,
                 image_file_path=None):
    """Uploads an image to Imgur by the image's url or file_path. Returns the
    Imgur api response."""
    if image_url is None and image_file_path is None:
        raise ValueError('Either image_url or image_file_path must be '
                         'supplied.')
    client = ImgurClient(imgur_client_id, imgur_client_secret)
    title = '{} Image Upload'.format(current_app.config['APP_NAME'])

    description = 'This is part of an idling vehicle report on {}.'.format(
        current_app.config['APP_NAME'])

    if image_url is not None:
        result = client.upload_from_url(url=image_url,
                                        config={
                                            'title': title,
                                            'description': description,
                                        })
    else:
        result = client.upload_from_path(path=image_file_path,
                                         config={
                                             'title': title,
                                             'description': description,
                                         })
        os.remove(image_file_path)

    return result['link'], result['deletehash']
Exemplo n.º 3
0
def uploadToImgur(userid,url):
	db = app.connect_db()
	c = db.cursor()
	c.execute('SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url='+app.app.sqlesc,(url,))
	result = c.fetchone()
	if result[4] != None:
		previous_upload_properties = json.loads(result[4])
		if time.time() < previous_upload_properties['upload_time']+(2*3600):
			return {'error':'too_soon','link':previous_upload_properties['imgur_url']}
	map_url = result[0]
	titlestring = u"{} Farm, {} by {}".format(result[2],result[3],result[1])
	descriptionstring = u"Stardew Valley game progress, full summary at http://upload.farm/{}".format(url)
	# try:
	c.execute('SELECT imgur_json FROM users WHERE id='+app.app.sqlesc,(userid,))
	r = json.loads(c.fetchone()[0])
	access_token = r['access_token']
	refresh_token = r['refresh_token']
	client = ImgurClient(app.app.config['IMGUR_CLIENTID'],app.app.config['IMGUR_SECRET'])
	client.set_user_auth(access_token,refresh_token)
	# file = url_for('home',filename=map_url,_external=True)
	# print 'uploaded to',file
	# client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
	if app.app.config['IMGUR_DIRECT_UPLOAD'] == True:
		result = client.upload_from_path(map_url,config={'title':titlestring,'description':descriptionstring},anon=False)
	else:
		map_url = u"http://upload.farm/{}".format(map_url)
		result = client.upload_from_url(map_url,config={'title':titlestring,'description':descriptionstring},anon=False)
	print(result)
	imgur_json = json.dumps({'imgur_url':'http://imgur.com/'+result['id'],'upload_time':time.time()})
	c.execute('UPDATE playerinfo SET imgur_json='+app.app.sqlesc+' WHERE url='+app.app.sqlesc,(imgur_json,url))
	db.commit()
	try:
		return {'success':None,'link':result['link']}
	except:
		return {'error':'upload_issue','link':None}
Exemplo n.º 4
0
def upload_img_from_url(url):
    imgur_api = ImgurClient(app.config['CLIENT_ID'],
                            app.config['CLIENT_SECRET'])
    resp = imgur_api.upload_from_url(url)
    resp['link'] = normalize_url(resp['link'])

    return {u'link': resp['link']}
Exemplo n.º 5
0
class uploader():
    def __init__(self):
        self.client_id = os.environ.get('Client_ID')
        self.client_secret = os.environ.get('Client_Secret')
        self.access_token = os.environ.get('access_token')
        self.refresh_token = os.environ.get('refresh_token')
        self.client = ImgurClient(self.client_id, self.client_secret,
                                  self.access_token, self.refresh_token)

    def upload_photo(self, image_url, album):

        config = {
            'album': album,
        }
        print(config)
        print("Uploading image... ")
        try:
            image = self.client.upload_from_url(image_url,
                                                config=config,
                                                anon=False)
        except Exception as e:
            print(e)
            return True

        print("Done")

        return False
Exemplo n.º 6
0
def run():
    client = ImgurClient(os.environ['IMGUR_CLIENT_ID'], os.environ['IMGUR_CLIENT_SECRET'], os.environ['IMGUR_ACCESS_TOKEN'], os.environ['IMGUR_REFRESH_TOKEN'])

    url = "http://www.elztalflieger1.de/webcamsp/images/current.jpg"
    try:
        client.upload_from_url(url, config={'album': '1EN5T'}, anon=False)
        print("Uploaded " + url)
    except ImgurClientError as e:
        print(e.error_message)

    url = current_image_url(2)
    try:
        client.upload_from_url(url, config={'album': 'BN5AE'}, anon=False)
        print("Uploaded " + url)
    except ImgurClientError as e:
        print(url + ' ' + e.error_message)
Exemplo n.º 7
0
async def buildEmbed(msg, url, tweet='', author=''):
    if url != "":
        if cfg["config"]["cache"] == True:
            try:
                url2 = url
                if not any(ext in url
                           for ext in ['.mp4', ".mov", ".webm", ".webp"]):
                    client = ImgurClient(cfg["config"]["imgur_usr"],
                                         cfg["config"]["imgur_scr"])
                    url = client.upload_from_url(url, anon=True)["link"]
            except:
                url = url2
                pass

    embed = discord.Embed()
    if len(tweet):
        embed.add_field(name='Tweet/Embed Content', value=tweet, inline=False)
    elif isinstance(msg, discord.Message) and len(msg.content):
        embed.add_field(name='Content', value=msg.content, inline=False)
    embed.add_field(name='Message Link',
                    value='https://discordapp.com/channels/{}/{}/{}'.format(
                        msg.guild.id, msg.channel.id, msg.id),
                    inline=False)
    if len(author):
        auth = msg.guild.get_member_named(author).mention
    else:
        auth = msg.author.mention
    embed.add_field(name='Author', value=auth, inline=True)
    embed.add_field(name='Channel', value=msg.channel.mention, inline=True)
    embed.set_image(url=url)

    await bot.get_channel(cfg[str(msg.guild.id)]['bot']['archive_channel']
                          ).send(embed=embed)
Exemplo n.º 8
0
class ImgurReuploader:
	def __init__(self, config_address = 'imgur.ini'):
		self.images_address = 'images.json'
		config = configparser.ConfigParser()
		config.read(config_address)
		self.client = ImgurClient(config["imgur"]["client_id"], config["imgur"]["client_secret"], config["imgur"]["access_token"], config["imgur"]["refresh_token"])
		self.uploads = {}
		self.load_from_json()

	def upload(self, url):
		if url in self.uploads:
			return self.uploads[url]
		final_url = requests.get(url).url
		rehost = self.client.upload_from_url(final_url)
		self.uploads[url] = rehost['link']
		return rehost['link']

	def save_to_json(self):
		with open(self.images_address, 'w') as json_file:
			json.dump(self.uploads, json_file)

	def load_from_json(self):
		if os.path.isfile(self.images_address):
			with open(self.images_address) as json_file:
				self.uploads = json.load(json_file)
Exemplo n.º 9
0
def uploadToImgur(userid, url):
    db = app.connect_db()
    c = db.cursor()
    c.execute(
        'SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url='
        + app.app.sqlesc, (url, ))
    result = c.fetchone()
    if result[4] != None:
        previous_upload_properties = json.loads(result[4])
        if time.time() < previous_upload_properties['upload_time'] + (2 *
                                                                      3600):
            return {
                'error': 'too_soon',
                'link': previous_upload_properties['imgur_url']
            }
    map_url = result[0]
    titlestring = u"{} Farm, {} by {}".format(result[2], result[3], result[1])
    descriptionstring = u"Stardew Valley game progress, full summary at http://upload.farm/{}".format(
        url)
    # try:
    c.execute('SELECT imgur_json FROM users WHERE id=' + app.app.sqlesc,
              (userid, ))
    r = json.loads(c.fetchone()[0])
    access_token = r['access_token']
    refresh_token = r['refresh_token']
    client = ImgurClient(app.app.config['IMGUR_CLIENTID'],
                         app.app.config['IMGUR_SECRET'])
    client.set_user_auth(access_token, refresh_token)
    # file = url_for('home',filename=map_url,_external=True)
    # print 'uploaded to',file
    # client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
    if app.app.config['IMGUR_DIRECT_UPLOAD'] == True:
        result = client.upload_from_path(map_url,
                                         config={
                                             'title': titlestring,
                                             'description': descriptionstring
                                         },
                                         anon=False)
    else:
        map_url = u"http://upload.farm/{}".format(map_url)
        result = client.upload_from_url(map_url,
                                        config={
                                            'title': titlestring,
                                            'description': descriptionstring
                                        },
                                        anon=False)
    print(result)
    imgur_json = json.dumps({
        'imgur_url': 'http://imgur.com/' + result['id'],
        'upload_time': time.time()
    })
    c.execute(
        'UPDATE playerinfo SET imgur_json=' + app.app.sqlesc + ' WHERE url=' +
        app.app.sqlesc, (imgur_json, url))
    db.commit()
    try:
        return {'success': None, 'link': result['link']}
    except:
        return {'error': 'upload_issue', 'link': None}
class ImgurBot:
    def __init__(self):
        self.client = None

        self.authorized = False
        if (req_info.needToAddInfo(useImgur=True)):
            print(
                "Need to update required_info.py with correct credentials before running, see README for more info"
            )
        super().__init__()

    def authenticate(self):
        self.client = ImgurClient(req_info.client_id_imgur,
                                  req_info.client_secret_imgur)
        authorization_url = self.client.get_auth_url('pin')
        print("Go to the following URL: {0}".format(authorization_url))
        # Read in the pin, handle Python 2 or 3 here.
        pin = get_input("Enter pin code: ")
        # ... redirect user to `authorization_url`, obtain pin (or code or token) ...
        credentials = self.client.authorize(pin, 'pin')
        self.client.set_user_auth(credentials['access_token'],
                                  credentials['refresh_token'])
        print("Authentication successful! Here are the details:")
        print("   Access token:  {0}".format(credentials['access_token']))
        print("   Refresh token: {0}".format(credentials['refresh_token']))
        self.authorized = True

    def getalbums(self):
        albums = self.client.get_account_albums(req_info.imgur_account, page=0)
        print("Done")
        print(albums)

    def upload_to_album(self, albumId, title, description, image_url):
        config = {
            'album': albumId,
            'name': title,
            'title': title,
            'description': description
        }
        self.client.upload_from_url(image_url, config=config, anon=False)
        pass
Exemplo n.º 11
0
 def upload(self, url):
     imagecloud = ImageCloud().get_by_id('1')
     logging.info('Image cloud: '+str(imagecloud))
     try:                
         client = ImgurClient(imagecloud.client_id, imagecloud.client_secret)
         client.set_user_auth(imagecloud.access_token, imagecloud.refresh_token)
         conf = {"album" : "KHz1y"}
                
         resp = client.upload_from_url(url, config=conf, anon=False)
         logging.info('Resp imgur: '+str(resp))
         return resp['link']
     except Exception as e:
         logging.info("Erro imgur: "+str(e))
         raise ErrorUploadImage      
Exemplo n.º 12
0
class Imgur():

    def __init__(self):
        self.imgur_client = ImgurClient(IMGUR_ID, IMGUR_SECRET)
        
    def save_from_url(self, image_url):
        logging.debug("trying to save %s" % image_url)
        response = self.imgur_client.upload_from_url(clean_path(image_url))
        return str(response['link'])

    def save_from_file(self, file_path):
        logging.debug("trying to save file %s" % file_path)
        response = self.imgur_client.upload_from_path(file_path)
        return (str(response['link']))
Exemplo n.º 13
0
def upload_photo(image_url):
    client_id = '08dd33b004aeb70'
    client_secret = 'df0e8f73218d6046b49cf3d481f898ff658868fa'
    access_token = '714846b6f07dddaea1b9144a0b5fdc1e49c2cc93'
    refresh_token = '714846b6f07dddaea1b9144a0b5fdc1e49c2cc93'
    client = ImgurClient(client_id, client_secret, access_token, refresh_token)
    album = None  # You can also enter an album ID here
    config = {
        'album': album,
    }
    print("Uploading image... ")
    image = client.upload_from_url(image_url, config=config, anon=False)
    print("Done")
    return image['link']
Exemplo n.º 14
0
def reupload_image_to_imgur(url):
    try:
        if 'jpg' in url:
            client = ImgurClient(IMGUR_CLIENT_ID, IMGUR_CLIENT_SECRET)
            # print(client.credits)
            time.sleep(3)  # Be nice to Imgur, we're a bot in no rush.
            print("Uploading image")
            item = client.upload_from_url(url)
            return item['link']
        else:
            return url
    except Exception as e:
        print(f"error in reupload_image_to_imgur() : {e}")
        return url
Exemplo n.º 15
0
def upload_photo(image_url):
    client_id = '9efb779cd512a75'
    client_secret = 'f0c60f6d82e3b9b2d33a7f81318ac950ee424aa4'
    access_token = '3827abdb987541f4e5d7831cfa91f64b838d6fcf'
    refresh_token = '1df2672c250d33d5823ddf7d7a1d34ffbca0b2fc'
    client = ImgurClient(client_id, client_secret, access_token, refresh_token)
    album = None  # You can also enter an album ID here
    config = {
        'album': album,
    }

    print("Uploading image... ")
    image = client.upload_from_url(image_url, config=config, anon=False)
    print("Done")
    return image['link']
Exemplo n.º 16
0
def upload_photo(image_url):
    client_id = 'happysorry34'
    client_secret = 'jeff81397'
    access_token = 'your token'
    refresh_token = 'your token'
    client = ImgurClient(client_id, client_secret, access_token, refresh_token)
    album = None  # You can also enter an album ID here
    config = {
        'album': album,
    }

    print("Uploading image... ")
    image = client.upload_from_url(image_url, config=config, anon=False)
    print("Done")
    return image['link']
Exemplo n.º 17
0
    def upload(self, client: ImgurClient) -> list[int]:
        """Upload images to Imgur

        :param client: imgur api client
        :type client: ImgurClient
        :return: list of uplaoded image ids
        :rtype: list[int]
        """
        if self.image_id is None:
            logging.info('Uploading image to album "%s"', self.deletehash)
            print(self.status["twitter"]["media"], self.gen_config())
            ret = client.upload_from_url(
                self.status["twitter"]["media"], config=self.gen_config(), anon=False
            )
            self.image_id = ret["id"]
        else:
            logging.info(
                'Image already uploaded to album "%s" with id "%s"',
                self.deletehash,
                self.image_id,
            )
        return self.image_id
Exemplo n.º 18
0
def upload_photo(image_url, image_name):
	client_id = 'YOUR_CLIENT_ID'
	client_secret = 'YOUR_CLIENT_SECRET'
	access_token = 'YOUR_ACCESS_TOKEN'
	refresh_token = 'YOUR_REFRESH_TOKEN'
	client = ImgurClient(client_id, client_secret, access_token, refresh_token)
	album_id = 'YOUR_ALBUM_ID'
	album = client.get_album(album_id)
	# check whether image have been upload ever 
	for image in album.images:
		if image['name'] == image_name:
			print('Find image in Album {}!'.format(album_id))
			return image['link']

	# set configuration
	config = {
		'album': album_id,
		'name': image_name
	}
	print('Uploading image to imgur.com ... ...')
	image = client.upload_from_url(image_url, config=config, anon=False)
	print('Upload Done~')
	return image['link']
Exemplo n.º 19
0
class Author:
    """Class that represents an author inside some underhood account, tweets and has even few API calls when needed."""

    underhood: str
    twitter_token: InitVar[str]
    name: str = ""
    username: str = ""
    has_twitter: bool = True
    avatar: str = ""
    first_id: InitVar[int] = 0
    until_id: InitVar[int] = 0
    limit: int = 0

    def __post_init__(self,
                      twitter_token: str,
                      first_id: int = 0,
                      until_id: int = 0):
        self.client = Client(twitter_token)
        self.user = self.client.get_user(
            username=self.underhood,
            expansions="pinned_tweet_id",
            user_fields=["profile_image_url", "description"])
        first_id = first_id or self.user.data.pinned_tweet_id
        _first_tweet = self.get_tweet(first_id)

        all_tweets = [_first_tweet]
        for response in reversed(
                list(
                    Paginator(
                        self.client.get_users_tweets,
                        id=self.user.data.id,
                        since_id=first_id,
                        until_id=until_id or None,
                        expansions=EXPANSIONS,
                        tweet_fields=TWEET_FIELDS,
                        media_fields=MEDIA_FIELDS,
                    ))):
            for t in response.data or []:
                if t.attachments:
                    if t.attachments.get("media_keys"):
                        t.attachments["media"] = [  # v2 api me no like it
                            m.get("url") or m.get("preview_image_url")
                            for m in response.includes.get("media", [])
                            for mkey in t.attachments.get("media_keys", [])
                            if m["media_key"] == mkey
                        ]
                    if t.attachments.get("poll_ids"):
                        t.attachments["polls"] = [
                            p.get("options")
                            for p in response.includes.get("polls", [])
                            if p.get("id") == t.attachments.get(
                                "poll_ids", [0])[0]  # TODO: refactor this
                        ]
                all_tweets.append(t)
        self.timeline = []
        self.conversations: defaultdict[
            int, list[UnderhoodTweet]] = defaultdict(list)
        for t in sorted(all_tweets, key=lambda k: k.id):
            if not t.text.startswith(("@", "RT @")):
                tweet = UnderhoodTweet(
                    tweet=t,
                    quoted=
                    (  # Twitter V2 API :( or I don't know the best way to do this
                        self.get_tweet(t.referenced_tweets[0].id)
                        if t.referenced_tweets
                        and t.referenced_tweets[0].type == "quoted" else None),
                )
                self.timeline.append(tweet)
                self.conversations[tweet.conversation_id].append(tweet)
                if self.limit and len(self.timeline) >= self.limit:
                    break
        if not self.username:
            self.username = self.extract_username() or self.author_hash
        if not self.name:
            self.name = self.extract_name()
        if not self.avatar:
            self.imgur_client = ImgurClient(environ.get("IMGUR_API_ID"),
                                            environ.get("IMGUR_API_SECRET"))
            response = self.imgur_client.upload_from_url(
                self.user.data.profile_image_url.replace("_normal", ""),
                anon=True)
            self.avatar = response["link"]

    def get_tweet(self, tweet_id: int) -> Tweet:
        """Get tweet from Twitter API by id."""
        return self.client.get_tweet(id=tweet_id,
                                     expansions=EXPANSIONS,
                                     tweet_fields=TWEET_FIELDS,
                                     media_fields=MEDIA_FIELDS).data

    @property
    def author_hash(self) -> str:
        """Internal first author tweet hash needed for different purposes."""
        self.has_twitter = False
        return hashlib.sha256(self.first_tweet.text.encode()).hexdigest()[:7]

    @property
    def description(self) -> str:
        """Profile description property."""
        return self.user.data.description

    @cached_property
    def first_tweet(self):
        """Last author tweet property."""
        return self.timeline[0]

    @cached_property
    def last_tweet(self):
        """Last author tweet property."""
        return self.timeline[-1]

    def extract_name(self):
        """Get author name from description or the first author tweet by using Spacy NER.

        Returns:
            name if found else an empty string
        """
        def search_name(d):
            """Extract first found two-word string (real person name) from Spacy entities analysis."""
            names = [
                x.text for x in d.ents if x.label_ == "PER"
                if len(x.text.split()) > 1
            ]
            return names[0] if names else ""

        description, tweet = self.description, self.first_tweet
        nlp = load(LOCALE.spacy_model)
        for text in (description, tweet.text):
            doc = nlp(text)
            name = search_name(doc)
            if name:
                break
        return name

    def extract_username(self) -> str:
        """Get username from description or the first author tweet by searching for @.

        Returns:
            username if found else an empty string
        """
        description, tweet = self.description, self.first_tweet
        for text in (description, tweet.text):
            if "@" in text:
                text = text.translate(
                    str.maketrans(string.punctuation.replace("@", ""),
                                  " " * (len(string.punctuation) - 1)))
                return text.split("@")[1].split()[0]
        return ""
Exemplo n.º 20
0
class MirrorOnImgurBot(tweepy.StreamListener):

    def __init__(self):
        # bot twitter user name
        self.username = '******'

        self.auth_twitter()
        self.auth_imgur()

        log.info('Init done')

    def auth_twitter(self):
        """ Authenticate with twitter and setup the 'api' object """

        log.info('Wiring up twitter auth')
        # wire up stuff with twitter auth
        self.auth = tweepy.OAuthHandler(creds.T_CONSUMER_KEY, creds.T_CONSUMER_SECRET)
        self.auth.set_access_token(creds.T_ACCESS_TOKEN, creds.T_ACCESS_TOKEN_SECRET)
        self.api  = tweepy.API(self.auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)

    def auth_imgur(self):
        """ Authenticate with imgur and setup the imgur client """

        log.info('Wiring up imgur auth')
        # wire up stuff with imgur auth
        self.imgur_client = ImgurClient(creds.I_CLIENT_ID, creds.I_CLIENT_SECRET)

    def rate_limit_status(self):
        """ Print twitter rate limit status """

        log.info('Rate limit status:\n%r' % self.api.rate_limit_status())

    def on_error(self, status):
        """ Handle twitter streaming endpoint error """

        log.error('Error: %r' % status)
        self.rate_limit_status()

    def on_data(self, raw_data):
        """ Handle tweets received from the twitter streaming endpoint.
        This method is pretty much the whole bot. It parses the tweet, pulls out the url, uploads
        to imgur and replies to the tweet it had received with the new imgur url
        """

        data             = json.loads(raw_data)
        user_mentions    = data.get('entities', {}).get('user_mentions', [])
        sender_username  = data.get('user', {}).get('screen_name', 'UnknownUser')
        bot_is_mentioned = len(filter(lambda mentioned_user : mentioned_user['screen_name'] == self.username, user_mentions)) > 0

        log.debug('Data %r' % data)
        log.info('Message from: %r' % sender_username)
        log.info('Is the bot mentioned? %r' % bot_is_mentioned)

        # if it's not a retweet and it isn't a tweet to myself from my own account
        # and if bot is mentioned then do stuff
        if not data['retweeted'] and sender_username != self.username and bot_is_mentioned:
            try:
                tweet_id = data.get('id_str')
                url  = 'http' + data.get('text', '').split('http')[1].strip().split()[0].strip()
            except Exception as e:
                log.error('Error: %r', e)
                url = ''
            finally:
                self.handle_url(url, tweet_id, sender_username)

    def handle_url(self, url, tweet_id, sender_username):
        """ Decide what to do based on url
        If it's valid, upload to imgur else reply with an error message
        """

        sender_username = '******' + sender_username

        log.info('Url received: %r' % url)
        log.info('Reply to user: %r' % sender_username)

        try:
            parsedUrl = urlparse(url)

            # if the url is indeed valid (has a hostname)
            if parsedUrl.netloc != '':
                # mirror on imgur
                imgur_reply = self.imgur_client.upload_from_url(url)
                msg         = sender_username + ' ' + imgur_reply.get('link', 'Upload failed')

                log.debug('Imgur response: %r' % imgur_reply)
                log.info('Bot reply: %r to tweet id %r' % (msg, tweet_id))

                self.api.update_status(status = msg, in_reply_to_status_id = tweet_id)
            else:
                self.api.update_status(status = sender_username + ' ' + 'send me a valid url please :)', in_reply_to_status_id = tweet_id)
        except Exception as e:
            log.error('Error: %r' % e)
            self.api.update_status(status = sender_username + ' ' + 'something went wrong. womp womp :(', in_reply_to_status_id = tweet_id)
        finally:
            log.info('Reply sent')
Exemplo n.º 21
0
class Imgur:

   imgur = None

   client_id = None
   client_secret = None

   def __init__(self):
      config = get_config()
      config.read('auth.ini')
      self.client_id = config.get('imgur', 'client_id')
      self.client_secret = config.get('imgur', 'client_secret')

      self.imgur = ImgurClient(self.client_id, self.client_secret)

   def createAlbum(self, album_title):
   	  log.d('Starting album creation...')

   	  fields = {
	  	'Authorization':'Client-ID {' + self.client_id + '}',
		'title':album_title
	  }  

	  try:
	  	album = self.imgur.create_album(fields)
	  	log.d('Album created successfully.')
	  	return album
	  except ImgurClientError as e:
	  	log.e('Failed to create album, aborting.')
	  	log.e(e.error_message)
	  	log.e(e.status_code)
	  	return None

   def uploadImagesToAlbum(self, album, imgList):
      # For anonymous albums, use album's deletehash instead of id
	  config = {
	  	'album': album['deletehash']
	  }

	  log.d(imgList)

	  #Upload images to album
	  for link in imgList:
	  	log.d('Uploading ' + link + '...')
	  	try:
	  		response = self.imgur.upload_from_url(link, config=config, anon=True)
	  		log.d('Image uploaded successfully.')
	  	except ImgurClientError as e:
	  	    print(e.error_message)
	  	    print(e.status_code)
	  	    log.e("Image upload failed, aborting.")
	  	    return 0  

	  	# Wait for n seconds so Imgur doesn't get upset with us
	  	time.sleep(5)    

	  log.d('All images uploaded successfully.') 

	  return 1    	

   def upload(self, imgLinks, albumName):
	  # Create new Imgur album
	  album = self.createAlbum(albumName)

	  if(album is None):
	  	return None

	  success = self.uploadImagesToAlbum(album, imgLinks)

	  if(not success):
	  	return None
	  
	  # Return album URL
	  albumLink = 'https://imgur.com/a/' + album['id']

	  log.d('Album Link: ' + albumLink)

	  return albumLink
Exemplo n.º 22
0
def uploadToImgur(userid, url):
    db = app.connect_db()
    c = db.cursor()
    c.execute(
        "SELECT map_url,name,farmname,date,imgur_json FROM playerinfo WHERE url="
        + app.app.sqlesc,
        (url, ),
    )
    result = c.fetchone()
    if result[4] != None:
        previous_upload_properties = json.loads(result[4])
        if time.time() < previous_upload_properties["upload_time"] + (2 *
                                                                      3600):
            return {
                "error": "too_soon",
                "link": previous_upload_properties["imgur_url"],
            }
    map_url = result[0]
    titlestring = u"{} Farm, {} by {}".format(result[2], result[3], result[1])
    descriptionstring = (
        u"Stardew Valley game progress, full summary at http://upload.farm/{}".
        format(url))
    # try:
    c.execute("SELECT imgur_json FROM users WHERE id=" + app.app.sqlesc,
              (userid, ))
    r = json.loads(c.fetchone()[0])
    access_token = r["access_token"]
    refresh_token = r["refresh_token"]
    client = ImgurClient(app.app.config["IMGUR_CLIENTID"],
                         app.app.config["IMGUR_SECRET"])
    client.set_user_auth(access_token, refresh_token)
    # file = url_for('home',filename=map_url,_external=True)
    # print 'uploaded to',file
    # client.upload_from_url(file,config={'title':'uploaded from','description':'upload.farm'},anon=False)
    if app.app.config["IMGUR_DIRECT_UPLOAD"] == True:
        result = client.upload_from_path(
            map_url,
            config={
                "title": titlestring,
                "description": descriptionstring
            },
            anon=False,
        )
    else:
        map_url = u"http://upload.farm/{}".format(map_url)
        result = client.upload_from_url(
            map_url,
            config={
                "title": titlestring,
                "description": descriptionstring
            },
            anon=False,
        )
    print(result)
    imgur_json = json.dumps({
        "imgur_url": "http://imgur.com/" + result["id"],
        "upload_time": time.time()
    })
    c.execute(
        "UPDATE playerinfo SET imgur_json=" + app.app.sqlesc + " WHERE url=" +
        app.app.sqlesc,
        (imgur_json, url),
    )
    db.commit()
    try:
        return {"success": None, "link": result["link"]}
    except:
        return {"error": "upload_issue", "link": None}
Exemplo n.º 23
0
class MirrorOnImgurBot(tweepy.StreamListener):
    def __init__(self):
        # bot twitter user name
        self.username = '******'

        self.auth_twitter()
        self.auth_imgur()

        log.info('Init done')

    def auth_twitter(self):
        """ Authenticate with twitter and setup the 'api' object """

        log.info('Wiring up twitter auth')
        # wire up stuff with twitter auth
        self.auth = tweepy.OAuthHandler(creds.T_CONSUMER_KEY,
                                        creds.T_CONSUMER_SECRET)
        self.auth.set_access_token(creds.T_ACCESS_TOKEN,
                                   creds.T_ACCESS_TOKEN_SECRET)
        self.api = tweepy.API(self.auth,
                              wait_on_rate_limit=True,
                              wait_on_rate_limit_notify=True)

    def auth_imgur(self):
        """ Authenticate with imgur and setup the imgur client """

        log.info('Wiring up imgur auth')
        # wire up stuff with imgur auth
        self.imgur_client = ImgurClient(creds.I_CLIENT_ID,
                                        creds.I_CLIENT_SECRET)

    def rate_limit_status(self):
        """ Print twitter rate limit status """

        log.info('Rate limit status:\n%r' % self.api.rate_limit_status())

    def on_error(self, status):
        """ Handle twitter streaming endpoint error """

        log.error('Error: %r' % status)
        self.rate_limit_status()

    def on_data(self, raw_data):
        """ Handle tweets received from the twitter streaming endpoint.
        This method is pretty much the whole bot. It parses the tweet, pulls out the url, uploads
        to imgur and replies to the tweet it had received with the new imgur url
        """

        data = json.loads(raw_data)
        user_mentions = data.get('entities', {}).get('user_mentions', [])
        sender_username = data.get('user', {}).get('screen_name',
                                                   'UnknownUser')
        bot_is_mentioned = len(
            filter(
                lambda mentioned_user: mentioned_user['screen_name'] == self.
                username, user_mentions)) > 0

        log.debug('Data %r' % data)
        log.info('Message from: %r' % sender_username)
        log.info('Is the bot mentioned? %r' % bot_is_mentioned)

        # if it's not a retweet and it isn't a tweet to myself from my own account
        # and if bot is mentioned then do stuff
        if not data[
                'retweeted'] and sender_username != self.username and bot_is_mentioned:
            try:
                tweet_id = data.get('id_str')
                url = 'http' + data.get(
                    'text', '').split('http')[1].strip().split()[0].strip()
            except Exception as e:
                log.error('Error: %r', e)
                url = ''
            finally:
                self.handle_url(url, tweet_id, sender_username)

    def handle_url(self, url, tweet_id, sender_username):
        """ Decide what to do based on url
        If it's valid, upload to imgur else reply with an error message
        """

        sender_username = '******' + sender_username

        log.info('Url received: %r' % url)
        log.info('Reply to user: %r' % sender_username)

        try:
            parsedUrl = urlparse(url)

            # if the url is indeed valid (has a hostname)
            if parsedUrl.netloc != '':
                # mirror on imgur
                imgur_reply = self.imgur_client.upload_from_url(url)
                msg = sender_username + ' ' + imgur_reply.get(
                    'link', 'Upload failed')

                log.debug('Imgur response: %r' % imgur_reply)
                log.info('Bot reply: %r to tweet id %r' % (msg, tweet_id))

                self.api.update_status(status=msg,
                                       in_reply_to_status_id=tweet_id)
            else:
                self.api.update_status(status=sender_username + ' ' +
                                       'send me a valid url please :)',
                                       in_reply_to_status_id=tweet_id)
        except Exception as e:
            log.error('Error: %r' % e)
            self.api.update_status(status=sender_username + ' ' +
                                   'something went wrong. womp womp :(',
                                   in_reply_to_status_id=tweet_id)
        finally:
            log.info('Reply sent')
Exemplo n.º 24
0
submission_generator = reddit.get_subreddit('gifs').get_new(limit=5)

found_something = False

for submission in submission_generator:
    post_id = vars(submission)['id']
    if post_id in open("processed.txt").read():
        print("Already processed")
        continue
    title = submission.title
    found_something = True
    url = vars(submission)['url']
    permalink = vars(submission)['permalink']
    #print(pprint.pprint(vars(submission)))
    extension = url[len(url) - 3:]
    if (extension == "gif"):
        try:
            result = client.upload_from_url(url, config=None, anon=True)
            gifv = result['gifv']
            #print("gifv version: " + gifv)
            v_fixed.append([url, gifv])
            submission.add_comment('Here is a blazing fast gifv version! ' +
                                   gifv)
            print(("Title: " + title + "\nPermalink: " + permalink +
                   "\nURL: " + url + "\ngifv: " + gifv))
            with open("processed.txt", "a") as test:
                test.write(post_id + "\n")
        except:
            print("Looks like an error, skipping to the next post...")
            continue
    time.sleep(5)
Exemplo n.º 25
0
from imgurpython import ImgurClient

# If you already have an access/refresh pair in hand
client_id = 'ffc7dd68362e1f6'
client_secret = '9a2a06c85329e39648ac4094ed6174a3215f098e'
access_token = '00b4ebf77302bf9665cb8d016858d77e19a4f93a'
refresh_token = '6e7c6da8f481ee71cbae41a046dcedeec84797db'

# Note since access tokens expire after an hour, only the refresh token is required (library handles autorefresh)
client = ImgurClient(client_id, client_secret, access_token, refresh_token)

url = 'https://im.ezgif.com/tmp/ezgif-1-7847e769eb.mp4'

client.upload_from_url(url, config=None, anon=False)
Exemplo n.º 26
0
class Imgur:
    """The most awesome images on the internet!
    
    This plugin allows basic searching on Imgur including subreddits.
    
    Warning: Searching on subreddits cannot be easily moderated, therefore it is
    extremely easy for a user to post images from an nsfw subreddit to whatever
    channel the bot is enabled in if this plugin is enabled without modification.
    The subreddit command can be disabled by changing 'enabled=True' to 'enabled=False'
    in the plugin's main file: 'plugins/imgur.py' on line 53.
    """
    def __init__(self, bot):
        self.bot = bot
        self.client = ImgurClient(config["client_id"], config["client_secret"])
    
    @c.group(pass_context=True)
    async def imgur(self, ctx):
        """Search on Imgur!"""
        if ctx.invoked_subcommand is None:
            await self.bot.say("Zoinks! You've taken a wrong turn! Try `help imgur`.")
    
    # Helper function to actually get/post the images
    async def post_image(self, request, query=None):
        case = {
            "subreddit": lambda: self.client.subreddit_gallery(query),
            "search"   : lambda: self.client.gallery_search(query),
            "random"   : lambda: self.client.gallery_random(),
            "top"      : lambda: self.client.gallery("top"),
            "hot"      : lambda: self.client.gallery("hot"),
            "rehost"   : lambda: self.client.upload_from_url(query),
        }
        function = case.get(request, None)
        image = self.bot.loop.run_in_executor(None, function)
        while True:
            await asyncio.sleep(0.25)
            if image.done():
                image = image.result()
                break
        if request == "rehost":
            await self.bot.say(image.get("link", None))
        else:
            await self.bot.say(random.choice(image).link)

    @imgur.command(name="sub", aliases=["subreddit", "reddit", "r/"], enabled=True)
    async def imgur_subreddit(self, subreddit: str):
        """Get an image from a subreddit."""
        await self.post_image("subreddit", subreddit)

    @imgur.command(name="search")
    async def imgur_search(self, *, query: str):
        """Search Imgur for (almost) anything."""
        await self.post_image("search", query)
    
    @imgur.command(name="random")
    async def imgur_random(self):
        """One free random image."""
        await self.post_image("random")
    
    @imgur.command(name="viral")
    async def imgur_viral(self, section: str = "top"):
        """Get one of the most viral images of the day.
        
        Section may be either 'top' or 'hot' and will get an image based on that criteria."""
        section = section.lower()
        if section != "top":
            section = "hot"
        await self.post_image(section)
    
    @imgur.command(name="rehost")
    async def imgur_rehost(self, url: str):
        """Rehost an image from any link to Imgur."""
        await self.post_image("rehost", url)
Exemplo n.º 27
0
from imgurpython import ImgurClient

PATH = "https://www.shardveil.com/images/cards/placeholder/arcanum-steward.png"

client = ImgurClient("ee1f0e6204e2e8d",
                     "263d92665bb96cc79098637b79958f6a317f554f")

uploaded_image = client.upload_from_url(PATH)

print(uploaded_image['id'])
Exemplo n.º 28
0
class App:
    def __init__(self):
        self.client = None


    def initialize_client(self):
        with open(os.path.join(dirname, "creds.json"), "r") as f:
            stored_creds = json.load(f)
            if "client_id" not in stored_creds:
                stored_creds["client_id"] = None
            if "client_secret" not in stored_creds:
                stored_creds["client_secret"] = None

        client_id = stored_creds["client_id"]
        client_secret = stored_creds["client_secret"]
        new_creds = False

        while True:
            try:
                self.client = ImgurClient(client_id, client_secret)
                break
            except:
                print("*** Missing or invalid client credentials. ", end="")
                print("You must first register an applicaton with Imgur ", end="")
                print("and provide the client id and secret. ", end="")
                print("See README.md for help.")
                client_id = input("Client ID: ")
                client_secret = input("Client secret: ")
                new_creds = True


        # Update creds file if there was a change.
        if new_creds:
            print("Client credentials successfully validated.\n")

            stored_creds["client_id"] = client_id
            stored_creds["client_secret"] = client_secret

            with open(os.path.join(dirname, "creds.json"), "w") as f:
                json.dump(stored_creds, f)


    def authorize_user(self):
        with open(os.path.join(dirname, "creds.json"), "r") as f:
            stored_creds = json.load(f)
            if "access_token" not in stored_creds:
                stored_creds["access_token"] = None
            if "refresh_token" not in stored_creds:
                stored_creds["refresh_token"] = None

        access_token = stored_creds["access_token"]
        refresh_token = stored_creds["refresh_token"]
        new_tokens = False
        
        while True:
            try:
                self.client.set_user_auth(access_token, refresh_token)
                break
            except:
                print("*** Missing or invalid tokens. ", end="")
                print("Please follow the OAuth flow and enter the pin.")
                webbrowser.open(self.client.get_auth_url("pin"))
                pin = input("Pin: ")

                try:
                    credentials = self.client.authorize(pin, "pin")
                except:
                    print("Invalid pin.")
                    continue

                access_token = credentials["access_token"]
                refresh_token = credentials["refresh_token"]
                new_tokens = True

        # Update creds file if there was a change.
        if new_tokens:
            print("Tokens successfully validated.\n")

            stored_creds["access_token"] = access_token
            stored_creds["refresh_token"] = refresh_token

            with open(os.path.join(dirname, "creds.json"), "w") as f:
                json.dump(stored_creds, f)


    def create_album(self, targets, title, description):
        print("Creating album... ", end="", flush=True)
        album = self.client.create_album({
            "title": title,
            "description": description
        })
        print(f"done. (id={album['id']})")

        config = { "album": album["deletehash"] }

        # If there is only one image, add title/description to the image also.
        if len(targets) == 1:
            config["title"] = title
            config["description"] = description

        images = []
        for target in targets:
            # URL upload.
            if urllib.parse.urlparse(target).scheme in ("http", "https"):
                images += filter(None, [self.upload_image(config, url=target)])
            # File upload.
            elif os.path.isfile(target):
                images += filter(None, [self.upload_image(config, path=target)])
            # Directory upload.
            elif os.path.isdir(target):
                for filepath in glob.glob(f"{target}/**"):
                    images += filter(None,
                            [self.upload_image(config, path=filepath)])
            else:
                print(f"*** Invalid target: {target} ***")

        print("========================================")

        print(f"Album: https://imgur.com/a/{album['id']}")
        if images:
            print(f"First Image: https://imgur.com/{images[0]['id']}.png")
        print("")

        return {
            "album_id": album["id"],
            "image_ids": [i["id"] for i in images]
        }


    def upload_image(self, config, url=None, path=None):
        try:
            if url is not None:
                print(f"Uploading {url}... ", end="", flush=True)
                image = self.client.upload_from_url(url, config=config)
            elif path is not None:
                print(f"Uploading {path}... ", end="", flush=True)
                image = self.client.upload_from_path(path, config=config)
            else:
                return
        except Exception as e:
            print(f"failed.\n*** {e} ***")
            return

        print(f"done. (id={image['id']})")
        return image


    def parse_args(self, raw_args):
        parser = argparse.ArgumentParser(raw_args)
        parser.add_argument(
            "targets",
            metavar="target",
            nargs="+",
            help="directory, filepath, or url to upload"
        )
        parser.add_argument(
            "-t", "--title",
            dest="title",
            metavar="title",
            help="post's title"
        )
        parser.add_argument(
            "-d", "--description",
            dest="description",
            metavar="desc",
            help="post's description"
        )
        parser.add_argument(
            "-a", "--anonymous",
            dest="anon",
            action="store_const", const=True, default=False,
            help="anonymously upload (no imgur account)"
        )
        return parser.parse_args()


    def prompt_args(self):
        print("-- Targets may be a url, filepath, or directory.")
        targets = []
        while True:
            target = input("Target: ")
            if target:
                targets.append(target)
            else:
                break

        title = input("Title: ")
        description = input("Description: ")
        anon = (input("Anonymous (y/n): ") == "y")
        print("")

        return argparse.Namespace(targets=targets, title=title,
                description=description, anon=anon)


    def run(self, raw_args):
        self.initialize_client()

        if raw_args:
            interactive = False
            args = self.parse_args(raw_args)
        else:
            interactive = True
            args = self.prompt_args()

        if not args.anon:
            self.authorize_user()

        self.create_album(args.targets, args.title, args.description)

        print(self.client.credits)

        if interactive:
            input("press enter to exit...")