本文整理汇总了Python中variety.Util.Util类的典型用法代码示例。如果您正苦于以下问题:Python Util类的具体用法?Python Util怎么用?Python Util使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Util类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _set_rating
def _set_rating(widget, rating=rating):
try:
Util.set_rating(file, rating)
main_window.on_rating_changed(file)
except Exception:
logger.exception(lambda: "Could not set EXIF rating")
main_window.show_notification(_("Could not set EXIF rating"))
示例2: fill_queue
def fill_queue(self):
page = random.randint(1, 250)
url = 'https://api.unsplash.com/photos/?page=%d&per_page=30&client_id=%s' % (page, UnsplashDownloader.CLIENT_ID)
logger.info(lambda: "Filling Unsplash queue from " + url)
r = Util.request(url)
if int(r.headers.get('X-Ratelimit-Remaining', 1000000)) < 100:
UnsplashDownloader.rate_limiting_started_time = time.time()
for item in r.json():
try:
width = item['width']
height = item['height']
if self.parent and not self.parent.size_ok(width, height):
continue
image_url = item['links']['download']
origin_url = item['links']['html']
filename = os.path.join(self.target_folder, Util.sanitize_filename(image_url.split('/')[-2] + '.jpg'))
extra_metadata = {
'sourceType': 'unsplash',
'sfwRating': 100,
'author': item['user']['name'],
'authorURL': item['user']['links']['html'],
'keywords': [cat['title'].lower().strip() for cat in item['categories']]
}
self.queue.append((origin_url, image_url, extra_metadata, filename))
except:
logger.exception(lambda: "Could not process an item from Unsplash")
raise
random.shuffle(self.queue)
logger.info(lambda: "Unsplash populated with %d URLs" % len(self.queue))
示例3: test_guess_image_url
def test_guess_image_url(self):
self.assertEquals('https://farm5.staticflickr.com/4032/4558166441_4e34855b39_o.jpg',
Util.guess_image_url({'sourceURL': 'https://www.flickr.com/photos/[email protected]/4558166441'}))
self.assertEquals('https://farm5.staticflickr.com/4077/4768189432_24275ea76b_b.jpg',
Util.guess_image_url({'sourceURL': 'http://www.flickr.com/photos/[email protected]/4768189432'}))
self.assertEquals('http://fc04.deviantart.net/fs71/i/2011/319/4/f/scarlet_leaf_wallpaper_by_venomxbaby-d4gc238.jpg',
Util.guess_image_url({'sourceURL': 'http://fc04.deviantart.net/fs71/i/2011/319/4/f/scarlet_leaf_wallpaper_by_venomxbaby-d4gc238.jpg'}))
示例4: save_locally
def save_locally(self, origin_url, image_url,
source_type=None, source_location=None, source_name=None,
force_download=False, extra_metadata={}, local_filename=None):
if not source_type:
source_type = self.source_type
if not source_name:
source_name = self.name
if not source_location:
source_location = self.location
if not force_download and self.parent and origin_url in self.parent.banned:
logger.info(lambda: "URL " + origin_url + " is banned, skip downloading")
return None
try:
os.makedirs(self.target_folder)
except Exception:
pass
if origin_url.startswith('//'):
origin_url = 'https:' + origin_url
if image_url.startswith('//'):
image_url = origin_url.split('//')[0] + image_url
if not local_filename:
local_filename = self.get_local_filename(image_url)
logger.info(lambda: "Origin URL: " + origin_url)
logger.info(lambda: "Image URL: " + image_url)
logger.info(lambda: "Local name: " + local_filename)
if not force_download and os.path.exists(local_filename):
logger.info(lambda: "File already exists, skip downloading")
return None
if self.parent and self.parent.options.safe_mode:
sfw_rating = Smart.get_sfw_rating(origin_url)
if sfw_rating is not None and sfw_rating < 100:
logger.info(lambda: "Skipping non-safe download %s. Is the source %s:%s "
"suitable for Safe mode?" % (origin_url, source_type, self.location))
return None
if self.parent and self.parent.options.safe_mode and 'keywords' in extra_metadata:
blacklisted = set(k.lower() for k in extra_metadata['keywords']) & Smart.get_safe_mode_keyword_blacklist()
if len(blacklisted) > 0:
logger.info(lambda: "Skipping non-safe download %s due to blacklisted keywords (%s). "
"Is the source %s:%s suitable for Safe mode?" %
(origin_url, str(blacklisted), source_type, self.location))
return None
try:
r = Util.request(image_url, stream=True)
with open(local_filename, 'wb') as f:
Util.request_write_to(r, f)
except Exception, e:
logger.info(lambda: "Download failed from image URL: %s (source location: %s) " % (image_url, self.location))
raise e
示例5: test_is_dead_or_not_image
def test_is_dead_or_not_image(self):
self.assertTrue(Util.is_dead_or_not_image(None))
self.assertTrue(Util.is_dead_or_not_image('not a URL'))
self.assertTrue(Util.is_dead_or_not_image('http://www.google.com/'))
self.assertTrue(Util.is_dead_or_not_image('http://vrty.org/'))
self.assertTrue(Util.is_dead_or_not_image('http://www.google.com/dejkjdrelkjflkrejfjre'))
self.assertFalse(Util.is_dead_or_not_image('http://upload.wikimedia.org/wikipedia/commons/5/53/Wikipedia-logo-en-big.png'))
self.assertFalse(Util.is_dead_or_not_image('https://farm8.staticflickr.com/7133/7527967878_85fea93129_o.jpg'))
self.assertFalse(Util.is_dead_or_not_image('http://interfacelift.com/wallpaper/D98ef829/00899_rustedbolt_2560x1600.jpg'))
self.assertTrue(Util.is_dead_or_not_image('http://wallpapers.wallbase.cc/rozne/wallpaper-1227671.jpg'))
self.assertTrue(Util.is_dead_or_not_image('http://ns223506.ovh.net/rozne/a1b2/wallpaper-1996019.png'))
示例6: get_for_keyword
def get_for_keyword(self, keyword):
logger.info(lambda: "Fetching quotes from Goodreads for keyword=%s" % keyword)
url = iri2uri(u"https://www.goodreads.com/quotes/tag?utf8=\u2713&id=%s" % keyword)
soup = Util.html_soup(url)
page_links = list(Util.safe_map(int,
[pagelink.contents[0] for pagelink in
soup.find_all(href=re.compile('quotes/tag.*page='))]))
if page_links:
page = random.randint(1, max(page_links))
url = iri2uri(u"https://www.goodreads.com/quotes/tag?utf8=\u2713&id=%s&page=%d" % (keyword, page))
soup = Util.html_soup(url)
return self.get_from_soup(url, soup)
示例7: check_quit
def check_quit():
global terminate
if not terminate:
GObject.timeout_add(1000, check_quit)
return
logging.getLogger("variety").info("Terminating signal received, quitting...")
safe_print(_("Terminating signal received, quitting..."),
"Terminating signal received, quitting...")
global VARIETY_WINDOW
if VARIETY_WINDOW:
GObject.idle_add(VARIETY_WINDOW.on_quit)
Util.start_force_exit_thread(10)
示例8: get_for_author
def get_for_author(self, author):
logger.info(lambda: "Fetching quotes from Goodreads for author=%s" % author)
url = iri2uri(u"https://www.goodreads.com/quotes/search?utf8=\u2713&q=%s" % author)
soup = Util.html_soup(url)
page_links = list(Util.safe_map(int,
[pagelink.contents[0] for pagelink in
soup.find_all(href=re.compile('quotes/search.*page='))]))
if page_links:
page = random.randint(1, max(page_links))
url = iri2uri(u"https://www.goodreads.com/quotes/search?utf8=\u2713&q=%s&page=%d" % (author, page))
soup = Util.html_soup(url)
return self.get_from_soup(url, soup)
示例9: fill_queue
def fill_queue(self):
logger.info(lambda: "Filling Bing queue from " + self.location)
s = Util.fetch_json(BingDownloader.BING_JSON_URL)
for item in s['images']:
try:
image_url = 'https://www.bing.com' + item['url']
filename = item['url'].split('/')[-1]
name = filename[0:filename.find('_EN')]
src_url = 'https://www.bing.com/gallery/#images/%s' % name
try:
date = datetime.strptime(item['startdate'], '%Y%m%d').strftime('%Y-%m-%d')
except:
date = item['startdate']
extra_metadata = {
'sourceType': 'bing',
'sfwRating': 100,
'headline': 'Bing Photo of the Day, %s' % date,
'description': item['copyright'],
}
self.queue.append((src_url, image_url, extra_metadata))
except:
logger.exception(lambda: "Could not process an item in the Bing json result")
random.shuffle(self.queue)
logger.info(lambda: "Bing queue populated with %d URLs" % len(self.queue))
示例10: ajax
def ajax(self, url, data, error_msg_handler):
try:
return Util.fetch_json(url, data)
except requests.exceptions.HTTPError, e:
logger.exception(lambda: 'HTTPError for ' + url)
error_msg_handler(_('Oops, server returned error (%s)') % e.response.status_code)
raise
示例11: fill_queue
def fill_queue(self):
logger.info(lambda: "Reddit URL: " + self.location)
json_url = RedditDownloader.build_json_url(self.location)
s = Util.fetch_json(json_url)
for item in s['data']['children']:
try:
data = item['data']
image_url = data['url']
if re.match(r'^http(s)?://imgur\.com/\w+$', image_url):
image_url = image_url.replace('://', '://i.') + '.jpg'
if image_url.lower().endswith(('.jpg', '.jpeg', '.png')):
src_url = 'https://www.reddit.com' + data['permalink']
extra_metadata = {'sourceType': 'reddit'}
if data['over_18']:
extra_metadata['sfwRating'] = 0
if self.parent and self.parent.options.safe_mode:
continue
self.queue.append((src_url, image_url, extra_metadata))
except Exception:
logger.exception(lambda: "Could not process an item in the Reddit json result")
random.shuffle(self.queue)
logger.info(lambda: "Reddit queue populated with %d URLs" % len(self.queue))
示例12: get_image_url
def get_image_url(origin_url):
photo_id = FlickrDownloader.get_photo_id(origin_url)
call = 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=%s&photo_id=%s&format=json&nojsoncallback=1' % \
(API_KEY, photo_id)
resp = Util.fetch_json(call)
s = max(resp['sizes']['size'], key=lambda size: int(size['width']))
return s['source']
示例13: set_icon
def set_icon(self, icon):
def set_from_theme_icon(name):
if self.indicator:
logger.info(lambda: "Showing indicator icon %s from GTK theme" % name)
self.indicator.set_icon(name)
if self.status_icon:
logger.info(lambda: "Showing status icon %s from GTK theme" % name)
self.status_icon.set_from_icon_name(name)
if icon == "Light":
if Gtk.IconTheme.get_default().has_icon(THEME_ICON_NAME):
set_from_theme_icon(THEME_ICON_NAME)
return
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator.png")
elif icon == "Dark":
if Gtk.IconTheme.get_default().has_icon(THEME_ICON_NAME_DARK):
set_from_theme_icon(THEME_ICON_NAME_DARK)
return
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator-dark.png")
elif icon and os.access(icon, os.R_OK) and Util.is_image(icon):
icon_path = icon
else:
icon_path = varietyconfig.get_data_file("media", "variety-indicator.png")
if self.indicator:
logger.info(lambda: "Showing indicator icon image: " + icon_path)
self.indicator.set_icon(icon_path)
if self.status_icon:
logger.info(lambda: "Showing status icon image: " + icon_path)
self.status_icon.set_from_file(icon_path)
示例14: populate_sfw_menu
def populate_sfw_menu(self):
try:
self.rating_items = []
sfw_ratings = Smart.get_all_sfw_ratings()
def _gui_update(sfw_ratings):
try:
def _add_menuitem(rating):
menuitem = Gtk.ImageMenuItem(_(rating['label_long']))
menuitem.set_visible(True)
def _rate(*args, **kwargs):
self.parent.report_sfw_rating(file=None, rating=rating['rating'])
menuitem.connect("activate", _rate)
try:
menuitem.set_always_show_image(True)
image = Gtk.Image()
image.set_from_file(varietyconfig.get_data_file("media", "sfw-%s.svg" % rating['rating']))
menuitem.set_image(image)
except:
logger.exception(lambda: "Could not set image to NSFW menuitem:")
self.sfw_menu.append(menuitem)
self.rating_items.append(menuitem)
map(_add_menuitem, reversed(sfw_ratings))
separator = Gtk.SeparatorMenuItem.new()
separator.set_visible(True)
self.sfw_menu.append(separator)
self.safe_mode = Gtk.CheckMenuItem(_("_Safe mode"))
self.safe_mode.set_visible(True)
self.safe_mode.set_active(self.parent.options.safe_mode)
self.safe_mode.set_use_underline(True)
self.safe_mode_handler_id = self.safe_mode.connect("toggled", self.parent.on_safe_mode_toggled)
self.sfw_menu.append(self.safe_mode)
self.sfw_menu_item.set_sensitive(True)
self.parent.update_indicator()
except Exception:
logger.exception(lambda: 'Oops, could not populate NSFW menu:')
Util.add_mainloop_task(_gui_update, sfw_ratings)
except Exception:
logger.exception(lambda: 'Oops, could not populate NSFW menu:')
示例15: main
def main():
if datetime.datetime.now() - datetime.datetime.strptime(REL_DATE, "%Y-%m-%d") > datetime.timedelta(
days=180):
print VarietyWindow.OUTDATED_MSG
return
# Ctrl-C
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigint_handler)
signal.signal(signal.SIGQUIT, sigint_handler)
Util.makedirs(os.path.expanduser(u"~/.config/variety/"))
arguments = map(_u, sys.argv[1:])
# validate arguments and set up logging
options, args = VarietyWindow.VarietyWindow.parse_options(arguments)
set_up_logging(options.verbose)
monkeypatch_ssl()
if options.verbose > 2:
Util.log_all(VarietyWindow.VarietyWindow)
if options.verbose > 3:
Util.log_all(ThumbsManager.ThumbsManager)
Util.log_all(ThumbsWindow.ThumbsWindow)
bus = dbus.SessionBus()
# ensure singleton
if bus.request_name(DBUS_KEY) != dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
if not arguments:
arguments = ["--preferences"]
safe_print(_("Variety is already running. Sending the command to the running instance."),
"Variety is already running. Sending the command to the running instance.")
method = bus.get_object(DBUS_KEY, DBUS_PATH).get_dbus_method("process_command")
result = method(arguments)
if result:
safe_print(result)
return
# Run the application.
window = VarietyWindow.VarietyWindow()
global VARIETY_WINDOW
VARIETY_WINDOW = window
service = VarietyService(window)
bus.call_on_disconnection(window.on_quit)
window.start(arguments)
GObject.timeout_add(2000, check_quit)
GObject.threads_init()
Gdk.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()