def do(self): logger.info("Initiating cronjob: {}".format(self.code)) try: auth = tweepy.OAuthHandler(self._tak, self._tas) auth.set_access_token(self._tat, self._tats) api = tweepy.API(auth) user_timeline = api.user_timeline(screen_name = self.screen_name) except Exception: e = traceback.format_exc() logger.error("Could not get timeline for user {}. Error: {}".format(self.screen_name, e)) return False not_seen = [] for t in user_timeline: if t.author.screen_name == self.screen_name: chex, url = t.text.split() tweet_id = t.id_str if EveryColorBotTweet.objects.get_or_none(url = url) is None: R, G, B = cu.hex2rgb(chex) html = cu.rgb2html((R, G, B)) l, a, b = (cu._2lab((R, G, B))).get_value_tuple() color_inst = Color.objects.get_or_none(html = html) if color_inst is None: color_inst = Color(html = html, hex = chex, rgb_r = R, rgb_g = G, rgb_b = B, l = l, a = a, b = b) color_inst.save() not_seen.append({'color_code': chex, 'everycolorbot_url': url}) logger.info("Adding {} {} into EveryColorBotTweet-table".format(chex, url)) instance = EveryColorBotTweet(color = color_inst, url = url, tweeted = False, tweet_id = tweet_id) instance.save()
def addItem(request): form1 = TypeForm({}) form2 = VenderForm({}) form3 = ColorForm({}) if request.method == 'POST': form1 = TypeForm(request.POST) form2 = VenderForm(request.POST) form3 = ColorForm(request.POST) success1 = '' success2 = '' success3 = '' inventorys = None if form1.is_valid(): cd = form1.cleaned_data type = Type() type.TypeName = cd['TypeName'] inventorys = Type.objects.filter(TypeName__contains=type.TypeName) if (inventorys.count()==0): type.save() success1 = '类型添加成功' else: success1 = '类型已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success1': success1}, context_instance = RequestContext(request)) if form2.is_valid(): cd = form2.cleaned_data vender = Vender() vender.VenderName = cd['VenderName'] inventorys = Vender.objects.filter(VenderName__contains=vender.VenderName) if (inventorys.count()==0): vender.save() success2 = '厂家添加成功' else: success2 = '厂家已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success2': success2}, context_instance = RequestContext(request)) if form3.is_valid(): cd = form3.cleaned_data color = Color() color.ColorName = cd['ColorName'] inventorys = Color.objects.filter(ColorName__contains=color.ColorName) if (inventorys.count()==0): color.save() success3 = '颜色添加成功' else: success3 = '颜色已存在!无需再添加!' form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3,'success3': success3}, context_instance = RequestContext(request)) else: form1 = TypeForm() form2 = VenderForm() form3 = ColorForm() return render_to_response('itemAdd.html', {'form1': form1,'form2': form2,'form3': form3}, context_instance = RequestContext(request))