本文整理汇总了Python中models.Subscription.select方法的典型用法代码示例。如果您正苦于以下问题:Python Subscription.select方法的具体用法?Python Subscription.select怎么用?Python Subscription.select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Subscription
的用法示例。
在下文中一共展示了Subscription.select方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cmd_all
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_all(self, msg, args, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return self.reply(msg, 'You have no subscriptions, so no tweets to show!')
text = ""
for sub in subscriptions:
if sub.last_tweet is None:
text += "\n{screen_name}: <no tweets yet>".format(
screen_name=escape_markdown(sub.tw_user.screen_name),
)
else:
text += ("\n{screen_name}:\n{text} "
"[link](https://twitter.com/{screen_name}/status/{tw_id})").format(
text=markdown_twitter_usernames(escape_markdown(sub.last_tweet.text)),
tw_id=sub.last_tweet.tw_id,
screen_name=escape_markdown(sub.tw_user.screen_name),
)
self.reply(msg, text,
disable_web_page_preview=True,
parse_mode=telegram.ParseMode.MARKDOWN)
示例2: work
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def work(self):
for source in Subscription.select():
if self.produce(source.url):
self._logger.info('Producing source url %s.' % source.url)
else:
self._logger.info('Skipping source url %s.' % source.url)
time.sleep(10)
示例3: get_feed_groups_for_user
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def get_feed_groups_for_user(self, user):
q = Subscription.select(Subscription).join(User).where(User.id == user.id).distinct().naive()
groups = defaultdict(lambda: [])
for s in q:
groups[str(s.group.id)].append('%d' % s.feed.id)
result = []
for g in groups.keys():
result.append({'group':g, 'feed_ids':','.join(groups[g])})
return result
示例4: cmd_wipe
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_wipe(self, msg, args, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
subs = "You had no subscriptions."
if subscriptions:
subs = ''.join([
"For the record, you were subscribed to these users: ",
', '.join((s.tw_user.screen_name for s in subscriptions)),
'.'])
self.reply(msg, "Okay, I'm forgetting about this chat. " + subs +
" Come back to me anytime you want. Goodbye!")
chat.delete_instance(recursive=True)
示例5: cmd_export
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_export(self, msg, args, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return self.reply(msg, 'You have no subscriptions yet! Add one with /sub username')
subs = ['']
for sub in subscriptions:
subs.append(sub.tw_user.screen_name)
subject = "Use this to subscribe to all subscribed Twitter users in another chat:\n\n"
self.reply(
msg,
subject + "/sub " + " ".join(subs))
示例6: cmd_list
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_list(self, msg, args, chat=None):
subscriptions = list(Subscription.select().where(
Subscription.tg_chat == chat))
if len(subscriptions) == 0:
return self.reply(msg, 'You have no subscriptions yet! Add one with /sub username')
subs = ['']
for sub in subscriptions:
subs.append(sub.tw_user.full_name)
subject = "This group is" if chat.is_group else "You are"
self.reply(
msg,
subject + " subscribed to the following Twitter users:\n" +
"\n - ".join(subs) + "\n\nYou can remove any of them using /unsub username")
示例7: cmd_sub
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_sub(self, msg, args, chat=None):
if len(args) < 1:
self.reply(msg, "Use /sub username1 username2 username3 ...")
return
tw_usernames = args
not_found = []
already_subscribed = []
successfully_subscribed = []
for tw_username in tw_usernames:
tw_user = self.get_tw_user(tw_username)
if tw_user is None:
not_found.append(tw_username)
continue
if Subscription.select().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).count() == 1:
already_subscribed.append(tw_user.full_name)
continue
Subscription.create(tg_chat=chat, tw_user=tw_user)
successfully_subscribed.append(tw_user.full_name)
reply = ""
if len(not_found) is not 0:
reply += "Sorry, I didn't find username{} {}\n\n".format(
"" if len(not_found) is 1 else "s",
", ".join(not_found)
)
if len(already_subscribed) is not 0:
reply += "You're already subscribed to {}\n\n".format(
", ".join(already_subscribed)
)
if len(successfully_subscribed) is not 0:
reply += "I've added your subscription to {}".format(
", ".join(successfully_subscribed)
)
self.reply(msg, reply)
示例8: cmd_unsub
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def cmd_unsub(self, msg, args, chat=None):
if len(args) < 1:
self.reply(msg, "Use /unsub username1 username2 username3 ...")
return
tw_usernames = args
not_found = []
successfully_unsubscribed = []
for tw_username in tw_usernames:
tw_user = self.get_tw_user(tw_username)
if tw_user is None or Subscription.select().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).count() == 0:
not_found.append(tw_username)
continue
Subscription.delete().where(
Subscription.tw_user == tw_user,
Subscription.tg_chat == chat).execute()
successfully_unsubscribed.append(tw_user.full_name)
reply = ""
if len(not_found) is not 0:
reply += "I didn't find any subscription to {}\n\n".format(
", ".join(not_found)
)
if len(successfully_unsubscribed) is not 0:
reply += "You are no longer subscribed to {}".format(
", ".join(successfully_unsubscribed)
)
self.reply(msg, reply)
示例9: run
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def run(self):
self.logger.debug("Fetching tweets...")
# fetch the tw users' tweets
tw_users = (TwitterUser.select()
.join(Subscription)
.group_by(TwitterUser)
.order_by(TwitterUser.last_fetched))
for tw_user in tw_users:
try:
if tw_user.last_tweet_id == 0:
# get just the latest tweet
self.logger.debug(
"Fetching latest tweet by {}".format(tw_user.screen_name))
tweets = self.bot.tw.user_timeline(
screen_name=tw_user.screen_name,
count=1)
else:
# get the fresh tweets
self.logger.debug(
"Fetching new tweets from {}".format(tw_user.screen_name))
tweets = self.bot.tw.user_timeline(
screen_name=tw_user.screen_name,
since_id=tw_user.last_tweet_id)
tw_user.last_fetched = datetime.now()
tw_user.save()
except tweepy.error.TweepError as e:
sc = e.response.status_code
if sc == 429:
self.logger.debug("- Hit ratelimit, breaking.")
break
if sc == 401:
self.logger.debug("- Protected tweets here.")
continue
if sc == 404:
self.logger.debug("- 404? Maybe screen name changed?")
continue
self.logger.debug(
"- Unknown exception, Status code {}".format(sc))
continue
for tweet in tweets:
self.logger.debug("- Got tweet: {}".format(tweet.text))
# Check if tweet contains media, else check if it contains a link to an image
extensions = ('.jpg', '.jpeg', '.png', '.gif')
pattern = '[(%s)]$' % ')('.join(extensions)
photo_url = ''
if 'media' in tweet.entities:
photo_url = tweet.entities['media'][0]['media_url_https']
else:
for url in tweet.entities['urls']:
if re.search(pattern, url['expanded_url']):
photo_url = url['expanded_url']
break
if photo_url:
self.logger.debug("- - Found media URL in tweet: " + photo_url)
tw, _created = Tweet.get_or_create(
tw_id=tweet.id,
text=html.unescape(tweet.text),
created_at=tweet.created_at,
twitter_user=tw_user,
photo_url=photo_url,
)
# send the new tweets to subscribers
for s in Subscription.select():
# are there new tweets? send em all!
self.logger.debug(
"Checking subscription {} {}".format(s.tg_chat.chat_id, s.tw_user.screen_name))
if s.last_tweet_id == 0: # didn't receive any tweet yet
try:
tw = (s.tw_user.tweets.select()
.order_by(Tweet.tw_id.desc())
.limit(1))[0]
self.bot.send_tweet(s.tg_chat, tw)
# save the latest tweet sent on this subscription
s.last_tweet_id = tw.tw_id
s.save()
except IndexError:
self.logger.debug("- No tweets available yet on {}".format(s.tw_user.screen_name))
continue
if s.tw_user.last_tweet_id > s.last_tweet_id:
self.logger.debug("- Some fresh tweets here!")
for tw in (s.tw_user.tweets.select()
.where(Tweet.tw_id > s.last_tweet_id)
.order_by(Tweet.tw_id.desc())
):
self.bot.send_tweet(s.tg_chat, tw)
#.........这里部分代码省略.........
示例10: run
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def run(self):
self.logger.debug("Fetching tweets...")
# fetch the tw users' tweets
for tw_user in TwitterUser.select():
if tw_user.subscriptions.count() == 0:
self.logger.debug(
"Skipping {} because 0 subscriptions".format(tw_user.screen_name))
continue
try:
if tw_user.last_tweet_id == 0:
# get just the latest tweet
self.logger.debug(
"Fetching the latest tweet from {}".format(tw_user.screen_name))
tweets = self.bot.tw.user_timeline(
screen_name=tw_user.screen_name,
count=1)
else:
# get the fresh tweets
self.logger.debug(
"Fetching new tweets from {}".format(tw_user.screen_name))
tweets = self.bot.tw.user_timeline(
screen_name=tw_user.screen_name,
since_id=tw_user.last_tweet_id)
except tweepy.error.TweepError:
self.logger.debug(
"Whoops, I couldn't get tweets from {}!".format(tw_user.screen_name))
continue
for tweet in tweets:
self.logger.debug("Got tweet: {}".format(tweet.text))
# Check if tweet contains media, else check if it contains a link to an image
extensions = ('.jpg', '.jpeg', '.png', '.gif')
pattern = '[(%s)]$' % ')('.join(extensions)
photo_url = ''
if 'media' in tweet.entities:
photo_url = tweet.entities['media'][0]['media_url_https']
else:
for url in tweet.entities['urls']:
if re.search(pattern, url['expanded_url']):
photo_url = url['expanded_url']
break
if photo_url:
self.logger.debug("Found media URL in tweet: " + photo_url)
tw, _created = Tweet.get_or_create(
tw_id=tweet.id,
text=html.unescape(tweet.text),
created_at=tweet.created_at,
twitter_user=tw_user,
photo_url=photo_url,
)
# send the new tweets to subscribers
for s in Subscription.select():
# are there new tweets? send em all!
self.logger.debug(
"Checking subscription {} {}".format(s.tg_chat.chat_id, s.tw_user.screen_name))
if s.last_tweet_id == 0: # didn't receive any tweet yet
try:
tw = (s.tw_user.tweets.select()
.order_by(Tweet.tw_id.desc())
.limit(1))[0]
self.bot.send_tweet(s.tg_chat, tw)
# save the latest tweet sent on this subscription
s.last_tweet_id = tw.tw_id
s.save()
except IndexError:
self.logger.debug("No tweets available yet on {}".format(s.tw_user.screen_name))
continue
if s.tw_user.last_tweet_id > s.last_tweet_id:
self.logger.debug("Some fresh tweets here!")
for tw in (s.tw_user.tweets.select()
.where(Tweet.tw_id > s.last_tweet_id)
.order_by(Tweet.tw_id.desc())
):
self.bot.send_tweet(s.tg_chat, tw)
# save the latest tweet sent on this subscription
s.last_tweet_id = s.tw_user.last_tweet_id
s.save()
continue
self.logger.debug("No new tweets here.")
示例11: run
# 需要导入模块: from models import Subscription [as 别名]
# 或者: from models.Subscription import select [as 别名]
def run(self, bot):
self.logger.debug("Fetching tweets...")
tweet_rows = []
# fetch the tw users' tweets
tw_users = list((TwitterUser.select()
.join(Subscription)
.group_by(TwitterUser)
.order_by(TwitterUser.last_fetched)))
updated_tw_users = []
users_to_cleanup = []
for tw_user in tw_users:
try:
if tw_user.last_tweet_id == 0:
# get just the latest tweet
self.logger.debug(
"Fetching latest tweet by {}".format(tw_user.screen_name))
tweets = bot.tw.user_timeline(
screen_name=tw_user.screen_name,
count=1)
else:
# get the fresh tweets
self.logger.debug(
"Fetching new tweets from {}".format(tw_user.screen_name))
tweets = bot.tw.user_timeline(
screen_name=tw_user.screen_name,
since_id=tw_user.last_tweet_id)
updated_tw_users.append(tw_user)
except tweepy.error.TweepError as e:
sc = e.response.status_code
if sc == 429:
self.logger.debug("- Hit ratelimit, breaking.")
break
if sc == 401:
users_to_cleanup.append((tw_user, 'PROTECTED'))
self.logger.debug("- Protected tweets here. Cleaning up this user")
continue
if sc == 404:
users_to_cleanup.append((tw_user, 'NOTFOUND'))
self.logger.debug("- 404? Maybe screen name changed? Cleaning up this user")
continue
self.logger.debug(
"- Unknown exception, Status code {}".format(sc))
continue
for tweet in tweets:
self.logger.debug("- Got tweet: {}".format(tweet.text))
# Check if tweet contains media, else check if it contains a link to an image
extensions = ('.jpg', '.jpeg', '.png', '.gif')
pattern = '[(%s)]$' % ')('.join(extensions)
photo_url = ''
tweet_text = html.unescape(tweet.text)
if 'media' in tweet.entities:
photo_url = tweet.entities['media'][0]['media_url_https']
else:
for url_entity in tweet.entities['urls']:
expanded_url = url_entity['expanded_url']
if re.search(pattern, expanded_url):
photo_url = expanded_url
break
if photo_url:
self.logger.debug("- - Found media URL in tweet: " + photo_url)
for url_entity in tweet.entities['urls']:
expanded_url = url_entity['expanded_url']
indices = url_entity['indices']
display_url = tweet.text[indices[0]:indices[1]]
tweet_text = tweet_text.replace(display_url, expanded_url)
tw_data = {
'tw_id': tweet.id,
'text': tweet_text,
'created_at': tweet.created_at,
'twitter_user': tw_user,
'photo_url': photo_url,
}
try:
t = Tweet.get(Tweet.tw_id == tweet.id)
self.logger.warning("Got duplicated tw_id on this tweet:")
self.logger.warning(str(tw_data))
except Tweet.DoesNotExist:
tweet_rows.append(tw_data)
if len(tweet_rows) >= self.TWEET_BATCH_INSERT_COUNT:
Tweet.insert_many(tweet_rows).execute()
tweet_rows = []
TwitterUser.update(last_fetched=datetime.now()) \
.where(TwitterUser.id << [tw.id for tw in updated_tw_users]).execute()
if not updated_tw_users:
return
if tweet_rows:
Tweet.insert_many(tweet_rows).execute()
#.........这里部分代码省略.........